【问题标题】:Selenium 2 (WebDriver) and Phpunit?Selenium 2 (WebDriver) 和 Phpunit?
【发布时间】:2011-05-11 12:43:18
【问题描述】:

有人知道如何将 Selenium 2 与 Phpunit 一起使用吗? PHP 中有 Selenium 2 示例吗?

【问题讨论】:

  • +1 这是一个很好的问题。前段时间我想问一下,因为,很抱歉,Selenium 页面对 PHP 用户不是很友好。最近有很多关于 WebDriver 的讨论,但我还没有看到这两个在行动。
  • PHPUnit 从 3.6 版本开始支持 Selenium2 详情见phpunit.de/manual/3.6/en/…

标签: php selenium phpunit selenium-webdriver


【解决方案1】:

PHPUnit Selenium 集成代码在github 中作为一个单独的项目存在,据我所知,它不支持 Selenium 2,因此您的问题的答案是 - 不,您不能将 Selenium 2 与 PHPUnit 一起使用。

但您可以克隆源代码树并使其与 Selenium 2 一起使用。

【讨论】:

【解决方案2】:

请查看http://code.google.com/p/php-webdriver-bindings/。这是一个使用 JsonWireProtocol 与 Selenium Webdriver 服务器通信的 PHP 库。这是早期版本,但它可以工作。

【讨论】:

【解决方案3】:

phpunit webdriver 绑定托管在 google 代码上。除此之外,我们还需要了解一些东西。

  1. 需要安装 PHPUnit。 (通过 PEAR 包或手动下载安装)
  2. 必须下载并安装 Eclipse PDT 等 PHP IDE。
  3. Selenium-Stand-Alone 服务器必须在执行 WebDriver Selenium 测试时运行

【讨论】:

  • 为什么需要安装IDE?
【解决方案4】:

快速更新: phpunit 现在支持 Selenium 2


在撰写本文时,PHPUnit 不支持 Selenium 2。

php-webdriver from facebook 允许从 PHP 以优雅的方式调用完整的 WebDriver API。引用:

大多数客户要求您先阅读协议以了解其内容 可能,然后研究客户端本身,看看如何调用它。这 希望消除后一步。

它用于启动 Selenium 2 服务器,它提供了localhost:4444/wd/hub 的接口。

/usr/bin/java -jar /path/to/selenium-server-standalone-2.7.0.jar

然后运行调用该接口的 PHP 测试代码。例如:

<?php

require '/path/to/php-webdriver/__init__.php';

$webdriver = new WebDriver();

$session = $webdriver->session('opera', array());
$session->open("http://example.com");
$button = $session->element('id', 'my_button_id');
$button->click();
$session->close();

WebDriver API 映射到 PHP 方法,将示例中在 element 上调用 click 与文档中的 element/click API 调用进行比较。

然后可以将测试代码包装在常规的 phpUnit 测试中。

这不是原生 phpUnit 支持,但它是一种非常强大的方法。

【讨论】:

  • 感谢您指向这个 facebook 包装器。似乎很多命令都不起作用,例如 sendKeys
  • 嘿阿迪尔!你的意思是很多命令在其他 PHP selenium 实现中不起作用,对吧?这只是使用它的另一个原因,该架构固有地使其完整。
  • @cmc:@php-webdriver@ 中没有任何内容可以使其“内在完整”(认为添加新命令似乎相当容易)。有一个接受命令的静态列表(参见各个类中的 @getMethod()@ 函数)。
  • @Tgr:请参阅“直接协议翻译的一些不可避免的例外”部分。在the readme.
  • 快速更新 - phpunit 现在支持 Selenium 2 phpunit.de/manual/3.6/en/selenium.html
【解决方案5】:

我们为此创建了一个库,希望对您有所帮助。它也使用 JSON Wire 协议,但我们的目标是使其与其他语言的示例兼容,因此语法将非常相似。这是链接:https://github.com/Nearsoft/PHP-SeleniumClient

如果您喜欢,请分享、改进或分叉 :)

问候,马克。

【讨论】:

    【解决方案6】:

    我写了一篇关于如何使用 Selenium 2,Facebook 包装器的教程,在这里可以找到:

    http://testigniter.blogspot.co.uk/2012/01/running-selenium-2-webdriver-using.html

    【讨论】:

      【解决方案7】:

      我推荐使用 Menta,这是一个需要 WebDriver 的 Selenium 2 框架。这两个包都兼容 PSR-0,因此您可以将它们与 Composer 一起使用。您可以使用 phpunit.xml 配置 selenium。这里是一个例子

      <phpunit bootstrap="tests/bootstrap.php"
               backupGlobals="false" backupStaticAttributes="false"
               strict="true" verbose="true">
          <php>
              <var name="testing.selenium.seleniumServerUrl" value="http://localhost:4444/wd/hub" />
              <var name="testing.selenium.browser" value="firefox" />
              <var name="testing.selenium.windowPosition" value="0,0" />
              <var name="testing.selenium.windowSize" value="1280x1024" />
              <var name="testing.selenium.windowFocus" value="1" />
              <var name="testing.selenium.timeoutImplicitWait" value="10000" />
          </php>
          <testsuites>
              <testsuite name="Integrationstests">
                  <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">tests/integration</directory>
              </testsuite>
          </testsuites>
          <logging>
              <log type="junit" target="build/logs/junit.xml"/>
          </logging>
      </phpunit>
      

      引导文件从 testing.selenium.* 中读取配置变量,因此您可以轻松设置新变量。

      <?php
      
      \Menta_ConfigurationPhpUnitVars::addConfigurationFile(__DIR__ . '/../phpunit.xml');
      
      $configuration = \Menta_ConfigurationPhpUnitVars::getInstance();
      \Menta_SessionManager::init(
          $configuration->getValue('testing.selenium.seleniumServerUrl'),
          $configuration->getValue('testing.selenium.browser')
      );
      

      现在您可以轻松实现您的测试用例。这里是一个例子

      <?php
      
      namespace tests\integration;
      
      use WebDriver\LocatorStrategy;
      
      class TestSearch extends \PHPUnit_Framework_TestCase
      {
          public function testGoogle()
          {
              $session = \Menta_SessionManager::getSession();
              $session->open('http://www.google.de');
              $element = $session->element(LocatorStrategy::NAME, 'q');
              $this->assertTrue($element->displayed());
          }
      }
      

      Menta 包还有两个演示文件,位于here

      【讨论】:

        【解决方案8】:

        是的,Selenium 2 (WebDriver)PHPUnit tests 很简单。但是我想给你一个建议,首先你应该尝试Selenium IDE,因为你需要期待selenium command。如果您期望在Selenium command 中,则selenium 2 (Webdriver)PHPUnit test 对您来说会更简单。

        您可以在here 上尝试selenium IDE 教程,也可以在here 上学习selenium 2 (Webdriver) and PHPUnit

        【讨论】:

          【解决方案9】:

          今天我们深入了解了 Selenium 和 phpunit。 这是可能的,您可以在这里找到一些示例和说明: http://phpunit.de/manual/current/en/selenium.html

          phpunit 的创建者得到了一些很好的 api 示例。 通过一些实验并阅读错误消息,您将相处融洽。 我自己也没有找到一个很棒的图书馆。

          https://github.com/sebastianbergmann/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php

          作为最后一个来自 nettuts 的教程,它可以帮助您了解基础知识: http://net.tutsplus.com/tutorials/php/how-to-use-selenium-2-with-phpunit/

          【讨论】:

            【解决方案10】:

            我在 selenium2php 工作。我有太多用 Selenium IDE 记录的 Selenium1 测试。现在它将 html 测试转换为 Selenium2。实际上,对于 PHPUnit_Extensions_Selenium2TestCase。我将执行更多命令。

            【讨论】:

              【解决方案11】:

              目前(2017 年)我推荐使用 php-webdriver,AFAIK 与 Selenium WebDriver 交互的功能最完整的 PHP 语言绑定是什么。

              这个库在 2014 年被重写以支持 Selenium 2,它的 API 主要基于官方的 Java WebDriver 绑定。这意味着您还可以利用用 Java 编写的代码示例,因为它们通常可以在 PHP 中简单地遵循。它还以现代 OOP 方式编写,并遵循标准 PSR-4 命名空间以及 PSR-2 编码标准。

              我会推荐这个库而不是 phpunit-selenium - 因为它最初是为 Selenium 1 设计的(尽管它现在支持 Selenium 2)并且它的 API 与 PHPUnit 非常紧密。它也不支持某些 WebDriver 功能,并且与 upcomin W3C WebDriver specification 不是最新的。

              另一方面,Php-webdriver 是独立的库,但它与 PHPUnit 的集成 非常简单 - 或者您可以使用现有的工具,如 Steward,其中包括所有 PHPUnit 集成并提供很好的便利层,例如。允许简单地并行运行多个测试(不需要像 paratest 这样的其他工具)。

              project homepage 中还提到了其他测试框架集成选项。

              【讨论】:

              • 及时,因为我最近刚刚重试了半官方的 phpunit-selenium 以发现文档仍然缺乏。不过,让这个可靠地工作也是一个挑战。
              猜你喜欢
              • 2012-09-23
              • 2014-08-25
              • 1970-01-01
              • 2018-01-21
              • 2012-03-30
              • 1970-01-01
              • 2018-01-30
              • 2014-01-07
              • 2016-07-27
              相关资源
              最近更新 更多