Here my problem : I'm in a specific case where I try to set the option of a select dropdown list.
I usually use this.mouse.up() + this.mouse.down()
but I can't in this case because this behavior doesn't work on the website with webkit (you can compare the two with google chrome and Firefox).
Here the url : I want to set the field 'ANNEE' to a year, 2008 in my example
My code : (my function changes the HTML and launches the change() event)
//custom function
casper.fillSelect = function(selectSelector, optionText){
this.evaluate(function(sel,setByText) {
if ("createEvent" in document) {
var evt = document.createEvent("HTMLEvents")
,x = document.querySelectorAll(sel + ' > option')
,l = x.length
;
evt.initEvent("change", false, true);
for (i=0; i<l; i++){
if(x[i].textContent.indexOf(setByText) !== -1){
console.log(x[i]);
console.log(x[i].getAttribute('value'));
x[i].setAttribute('selected', true);
x[i].parentNode.dispatchEvent(evt);
}
}
}
else {console.log("error with fillSelect");}
},selectSelector, optionText);
};
//event
casper.test.on('fail', function(failure) {
casper.capture('fail.png');
});
/*************************************** Tests *****************************************************/
casper.test.begin('\n********* Compare : ***********', function (test) {
"use strict";
casper.start()
.thenOpen("http://www.linternaute.com/voyage/climat/paris/ville-75056",function(){
casper.fillSelect('fieldset.fcNeutre > div.odSelect:nth-of-type(2) > select', '2008');
})
.waitForUrl(/2008/, function(){
this.capture('fail2.png');
this.test.assertSelectorHasText("h2", "maximales");
this.test.assertSelectorHasText("h2", "minimales");
this.test.assertSelectorHasText("h2", "Paris");
this.test.assertSelectorHasText("h2", "Le soleil");
this.test.assertSelectorHasText("h2", "La pluie");
this.test.assertExists("tspan");
this.test.assertExists("div.marB20");
this.test.assertNotEquals(this.fetchText("div.marB20 > table > thead > tr > th"), "", "Table first data not empty");
})
.run(function() {
this.test.comment('--- Done ---\n');
test.done();
});
});
And the equivalent to my casper custom function, you can execute it in the browser :
var fillSelect = function(sel,setByText) {
if ("createEvent" in document) {
var evt = document.createEvent("HTMLEvents")
,x = document.querySelectorAll(sel + ' > option')
,l = x.length
;
evt.initEvent("change", false, true);
for (i=0; i<l; i++){
if(x[i].textContent.indexOf(setByText) !== -1){
//console.log(x[i]);
//console.log(x[i].getAttribute('value'));
x[i].setAttribute('selected', true);
x[i].parentNode.dispatchEvent(evt);
}
}
}
else {console.log("error with fillSelect");}
};
fillSelect('fieldset.fcNeutre > div.odSelect:nth-of-type(2) > select', '2008');
So it works in FF, Google Chrome, with slimerJS, but not with PhantomJS ... help please, if you have another idea to just select one option in this 'ANNEE' field, with casper+phantom, I take !
Could it be a problem of browser compatibility?
It's strange because in the same website, sometimes it works with other 'select', identical to this one ...