shimabox / screru
具有补充php-webdriver的功能
v0.7.4
2020-06-06 03:10 UTC
Requires
- php: ^5.6 || ~7.0
- facebook/webdriver: ^1.7
- shimabox/selenium-downloader: ~0.3
- shimabox/url-status: ^1.0
- vlucas/phpdotenv: ^2.4
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- mobiledetect/mobiledetectlib: ^2.8
- phpunit/phpunit: ^4.8 || ~5.0
README
Screru 是一个补充 php-webdriver 的库
描述
Screru 是一个补充 php-webdriver 的库。它提供了以下功能。
- 提供 PHPUnit 的 trait
- 全屏截图
- 断言失败时可以捕获
- 元素截图
- 在屏幕截图滚动时可以控制元素的样式。
支持 Firefox (WebDriverBrowserType::FIREFOX),Chrome (WebDriverBrowserType::CHROME) 和 IE (WebDriverBrowserType::IE)。
演示
要求
- PHP 5.6+ 或更高版本
- Composer
- Java(JDK) >=1.8
安装
通过 composer。
$ composer require shimabox/screru
$ cd vendor/shimabox/screru
$ cp .env.default .env
开发
$ git clone https://github.com/shimabox/screru.git
$ cd screru
$ composer install
- 复制
.env.default
文件并创建一个.env
文件。
设置 (.env | .env.default)
如果您需要更改默认设置,请复制 .env.default
文件,创建一个 .env
文件,并修改 .env
文件。
默认设置查看 .env.default
文件。
$ vim .env
// selenium server url
SELENIUM_SERVER_URL='https://:4444/wd/hub'
// you can override the default User-agent
OVERRIDE_DEFAULT_USER_AGENT=''
// local port
LOCAL_PORT=8000
// true to enable
ENABLED_CHROME_DRIVER=true
ENABLED_FIREFOX_DRIVER=true
ENABLED_IE_DRIVER=
// true to start headless chrome
ENABLED_CHROME_HEADLESS=true
// true to start headless firefox
ENABLED_FIREFOX_HEADLESS=true
// true to platform is windows
IS_PLATFORM_WINDOWS=
// describe the webdriver path if necessary
CHROME_DRIVER_PATH=''
FIREFOX_DRIVER_PATH=''
IE_DRIVER_PATH=''
准备
下载 selenium-server-standalone, ChromeDriver, geckodriver, IEDriverServer 等。
使用下载器。
- 例如)对于 Mac。
$ php selenium_downloader.php -p m -d . -s 3.8.1 -c 81.0.4044.69 -g 0.26.0
- 例如)对于 Windows。
$ php selenium_downloader.php -p w -d . -s 3.8.1 -c 75.0.3770.90 -g 0.24.0 -i 3.141.59
- 例如)对于 Linux。
$ php selenium_downloader.php -p l -d . -s 3.8.1 -g 0.24.0
@see selenium-downloader/README.md at master · shimabox/selenium-downloader · GitHub
## macOS
- 将 ChromeDriver 和 geckodriver 的路径添加到您的 Path 环境变量中。
- 例如)ChromeDriver
$ mv chromedriver /usr/local/bin/ $ chmod +x /usr/local/bin/chromedriver
- 例如)geckodriver
$ mv geckodriver /usr/local/bin/ $ chmod +x /usr/local/bin/geckodriver
- 或者,在
.env
文件中写入驱动程序的路径
请给予执行权限 (chmod +x
)
例如)
CHROME_DRIVER_PATH=/Applications/MAMP/htdocs/screru/chromedriver
FIREFOX_DRIVER_PATH=/Applications/MAMP/htdocs/screru/geckodriver
运行 selenium-server-standalone。
$ java -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false
## windows(64位)
.env
- 编辑
.env
ENABLED_FIREFOX_DRIVER=true
ENABLED_CHROME_DRIVER=true
ENABLED_IE_DRIVER=true
// true to platform is windows
IS_PLATFORM_WINDOWS=true
// describe the webdriver path if necessary
FIREFOX_DRIVER_PATH='your geckodriver.exe path'
CHROME_DRIVER_PATH='your chromedriver.exe path'
IE_DRIVER_PATH='your IEDriverServer.exe path'
必须将 IS_PLATFORM_WINDOWS
的值设置为 true
。
运行 selenium-server-standalone。
$ java -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false
注意。
Facebook\WebDriver\Exception\SessionNotCreatedException: Unexpected error launching Internet Explorer.
Protected Mode settings are not the same for all zones.
Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.
当显示此错误时,请参阅以下链接。
通过将 IE 互联网选项的安全模式在所有区域设置为 ON 来解决。
## Linux (CentOS 6.9)
java
- 安装
$ sudo yum -y install java
- 版本 1.8>=
$ java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
Firefox
- 安装
$ sudo yum -y install firefox
或
$ sudo yum -y update firefox
- 版本 60.3.0
$ firefox -v
Mozilla Firefox 60.3.0
Xvfb
- 安装
$ sudo yum -y install xorg-x11-server-Xvfb
$ sudo yum -y groupinstall "Japanese Support"
路径
- 将 geckodriver 的路径添加到您的 Path 环境变量中。
$ mv geckodriver /usr/local/bin/
$ chmod +x /usr/local/bin/geckodriver
- 或者,在
.env
文件中写入驱动程序的路径
请给予执行权限 (chmod +x
)
例如)
FIREFOX_DRIVER_PATH=/home/user/screru/geckodriver
.env
- 编辑
.env
ENABLED_FIREFOX_DRIVER=true
ENABLED_CHROME_DRIVER=
ENABLED_IE_DRIVER=
运行 selenium-server-standalone。
- 运行 Xvfb & selenium-server-standalone
$ sudo sh start_selenium.sh
- 停止 Xvfb & selenium-server-standalone & geckodriver
$ sudo sh kill_selenium.sh
## Linux (Ubuntu trusty)
请参阅此设置。
使用方法
- 基本用法。
<?php require_once '/vendor/autoload.php'; use SMB\Screru\Elements\Spec; use SMB\Screru\Elements\SpecPool; use SMB\Screru\Factory\DesiredCapabilities; use SMB\Screru\Screenshot\Screenshot; use SMB\Screru\View\Observer; use SMB\UrlStatus; use Facebook\WebDriver\WebDriverBy; use Facebook\WebDriver\WebDriverDimension; use Facebook\WebDriver\WebDriverExpectedCondition; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\Remote\WebDriverBrowserType; if (getenv('ENABLED_CHROME_DRIVER') !== 'true') { die('Please enable ChromeDriver.'); } $host = getenv('SELENIUM_SERVER_URL'); // Use chromedriver. $cap = new DesiredCapabilities(WebDriverBrowserType::CHROME); $driver = RemoteWebDriver::create($host, $cap->get()); // Window size. $w = 600; $h = 800; $dimension = new WebDriverDimension($w, $h); $driver->manage()->window()->setSize($dimension); $url = 'https://www.google.com/webhp?gl=us&hl=en&gws_rd=cr'; // Transit to designated URL (Google). $driver->get($url); // Search for elements. $findElement = $driver->findElement(WebDriverBy::name('q')); // Enter keywords in search box. $findElement->sendKeys('Hello'); // Search execution. $findElement->submit(); // Wait 10 seconds for the contents to be visualized(Targeting '#botstuff'). // If the specified element does not appear and it takes more than 10 seconds, // 'Facebook\WebDriver\Exception\TimeOutException' is thrown. $driver->wait(10)->until( WebDriverExpectedCondition::visibilityOfElementLocated(WebDriverBy::id('botstuff')) ); // Confirm that the title "Hello - Google search" can be obtained if ($driver->getTitle() !== 'Hello - Google Search') { throw new Exception('fail $driver->getTitle()'); } // HttpStatus of url $status = UrlStatus::get($driver->getCurrentURL()); if ($status->is200() === false) { throw new Exception('fail HttpStatus'); } /* |------------------------------------------------------------------------------ | Capture test. |------------------------------------------------------------------------------ */ $fileName = 'capture_demo'; $ds = DIRECTORY_SEPARATOR; $captureDirectoryPath = realpath(__DIR__ . $ds . 'capture') . $ds; // Create a Screenshot. $screenshot = new Screenshot(); /* |------------------------------------------------------------------------------ | Create an Observer if you want to control the style of the element when scrolling the screen. |------------------------------------------------------------------------------ */ // Create a Observer. $observer = new Observer(); // Erase the following header (sticky header) when vertical scrolling is performed for the first time. $observer->processForFirstVerticalScroll(function($driver) { $driver->executeScript("document.querySelector('#searchform') ? document.querySelector('#searchform').style.display = 'none' : null;"); }); // Undo when rendering is complete. $observer->processForRenderComplete(function($driver) { $driver->executeScript("document.querySelector('#searchform') ? document.querySelector('#searchform').style.display = 'inherit' : null;"); }); // Set Observer to Screenshot. $screenshot->setObserver($observer); // Full screen capture (extension will be .png). $screenshot->takeFull($driver, $captureDirectoryPath, $fileName . '_full.png'); // Define element selector. $spec = new Spec('.RNNXgb', Spec::EQUAL, 1); $spec2 = new Spec('.brs_col', Spec::GREATER_THAN, 1); // Push into SpecPool. $specPool = (new SpecPool()) ->addSpec($spec) ->addSpec($spec2); // Element capture (extension is .png). $screenshot->takeElement($driver, $captureDirectoryPath, $fileName, $specPool); // Close window. $driver->close();
-
当滚动屏幕时控制元素的样式。
-
当您想更改默认 selenium 服务器 URL 时。
$ vim .env
// selenium server url
SELENIUM_SERVER_URL='your selenium server url'
- 当您想更改默认 UserAgent 时。
$ vim .env
// you can override the default User-agent (Android 7.1.1)
OVERRIDE_DEFAULT_USER_AGENT='Mozilla/5.0 (Linux; Android 7.1.1; Nexus 5X Build/N4F26I) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36'
-
与 PHPUnit 一起使用时。
use \SMB\Screru\Traits\Testable
class Sample extends \PHPUnit_Framework_TestCase { // use Trait use \SMB\Screru\Traits\Testable { setUp as protected traitSetUp; tearDown as protected traitTearDown; } /** * setUp */ protected function setUp() { $this->traitSetUp(); } /** * tearDown */ protected function tearDown() { $this->traitTearDown(); } // do someting ... }
- 如果您想在断言失败时捕获。
takeCaptureWhenAssertionFails = true;
class Sample extends \PHPUnit_Framework_TestCase { // use Trait use \SMB\Screru\Traits\Testable { setUp as protected traitSetUp; tearDown as protected traitTearDown; } // Set this property to true protected $takeCaptureWhenAssertionFails = true; /** * setUp */ protected function setUp() { $this->traitSetUp(); } /** * tearDown */ protected function tearDown() { $this->traitTearDown(); } // do someting ... }
- 或调用以下函数。
$this->enableCaptureWhenAssertionFails(); // To disable, call the following function $this->disableCaptureWhenAssertionFails();
无头 Chrome
对于最新的 chrome,您可以使用无头模式。
- 编辑
.env
// true to start headless chrome
ENABLED_CHROME_HEADLESS=true
无头 Firefox
对于最新的Firefox,您可以使用无头模式。
- 编辑
.env
// true to start headless firefox
ENABLED_FIREFOX_HEADLESS=true
示例
$ php example/assertion_of_title.php
- example/assertion_of_title.php
- 执行标题断言。
- example/assertion_of_title.php
$ php example/example.php
- example/example.php
- 转到指定的URL(Google)。
- 执行全屏捕获。
- 执行屏幕元素捕获。
- example/example.php
$ php example/headless_chrome.php
- example/headless_chrome.php
- 转到指定的URL(Google)。
- 执行全屏捕获。
- example/headless_chrome.php
$ php example/headless_firefox.php
- example/headless_firefox.php
- 转到指定的URL(Google)。
- 执行全屏捕获。
- example/headless_firefox.php
$ php example/screenshot.php
- example/screenshot.php
- 转到指定的URL(Google)。
- 执行全屏捕获。
- 执行屏幕捕获。
- example/screenshot.php
$ php example/element_screenshot.php
- example/element_screenshot.php
- 转到指定的URL(Google)。
- 执行屏幕元素捕获。
- example/element_screenshot.php
$ php example/controll_display_state_of_elements.php
- example/controll_display_state_of_elements.php
- 转到指定的URL(Google)。
- 执行全屏捕获。
- 当滚动屏幕时控制元素的样式。
- 执行屏幕元素捕获。
- example/controll_display_state_of_elements.php
测试
启动PHP内置的Web服务器。
$ php -S 127.0.0.1:8000 -t tests/web
与.env
中的LOCAL_PORT
匹配端口。
运行测试。
$ vendor/bin/phpunit
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。