selenium2(WebDriver)和Phpunit?

任何人都知道如何使用selenium2与Phpunit? PHP中是否有Selenium 2样本?

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


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

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

大多数客户要求你首先阅读协议,看看有什么可能,然后研究客户端本身,看看如何调用它。 这希望消除后面的一步。

它通过启动Selenium 2服务器来使用,该服务器在localhost:4444/wd/hub提供接口。

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

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

 <?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方法,将示例中的调用click element与文档中的元素/点击API调用进行比较。

testing代码然后可以被包装在常规的phpUnittesting中。

这不是原生的phpUnit支持,但它是一个相当强大的方法。

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

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

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

我build议使用phpunit-selenium这个库,因为它最初是为Selenium 1devise的(尽pipe它现在支持Selenium 2),它的API对PHPUnit非常严格。 它也不支持WebDriver的某些function,也不支持upcomin W3C WebDriver规范 。

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

项目主页上还提到了其他testing框架集成选项。

PHPUnit Selenium集成代码作为一个单独的项目在github中生存,据我所知,它不支持Selenium 2,所以你的问题的答案是 – 不,你不能在PHPUnit中使用Selenium 2。

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

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

如果你喜欢它,分享它,改进它或叉:)

问候,马克。

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

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

我写了一个关于如何使用Selenium 2,Facebook包装的教程,在这里find它:

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

我推荐使用Menta ,一个需要WebDriver的Selenium 2框架。 两个软件包都兼容PSR-0,所以你可以在Composer中使用它们。 你可以用phpunit.xmlconfigurationselenium。 这里是一个例子

 <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。*的configurationvariables,因此您可以轻松设置新variables。

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

现在你可以很容易地实现你的testing用例。 这里是一个例子

 <?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软件包也有两个演示文件,位于这里

今天,深入了解了Selenium和phpunit。 这是可能的,你可以在这里find一些例子和说明: http : //phpunit.de/manual/current/en/selenium.html

phpunit的创build者得到了一些很好的API示例。 通过一些实验和阅读错误信息,你会相处融洽。 我自己也没有find一个好的图书馆。

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/

是的, Selenium 2 (WebDriver)PHPUnit tests很简单。 但是,我想给你的build议,第一你应该尝试Selenium IDE因为你需要在selenium command期待。 如果你期望在Selenium command如果让selenium 2 (Webdriver)PHPUnit test对你更简单。

您可以在这里试试selenium IDE教程,您可以在这里学习selenium 2 (Webdriver) and PHPUnit

我在selenium2php上工作。 我用Selenium IDElogging了很多Selenium1的testing。 现在它将htmltesting转换成Selenium2。 其实,对于PHPUnit_Extensions_Selenium2TestCase。 我将实施更多的命令。