如何在PHP中使用Selenium?

Sta*_*bie 14 php selenium web-testing headless-browser

我想使用Selenium自动执行一些Web任务(不用于测试).我认为我安装了Selenium RC Server,但由于我在PHP中找不到客户端驱动程序,因此无法编写"测试脚本"(参见:http://seleniumhq.org/download/).

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

Ani*_*nil 17

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

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


安装一切

  1. 下载并安装facebook/php-webdriver.composer 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
Run Code Online (Sandbox Code Playgroud)


lAH*_*2iV 15

尝试下面的事情

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

为了参考如何写这些文件点击这里,并尝试使用git hub

  • [php-webdriver](https://github.com/facebook/php-webdriver)库就足够了,使用[Steward]等工具可以更轻松地进行设置(https://github.com/ LMC-EU /管家).我建议不要浪费时间使用Selenium IDE,只需在PHP中编写所需的场景即可. (2认同)