如何在 PHP 中使用 Selenium?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6590360/
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
How to use Selenium with PHP?
提问by StackOverflowNewbie
I'd like to use Selenium to automate a few web tasks (not for testing). I think I have Selenium RC Server installed, but have no way of writing "test scripts" since I can't find a client driver in PHP (see: http://seleniumhq.org/download/).
我想使用 Selenium 来自动化一些网络任务(不用于测试)。我想我已经安装了 Selenium RC 服务器,但无法编写“测试脚本”,因为我在 PHP 中找不到客户端驱动程序(请参阅:http: //seleniumhq.org/download/)。
Is there a way for me to use Selenium with PHP? This seems to suggest I need PHPUnit http://www.phpunit.de/manual/current/en/selenium.html. I just want to automate a few tasks, not get involved with a full suite of testing.
有没有办法让我在 PHP 中使用 Selenium?这似乎表明我需要 PHPUnit http://www.phpunit.de/manual/current/en/selenium.html。我只想自动化一些任务,而不是参与一整套测试。
采纳答案by lAH2iV
Try Following things
尝试以下事情
- Get Phpunit installed and working
- Also have JAVA sdk & jre on your pc.
- Now record test cases using selenium IDE.
- Export the testcases to php files.
- Using these exported functions create an library of test cases.
- Create suite which calls the functions/tests from library.
- Now to execute Start Selenium Server using java command.
- Using phpunit Execute the suite.
- 安装并运行 Phpunit
- 在你的电脑上也有 JAVA sdk 和 jre。
- 现在使用 selenium IDE 记录测试用例。
- 将测试用例导出到 php 文件。
- 使用这些导出的函数创建一个测试用例库。
- 创建从库中调用函数/测试的套件。
- 现在使用 java 命令执行启动 Selenium 服务器。
- 使用 phpunit 执行套件。
for refrence how to write these files click hereand also try on git hub
有关如何编写这些文件的参考,请单击此处并尝试使用 git hub
回答by Anil
facebook/php-webdriveris an awesome client for selenium and php.
facebook/php-webdriver是一个很棒的 selenium 和 php 客户端。
You can use it to automate web tasks (as the OP wanted), or you can simply integrate php-webdriver to your testing framework. There are some project already providing this:
您可以使用它来自动化 Web 任务(如 OP 所希望的那样),或者您可以简单地将 php-webdriver 集成到您的测试框架中。有一些项目已经提供了这个:
- Stewardintegrates php-webdriver directly to PHPUnit.
- Codeceptiontesting framework provides BDD-layer on top of php-webdriver.
- You can also check out this blogpost+ demo project, describing custom PHPUnitintegration.
- Steward将 php-webdriver 直接集成到PHPUnit。
- Codeception测试框架在 php-webdriver 之上提供了 BDD 层。
- 您还可以查看此博客文章+演示项目,描述自定义PHPUnit集成。
Install Everything
安装一切
Download and install facebook/php-webdriver.
composer require facebook/webdriver
Download Selenium& Start it.
java -jar selenium-server-standalone-#.jar
Download Quick Javaand place it into your project directory.
下载并安装facebook/php-webdriver。
composer require facebook/webdriver
下载 Selenium并启动它。
java -jar selenium-server-standalone-#.jar
下载 Quick Java并将其放入您的项目目录中。
Usage
用法
In this example, we use the extension quickjava
to disable everything except javascript
and cookies
.
在这个例子中,我们使用扩展quickjava
来禁用除javascript
和之外的所有内容cookies
。
View more preference settings here:
https://github.com/ThatOneGuyDotNet/QuickJava/blob/master/defaults/preferences/defaults.js
在此处查看更多首选项设置:https:
//github.com/ThatOneGuyDotNet/QuickJava/blob/master/defaults/preferences/defaults.js
View more example commands here:
https://github.com/facebook/php-webdriver/wiki/Example-command-reference
在此处查看更多示例命令:https:
//github.com/facebook/php-webdriver/wiki/Example-command-reference
use Facebook\WebDriver\Firefox\FirefoxProfile;
use Facebook\WebDriver\Firefox\FirefoxDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
// Change this to the path of you xpi
$extensionPath = $this->container->getParameter('kernel.root_dir').'/../bin/selenium/quickjava-2.0.6-fx.xpi';
// Build our firefox profile
$profile = new FirefoxProfile();
$profile->addExtension($extensionPath);
$profile->setPreference('thatoneguydotnet.QuickJava.curVersion', '2.0.6.1');
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Images', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.AnimatedImage', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.CSS', 2);
//$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Cookies', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Flash', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Java', 2);
//$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.JavaScript', 2);
$profile->setPreference("thatoneguydotnet.QuickJava.startupStatus.Silverlight", 2);
// Create DC + Driver
$dc = DesiredCapabilities::firefox();
$dc->setCapability(FirefoxDriver::PROFILE, $profile);
$driver = RemoteWebDriver::create($host, $dc);
$driver->get('http://stackoverflow.com');
// Do stuff - https://github.com/facebook/php-webdriver/wiki/Example-command-reference
//$driver->findElement(WebDriverBy::id("element-id"));
// The HTML Source code
$html = $driver->getPageSource();
// Firefox should be open and you can see no images or css was loaded
回答by markdrake
You need the selenium server running and a web driver library to interact with it.
您需要运行 selenium 服务器和一个 web 驱动程序库来与之交互。
Officially selenium has no support for PHP but in Nearsoft we created a library to interact with the Json Wire Protocol. We aimed to make it as similar as possible to the examples from other languages and drivers from the official site so an example from the page in Java would have a very similar syntax in php.
正式 selenium 不支持 PHP,但在 Nearsoft 中,我们创建了一个库来与 Json Wire 协议交互。我们的目标是使其尽可能与来自官方网站的其他语言和驱动程序的示例相似,因此来自 Java 页面的示例在 php 中的语法非常相似。
Check it out: https://github.com/Nearsoft/PHP-SeleniumClient
看看:https: //github.com/Nearsoft/PHP-SeleniumClient
If you like it, share it, get involved, fork it or do as you please.
如果你喜欢它,分享它,参与其中,分叉它或随心所欲。
Regards, Mark.
问候,马克。
回答by Nadir
I think the guy asked mainly how to use IDE generated files.
我想这家伙问的主要是如何使用IDE生成的文件。
There is a formater for PHP: You then just have to export as PHPunit.
PHP 有一个格式化程序:然后您只需导出为 PHPunit。
Selenium IDE: PHP Formatters :: Add-ons for Firefox https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-php-formatters/
Selenium IDE:PHP 格式化程序 :: Firefox 附加组件 https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-php-formatters/