有人知道如何在Phpunit中使用Selenium 2吗? 是否有用PHP编写的Selenium 2示例?
有人知道如何在Phpunit中使用Selenium 2吗? 是否有用PHP编写的Selenium 2示例?
快速更新:phpunit现在支持Selenium 2
目前为止,PHPUnit不支持Selenium 2。
facebook的php-webdriver以优雅的方式允许从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
与文档中的元素/点击API调用进行比较。
然后,测试代码可以包装在常规的phpUnit测试中。
这不是本地phpUnit支持,但它是一种相当稳健的方法。
目前(2017年),我建议使用php-webdriver,这是我所知道的与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功能,并且没有更新到即将发布的W3C WebDriver规范。
另一方面,Php-webdriver是一个独立的库,但它与PHPUnit的集成非常简单 - 或者您可以使用现有的工具,如Steward,它包括所有PHPUnit集成,并提供方便的层,例如允许简单地并行运行多个测试(无需另一个工具,如paratest)。
在项目主页上还提到了其他测试框架集成选项。
<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>
<?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软件包还有两个演示文件,位于这里
http://testigniter.blogspot.co.uk/2012/01/running-selenium-2-webdriver-using.html
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/。PHPUnit WebDriver绑定托管在Google Code上。除此之外,还有一些需要我们了解的内容。
我在selenium2php上工作。我有太多的Selenium1测试记录在Selenium IDE中。现在,它将html测试转换为Selenium2。实际上,是为了PHPUnit_Extensions_Selenium2TestCase。我将要实现更多的命令。