将Selenium Grid 2与PHPUnit测试一起使用 [英] Using Selenium Grid 2 with PHPUnit tests

查看:104
本文介绍了将Selenium Grid 2与PHPUnit测试一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近为一个相当复杂的项目编写了许多硒1测试.它们都是用php编写的,并且可以在selenium服务器1.0.7上顺利运行.

显然,在firefox 5(和今天发布的6)中,硒服务器1.0.7不再起作用.我已经尝试过,但是服务器只是打开空白窗口.

现在,我正在尝试使这些测试在硒网格上运行.我使用网格v1设法获得了一个集线器和几个远程控制runnnig,但是它们就像旧服务器一样只能打开空白窗口.所以我认为我需要升级到网格v2.

由于某种原因,我可以将客户端连接到集线器,但是如果我尝试针对集线器运行测试,则它似乎根本无法连接("PHPUnit_Framework_Exception:无法连接到Selenium RC服务器").我尝试在selenium独立服务器2.4.0上运行它们,这似乎确实起作用.

我在一个论坛上读到,硒网格2不适用于phpunit(还可以吗?).

如何使测试在网格上运行? phpunit连接到服务器缺少了什么?感谢您的帮助!

我按如下所示设置了集线器:

java -jar selenium-server-standalone-2.4.0.jar -role hub

还有两个奴隶:

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

直到这里一切都可以正常工作,因为我在网格控制台(http://localhost:4444/grid/console)中看到了两个节点.

这就是我在代码中所做的所有初始化:

<?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);
    }
}

此代码在独立服务器2.4.0上仍然有效.按预期,它在最后一行失败.

似乎在PHPUnit/Extensions/SeleniumTestCase/Driver.php中引发了异常.似乎有问题.

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.'
        );
    }
    [...]
}

当我在请求中 http://localhost:4444/selenium-driver/driver 时,浏览器,我得到:

    HTTP ERROR: 500
    org.openqa.grid.internal.GridException: Session not available - []
    RequestURI=/selenium-server/driver

有什么办法解决这个问题吗?我是否需要更改该网址?

解决方案

还要确保正确设置了网格,这是一则小文章,显示了如何完成此操作:

了解更多信息: http://code.google.com/p/php- webdriver-bindings/

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 ?

解决方案

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/

这篇关于将Selenium Grid 2与PHPUnit测试一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆