Reputation: 4329
I'm currently working on writing an application test suite with WebdriverJS and PhantomJS. To ensure that my tests work, I run them via Chrome first, and they all work fine. When I swap out Chrome for PhantomJS however, the tests break.
This question - WebDriver PhantomJS Unable to find element, but works fine with Firefox - appears to outline a very similar problem, but the solution enclosed does not appear to help.
Here's a rough example of the type of thing that works on Chrome, but not on PhantomJS:
var client = webdriverjs.remote({
desiredCapabilities: {
browserName: 'chrome'
},
logLevel: 'silent'
});
client.waitForExist("[data-id='1568911']", function(e){
client.click("[data-id='1568911']", function(e){
assert(!e, "Should click on a specific element:" + element);
});
});
When running on PhantomJS, I obviously change the WebdriverJS options first:
var client = webdriverjs.remote({
desiredCapabilities: {
browserName: 'phantomjs',
'phantomjs.binary.path': "path/to/phantomjs"
},
logLevel: 'silent'
});
But when I run the tests and set the logLevel to 'verbose', I receive error messages like the one below:
[12:43:34]: COMMAND POST "/wd/hub/session/eb2b0a4b-e659-4607-bec0-82209bd6539a/element"
[12:43:34]: DATA {"using":"css selector","value":"[data-id='1568911']"}
[12:43:35]: ERROR UnknownError An unknown server-side error occurred while processing the command.
{"errorMessage":"Unable to find element with css selector '[data-id='1568911']'","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"54","Content-Type":"application/json; charset=utf-8","Host":"localhost:12784","User-Agent":"Apache-HttpClient/4.3.2 (java 1.5)"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"css selector\",\"value\":\"[data-id='1568911']\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/2e1ff0a0-68d7-11e4-ad4c-3105ad572a89/element"}}
Why do common CSS2+ selectors like "[data-id='1568911']", or even "#foo", not work on PhantomJS via WebdriverJS? Is it a PhantomJS bug, a WebdriverJS bug, or a mistake I've made in my implementation?
Upvotes: 1
Views: 737
Reputation: 4329
So at least for the google page it should mean that WebdriverJS does something wrong with the userAgent string. But that doesn't mean that it is the same issue as for your original page.
Something like that...
The default WebdriverJS user agent value is fine. The problem (for the page I was testing on, at least) came from a block of code that looks roughly like this:
if(navigator.onLine){
renderPage()
} else doSomethingElse();
It appears that PhantomJS always sets navigator.onLine to false...
https://github.com/ariya/phantomjs/issues/10647
Doh.
That said, I can't understand why anyone would want to use the navigator.onLine value in a product that's always online. One of those classics that can only be found in someone else's code...
Painful bug.
Thanks for the help.
Upvotes: 1