nightwatch: PhantomJS fails on demo test example

When running the demo test example on the nightwatchjs.org homepage using PhantomJS as the browser, I get the following output:

[Google Test] Test Suite
========================

Running:  Demo test Google 

✔  Element <body> was visible after 53 milliseconds.
✔  Testing if the page title equals "Google".
✖  Testing if element <input[type=text]> is visible. Element could not be located.  - expected "true" but got: null
ERROR: Unable to locate element: "input[type=text]" using: css selector


TEST FAILURE: 1 error during execution, 1 assertions failed, 2 passed and 6 skipped.. (6489 ms)

Strangely, if I change .assert.visible("input[type=text]") to .assert.visible("input[name=q]") then that step passes but the test later fails on another step: waitForElementVisible("button[name=btnG]", 1000).

My environment:

Mac OSX 10.9.4 Nightwatch 0.5.18 PhantomJS 1.9.2 selenium-server-standalone-2.42.0.jar

Note: the test runs fine against Firefox.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 20 (5 by maintainers)

Most upvoted comments

As @decates indicated above, if you want to use phantomjs, but are okay with it spoofing as a different browser via the user agent, you can configure phantomjs’ user-agent capabilities like this (spoofing as Mac Chrome here):

"desiredCapabilities": {
  "browserName": "phantomjs",
  "phantomjs.cli.args" : ["--ignore-ssl-errors=true"],
  "phantomjs.page.settings.userAgent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"
}

Then your tests should act the same as your other browser. Using any browser you like, you can check the user-agent string that it sends here: http://www.httpuseragent.org/. Here are some other examples:

// Mac Chrome 46
"phantomjs.page.settings.userAgent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"

// Windows Chrome 46
"phantomjs.page.settings.userAgent" : "Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"

// Mac Firefox 42.0
"phantomjs.page.settings.userAgent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:42.0) Gecko/20100101 Firefox/42.0"

// Windows Firefox 42.0
"phantomjs.page.settings.userAgent" : "Mozilla/5.0 (Windows NT 6.3; rv:42.0) Gecko/20100101 Firefox/42.0"

// PhantomJS 2.0
"phantomjs.page.settings.userAgent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.0.0 Safari/538.1"

Just to note, that the demo test on the http://nightwatchjs.org/ website still fails in PhantomJS

It might be better to use the nightwatchjs.org website as the target for the demo test - so that it wont break if/when google decides to change the way their search page works?

Just to clarify the original issue, this is caused by Google serving up different content depending on the user-agent, and (validly) omitting type="text" on the input element, as it’s the default value.

For Chrome 41 the page is XHTML, and the text box looks like this:

<input
    class="gsfi lst-d-f"
    id="lst-ib"
    maxlength="2048"
    name="q"
    autocomplete="off"
    title="Search"
    type="text"
    value=""
    aria-label="Search"
    aria-haspopup="false"
    role="combobox"
    aria-autocomplete="both"
    dir="ltr"
    spellcheck="false"
    style="border: none; ..."
/>

For PhantomJS 2.0.0 it looks like this.

<input
    style="color: rgb(0, 0, 0); margin: 0px; padding: 5px 8px 0px 6px; vertical-align: top; outline: none;"
    autocomplete="off"
    class="lst"
    value=""
    title="Google Search"
    maxlength="2048"
    name="q"
    size="57"
    dir="ltr" 
    spellcheck="false">

Perhaps the sample test could be changed to use input[name="q"], or just test nightwatchjs.org instead, as you have control over what is served up?