问题
I have recently written a lot of selenium 1 tests for a fairly complex project. They are all written in php and run smoothly on the selenium-server 1.0.7.
Obviously with firefox 5 (and 6 released today) selenium server 1.0.7 is not working anymore. I've tried, but the server is just opening blank windows.
Now I am trying to get those tests running on selenium grid. I managed to get a hub and a couple of remote-controls runnnig using grid v1, but they only open blank windows just like the old server did. So I figured i needed to upgrade to grid v2.
For some reason i can get the clients connected to the hub, but if I try running my tests against the hub it doesn't seem to be able to connect at all ("PHPUnit_Framework_Exception: Could not connect to the Selenium RC server"). I tried running them against selenium standalone server 2.4.0 and that does seem to work.
I read in a forum, that selenium grid 2 just doesn't work with phpunit (yet?).
How can i get my tests running on a grid? What is missing for phpunit to connect to the server? I appreciate any help!
I set up the hub as follows:
java -jar selenium-server-standalone-2.4.0.jar -role hub
And two slaves:
java -jar selenium-server-standalone-2.4.0.jar -role rc -hub http://127.0.0.1:4444/grid/register -port 5555
java -jar selenium-server-standalone-2.4.0.jar -role webdriver -hub http://127.0.0.1:4444/grid/register -port 5556
Everything seems to be working till here as i see two nodes in the grid console (http://localhost:4444/grid/console).
So here is all the initialization I am doing in code:
<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class Grid_Test extends PHPUnit_Extensions_SeleniumTestCase
{
public $captureScreenshotOnFailure = false;
public static $browsers = array(
'FFLinux' => array(
'name' => 'Firefox on Linux',
'browser' => '*firefox',
'host' => '127.0.0.1',
'port' => 4444,
'timeout' => 30000
));
public function setUp()
{
$this->setBrowserUrl('http://www.google.com');
}
public function testGridWorking()
{
$this->open('/');
$this->assertTrue(false);
}
}
This code still works on the standalone server 2.4.0. It fails at the last line as expected.
The Exception seems to be thrown in PHPUnit/Extentions/SeleniumTestCase/Driver.php. There seems to be the problem.
protected function doCommand($command, array $arguments = array())
{
$url = sprintf(
'http://%s:%s/selenium-server/driver/?cmd=%s',
$this->host,
$this->port,
urlencode($command)
);
[...]
$handle = @fopen($url, 'r', FALSE, $context);
if (!$handle) {
throw new PHPUnit_Framework_Exception(
'Could not connect to the Selenium RC server.'
);
}
[...]
}
When i request http://localhost:4444/selenium-driver/driver in the browser, i get:
HTTP ERROR: 500
org.openqa.grid.internal.GridException: Session not available - []
RequestURI=/selenium-server/driver
Any idea how to fix this? Do I need to change that url maybe ?
回答1:
Also make sure you have setup the Grid correctly here is a small post that shows how it is done : http://opensourcetester.co.uk/2011/07/06/selenium-grid-2/
BTW I dint see code that does the driver instantiation.. Am i missing something?
Here is how it is done:
require_once "phpwebdriver/WebDriver.php";
require("phpwebdriver/LocatorStrategy.php");
$webdriver = new WebDriver("localhost", "4444");
$webdriver->connect("firefox");
$webdriver->get("http://google.com");
$element = $webdriver->findElementBy(LocatorStrategy::name, "q");
$element->sendKeys(array("selenium google code" ) );
$element->submit();
$webdriver->close();
for more: http://code.google.com/p/php-webdriver-bindings/
回答2:
The problem with trying to use PHPUnit with Selenium Grid 2 has already been reported to the project owners. Have a look at the patch available here to see if it works for you.
Anyway, if I were you I would start thinking about migrating to WebDriver through one of the drivers available out there for PHP, like php-webdriver.
来源:https://stackoverflow.com/questions/7077090/using-selenium-grid-2-with-phpunit-tests