GOAL
Here I am trying to perfom a basic search navigation on the
Pubmed website. Lets say that the term that I am searching for is Hello
. What I expect at the end is to land on a result page after clicking the search button.
PROBLEM
The submit code works perfectly on the javascript console of the chrome browser. But it doesn't work through casperjs. The current-url seems to remain the same. I couldn't figure out where the problem is.
My Code
// USAGE: casperjs test navigation_test.js
var config = {
url: 'https://www.ncbi.nlm.nih.gov/pubmed/',
};
config.form = {
"term": "Hello",
};
casper.test.begin('Testing navigation and forms', 2, function suite(test) {
test.comment('⌚ Loading ' + config.url + '...');
casper.start(config.url, function() {
// adjust the view port
this.viewport(1280, 1024);
});
// #1 method-2 (short)
casper.then(function() {
this.fill('form#EntrezForm', config.form, true);
})
// #2
casper.then(function() {
test.assertUrlMatch(/term/, 'New location is ' + this.getCurrentUrl());
});
casper.run(function () {
test.done();
});
});
Extra Code
The #1 method
in the above code is short. I have also tried a longer version as follows. But it didn't work either.
// #1 method-1 (long)
casper.then(function() {
this.evaluate(function() {
$('term').value = "Hello"
});
test.assertEvalEquals(function () {
return $('term').value;
}, "Hello", 'The search was filled out properly.');
this.click('button[id="search"][type="submit"][class="button_search nowrap"]');
// OR
// this.clickLabel('Search', 'button');
// OR
/*this.evaluate(function() {
$('search').click();
// OR
// document.getElementById('search').click();
});*/
});