facebook/php-webdriver是一个很棒的 selenium 和 php 客户端。
您可以使用它来自动化 Web 任务(正如 OP 所希望的那样),或者您可以简单地将 php-webdriver 集成到您的测试框架中。有一些项目已经提供了这个:
安装一切
下载并安装facebook/php-webdriver。composer require facebook/webdriver
下载 Selenium并启动它。java -jar selenium-server-standalone-#.jar
下载 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