18

我想使用 Selenium 来自动化一些网络任务(不用于测试)。我想我已经安装了 Selenium RC 服务器,但是因为我在 PHP 中找不到客户端驱动程序,所以无法编写“测试脚本”(参见:http ://seleniumhq.org/download/ )。

有没有办法让我将 Selenium 与 PHP 一起使用?这似乎表明我需要 PHPUnit http://www.phpunit.de/manual/current/en/selenium.html。我只想自动化一些任务,而不是参与一整套测试。

4

4 回答 4

22

facebook/php-webdriver是一个很棒的 selenium 和 php 客户端。

您可以使用它来自动化 Web 任务(正如 OP 所希望的那样),或者您可以简单地将 php-webdriver 集成到您的测试框架中。有一些项目已经提供了这个:


安装一切

  1. 下载并安装facebook/php-webdrivercomposer require facebook/webdriver

  2. 下载 Selenium并启动它。java -jar selenium-server-standalone-#.jar

  3. 下载 Quick Java并将其放入您的项目目录中。


用法

在此示例中,我们使用扩展quickjava来禁用除javascript和之外的所有内容cookies

在此处查看更多首选项设置:
https ://github.com/ThatOneGuyDotNet/QuickJava/blob/master/defaults/preferences/defaults.js

在此处查看更多示例命令:
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
于 2015-11-11T21:09:33.463 回答
19

尝试关注事物

  1. 安装并运行 Phpunit
  2. 你的电脑上也有 JAVA sdk & jre。
  3. 现在使用 selenium IDE 记录测试用例。
  4. 将测试用例导出到 php 文件。
  5. 使用这些导出的函数创建一个测试用例库。
  6. 创建从库中调用函数/测试的套件。
  7. 现在使用 java 命令执行 Start Selenium Server。
  8. 使用 phpunit 执行套件。

有关如何编写这些文件的参考,请单击此处并在 git hub 上尝试

于 2011-07-07T18:58:48.437 回答
1

You need the selenium server running and a web driver library to interact with it.

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.

Check it out: https://github.com/Nearsoft/PHP-SeleniumClient

If you like it, share it, get involved, fork it or do as you please.

Regards, Mark.

于 2012-05-31T18:58:43.430 回答
1

我想这家伙主要是问如何使用 IDE 生成的文件。

PHP 有一个格式化程序:然后您只需导出为 PHPunit。

Selenium IDE:PHP 格式化程序 :: Firefox 附加组件 https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-php-formatters/

于 2016-08-29T16:02:57.640 回答