php Selenium 2 (WebDriver) 和 Phpunit?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4206547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium 2 (WebDriver) and Phpunit?
提问by Paul R Rogers
Any one know how to use Selenium 2 with Phpunit? Are there any Selenium 2 samples in PHP?
有人知道如何在 Phpunit 中使用 Selenium 2 吗?PHP 中是否有任何 Selenium 2 示例?
采纳答案by cmc
Quick update:phpunit does now support Selenium 2
快速更新:phpunit 现在支持 Selenium 2
At the time of writing, PHPUnit does not support Selenium 2.
在撰写本文时,PHPUnit 不支持 Selenium 2。
php-webdriverfrom facebookallows the complete WebDriver API to be called from PHP in an elegant way. To quote:
来自facebook 的php-webdriver允许以优雅的方式从 PHP 调用完整的 WebDriver API。报价:
Most clients require you to first read the protocol to see what's possible, then study the client itself to see how to call it. This hopes to eliminate the latter step.
大多数客户端要求您首先阅读协议以了解可能的情况,然后研究客户端本身以了解如何调用它。这希望消除后一步。
It is used by starting up the Selenium 2 server, which provides the interface at localhost:4444/wd/hub.
它通过启动 Selenium 2 服务器来使用,它在localhost:4444/wd/hub.
/usr/bin/java -jar /path/to/selenium-server-standalone-2.7.0.jar
then running the PHP test code, which calls that interface. For example:
然后运行调用该接口的 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();
The WebDriver APIis mapped to PHP methods, compare calling clickon elementin the example with the element/click API call in the documentation.
该webdriver的API映射到PHP方法,比较呼吁click对element在文档中的元素/点击API调用的例子。
The test code can then be wrapped in regular phpUnit tests.
然后可以将测试代码包装在常规的 phpUnit 测试中。
This is not native phpUnit support, but it's a quite robust approach.
这不是原生的 phpUnit 支持,但它是一种非常强大的方法。
回答by kolec
please look at the http://code.google.com/p/php-webdriver-bindings/. This is PHP library that communicates with Selenium Webdriver server using JsonWireProtocol. This is early version but it works.
请查看http://code.google.com/p/php-webdriver-bindings/。这是使用 JsonWireProtocol 与 Selenium Webdriver 服务器通信的 PHP 库。这是早期版本,但它有效。
回答by Ondrej Machulda
Currently (2017) I recommend using php-webdriver, what is AFAIK the most feature complete PHP language binding to interact with Selenium WebDriver.
目前(2017 年)我推荐使用php-webdriver,AFAIK 是什么功能最完整的 PHP 语言绑定与 Selenium WebDriver 交互。
This library was rewritten in 2014 to support Selenium 2, and its API is mostly based on the official Java WebDriver bindings. This means you can also take advantage of code examples written in Java, as they can be usually simply followed in PHP. Its also written in a modern OOP way and follows standard PSR-4 namespaces and also PSR-2 coding standards.
该库在 2014 年重写以支持 Selenium 2,其 API 主要基于官方 Java WebDriver 绑定。这意味着您还可以利用用 Java 编写的代码示例,因为它们通常可以在 PHP 中简单地遵循。它也是以现代 OOP 方式编写的,并遵循标准的 PSR-4 命名空间和 PSR-2 编码标准。
I would recommend this library over phpunit-selenium- as it was originally designed for Selenium 1 (though it nowadays support Selenium 2) and its API is strongly tight to PHPUnit. It also does not support some of the WebDriver features and is not up-to-date with upcomin W3C WebDriver specification.
我会推荐这个库而不是phpunit-selenium- 因为它最初是为 Selenium 1 设计的(尽管它现在支持 Selenium 2)并且它的 API 对 PHPUnit 非常严格。它也不支持某些 WebDriver 功能,并且不符合最新的W3C WebDriver 规范。
Php-webdriver is on the other hand independent library, but its integration with PHPUnitis quite simple - or you can use existing tools like Steward, which includes all the PHPUnit integration and provide also nice convenience layer and eg. allow to simply run multiple tests in parallel (without a need of another tools like paratest).
另一方面,Php-webdriver 是独立的库,但它与 PHPUnit 的集成非常简单——或者您可以使用现有的工具,如Steward,它包括所有 PHPUnit 集成并提供很好的便利层等。允许简单地并行运行多个测试(不需要其他工具,如paratest)。
There are also other testing framework integration options mentioned on the project homepage.
项目主页上还提到了其他测试框架集成选项。
回答by Anti Veeranna
PHPUnit Selenium integration code lives as a separate project in github, as far as I can see it does not support Selenium 2, so the answer to your question would be - No, you can not use Selenium 2 with PHPUnit.
PHPUnit Selenium 集成代码作为一个单独的项目存在于github 中,据我所知它不支持 Selenium 2,所以您的问题的答案是 - 不,您不能将 Selenium 2 与 PHPUnit 一起使用。
But you can clone the source tree and make it work with Selenium 2.
但是您可以克隆源代码树并使其与 Selenium 2 一起使用。
回答by markdrake
We created a library for that, I hope it helps. It also uses the JSON Wire protocol but we aimed to make it compatible with the examples from other languages, so the syntax would be very similar. Here's the link: https://github.com/Nearsoft/PHP-SeleniumClient
我们为此创建了一个库,希望对您有所帮助。它还使用 JSON Wire 协议,但我们旨在使其与其他语言的示例兼容,因此语法将非常相似。这是链接:https: //github.com/Nearsoft/PHP-SeleniumClient
If you like it, share it, improve it or fork it :)
如果你喜欢它,分享它,改进它或分叉它:)
Regards, Mark.
问候,马克。
回答by Adamantus
I wrote a tutorial about how to use Selenium 2, Facebook wrapper, find it here:
我写了一篇关于如何使用 Selenium 2 的教程,Facebook 包装器,在这里找到它:
http://testigniter.blogspot.co.uk/2012/01/running-selenium-2-webdriver-using.html
http://testigniter.blogspot.co.uk/2012/01/running-selenium-2-webdriver-using.html
回答by falsch
I recommened the usage of Menta, a Selenium 2 Framework which requires WebDriver. Both packages are PSR-0 compatible, so you can use them with Composer. You can configure selenium with the phpunit.xml. Here an example
我推荐使用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>
The bootstrap file reads the configuration variables from testing.selenium.*, so you can easily set new variables.
引导文件从 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')
);
Now you can easily implement you testcases. Here an example
现在您可以轻松实现您的测试用例。这里有一个例子
<?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());
}
}
The Menta Package also have two demo files, located here
Menta 包还有两个演示文件,位于此处
回答by user887648
phpunit webdriver bindings are hosted on google code. There is something we need to understand beyond this.
phpunit webdriver 绑定托管在谷歌代码上。除此之外,我们还需要了解一些东西。
- PHPUnit needs to be installed. (Either through PEAR package or download and install manually)
- PHP IDE such as Eclipse PDT has to be downloaded and installed.
- Selenium-Stand-Alone server has to be running while executing the WebDriver Selenium test
- 需要安装 PHPUnit。(通过PEAR包或者手动下载安装)
- 必须下载并安装 PHP IDE,例如 Eclipse PDT。
- Selenium-Stand-Alone 服务器必须在执行 WebDriver Selenium 测试时运行
回答by HerrWalter
Today a took a deep jump into Selenium and phpunit. It is possible and you may find some examples and instructions over here: http://phpunit.de/manual/current/en/selenium.html
今天深入了解了 Selenium 和 phpunit。这是可能的,您可以在这里找到一些示例和说明:http: //phpunit.de/manual/current/en/selenium.html
Creator of phpunit got some nice examples of the api. With a little experimentation and reading the error message, you'll get along. Havn't found a great library myself as well.
phpunit 的创建者得到了一些很好的 api 示例。通过一些实验和阅读错误消息,你会相处得很好。我自己也没有找到一个很棒的图书馆。
https://github.com/sebastianbergmann/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php
https://github.com/sebastianbergmann/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php
As last a tutorial with from nettuts which helps you along the basics: http://net.tutsplus.com/tutorials/php/how-to-use-selenium-2-with-phpunit/
最后一个来自 nettuts 的教程可以帮助您了解基础知识:http://net.tutsplus.com/tutorials/php/how-to-use-selenium-2-with-phpunit/
回答by rNix
I work on selenium2php. I have too many tests for Selenium1 recorded with Selenium IDE. Now it converts html tests into Selenium2. Actually, for PHPUnit_Extensions_Selenium2TestCase. I am going to implement more commands.
我在selenium2php 上工作。我用 Selenium IDE 记录了太多的 Selenium1 测试。现在它将 html 测试转换为 Selenium2。实际上,对于 PHPUnit_Extensions_Selenium2TestCase。我将执行更多命令。

