【问题标题】:Using Selenium Grid 2 with PHPUnit tests将 Selenium Grid 2 与 PHPUnit 测试一起使用
【发布时间】:2011-08-16 10:56:45
【问题描述】:

我最近为一个相当复杂的项目编写了很多 selenium 1 测试。它们都是用php编写的,在selenium-server 1.0.7上运行流畅。

很明显,使用 firefox 5(和今天发布的 6)selenium 服务器 1.0.7 不再工作了。我试过了,但服务器只是打开空白窗口。

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

出于某种原因,我可以让客户端连接到集线器,但是如果我尝试对集线器运行测试,它似乎根本无法连接(“PHPUnit_Framework_Exception:无法连接到 Selenium RC 服务器”)。我尝试在 selenium 独立服务器 2.4.0 上运行它们,这似乎确实有效。

我在一个论坛上看到,selenium grid 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/Extentions/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

知道如何解决这个问题吗?我是否需要更改该网址?

【问题讨论】:

  • 您能发布您的初始化代码,即进行设置的代码吗??

标签: phpunit selenium-grid


【解决方案1】:

另外,请确保您已正确设置了 Grid,这是一个小帖子,展示了它是如何完成的:http://opensourcetester.co.uk/2011/07/06/selenium-grid-2/

顺便说一句,我看不到驱动程序实例化的代码。我错过了什么吗?

这是怎么做的:

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();

更多:http://code.google.com/p/php-webdriver-bindings/

【讨论】:

  • 感谢您的链接。我以前读过,这正是我的做法。
  • 显然 PHPUnit_Extensions_SeleniumTestCase 还不支持 Selenium 2.0。您链接到的那些绑定也对我不起作用。但是我有一些工作要做,所以谢谢!
【解决方案2】:

已经向项目所有者报告了尝试将 PHPUnit 与 Selenium Grid 2 一起使用的问题。查看可用的补丁 here 看看它是否适合您。

无论如何,如果我是你,我会开始考虑通过 PHP 可用的驱动程序之一迁移到 WebDriver,例如 php-webdriver

【讨论】:

    猜你喜欢
    • 2012-02-23
    • 2023-04-06
    • 2012-07-23
    • 2013-04-29
    • 2012-03-21
    • 2012-02-29
    • 1970-01-01
    • 2016-02-24
    • 2016-05-12
    相关资源
    最近更新 更多