Question

I'm attempting to use PhantomJS as a browser for PHPUnit Selenium tests.

I've set Selenium running in grid mode, and started phantomjs with webdriver, and registered it to the grid, as in the GhostDriver Readme.

When I run a selenium test, it fails with an unknown command error - GhostDriver just doesn't understand what PHPUnit is saying.

[ERROR - 2013-05-12T16:23:06.326Z] RouterReqHand - _handle - Thrown => {
  "message": "Request => {\"headers\":{\"Accept\":\"*/*\",\"Connection\":\"Keep-Alive\",\"Content-Length\":\"85\",\"Content-Type\":\"application/x-www-form-urlencoded; charset=utf-8\",\"Host\":\"127.0.0.1:4444\"},\"httpVersion\":\"1.1\",\"method\":\"POST\",\"post\":\"cmd=getNewBrowserSession&1=phantomjs&2=https%3A%2F%2Ftest.testurl.com%2F&\",\"url\":\"/\",\"urlParsed\":{\"anchor\":\"\",\"query\":\"\",\"file\":\"\",\"directory\":\"/\",\"path\":\"/\",\"relative\":\"/\",\"port\":\"\",\"host\":\"\",\"password\":\"\",\"user\":\"\",\"userInfo\":\"\",\"authority\":\"\",\"protocol\":\"\",\"source\":\"/\",\"queryKey\":{},\"chunks\":[\"\"]}}",
  "name": "Unknown Command",
  "line": 87,
  "sourceId": 139810136032448,
  "sourceURL": ":/ghostdriver/request_handlers/router_request_handler.js",
  "stack": "Unknown Command: Request => {\"headers\":{\"Accept\":\"*/*\",\"Connection\":\"Keep-Alive\",\"Content-Length\":\"85\",\"Content-Type\":\"application/x-www-form-urlencoded; charset=utf-8\",\"Host\":\"127.0.0.1:4444\"},\"httpVersion\":\"1.1\",\"method\":\"POST\",\"post\":\"cmd=getNewBrowserSession&1=phantomjs&2=https%3A%2F%2FFtest.testurl.com%2F&\",\"url\":\"/\",\"urlParsed\":{\"anchor\":\"\",\"query\":\"\",\"file\":\"\",\"directory\":\"/\",\"path\":\"/\",\"relative\":\"/\",\"port\":\"\",\"host\":\"\",\"password\":\"\",\"user\":\"\",\"userInfo\":\"\",\"authority\":\"\",\"protocol\":\"\",\"source\":\"/\",\"queryKey\":{},\"chunks\":[\"\"]}}\n    at :/ghostdriver/request_handlers/router_request_handler.js:87",
  "stackArray": [
    {
      "sourceURL": ":/ghostdriver/request_handlers/router_request_handler.js",
      "line": 87
    }
  ]
}

This same question was asked and closed unanswered on the GhostDriver site with the suggestion that it's PHPUnit to blame. That may be the case, but I'm still no nearer to making this work. Does anyone have any idea how to fix it?

Was it helpful?

Solution

It looks like you are using a test class extending PHPUnit_Extensions_SeleniumTestCase. Use PHPUnit_Extensions_Selenium2TestCase instead.

Unfortunately, that's not the end of the story. The syntax of the Selenium-related methods changes when you swap out the base class.

The dated PHPUnit_Extensions_SeleniumTestCase class

  • uses the Selenium RC API
  • does not support Phantom.js as a browser
  • requires the "traditional" syntax as documented here
  • works fine with code which is generated in Selenium IDE and exported with the help of the "Selenium IDE: PHP Formatters" plugin.

By contrast, PHPUnit_Extensions_Selenium2TestCase

  • uses the WebDriver API
  • supports Phantom.js
  • requires a different set of commands which is not well documented - this test case demonstrates it by example, and that's about it
  • does not work with code exported from Selenium IDE without an extensive rewrite.

So it is possible to run PHPUnit-driven Selenium tests faster with PhantomJS, but it does come at a cost.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top