1

我正在尝试使用 PhantomJS 作为 PHPUnit Selenium 测试的浏览器。

我已将 Selenium 设置为在网格模式下运行,并使用 webdriver 启动 phantomjs,并将其注册到网格中,如GhostDriver 自述文件中所示。

当我运行 selenium 测试时,它因未知命令错误而失败 - GhostDriver 只是不明白 PHPUnit 在说什么。

[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
    }
  ]
}

在GhostDriver 网站上提出了同样的问题,但没有得到答复,并建议归咎于 PHPUnit。情况可能是这样,但我仍然没有更接近完成这项工作。有谁知道如何解决它?

4

1 回答 1

3

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.

于 2013-06-29T18:35:02.540 回答