定制 shimabox/screru 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shimabox/screru

Composer 安装命令:

composer require shimabox/screru

包简介

It has a function to supplement php-webdriver

README 文档

README

Screru is a library that supplements php-webdriver

License Build Status Maintainability Coverage Status Latest Stable Version Latest Unstable Version

Description

Screru is a library that supplements php-webdriver. It provides the following functions.

  • Trait for PHPUnit is available
  • Full screenshot
    • Capture can be done when the assertion fails
  • Screenshot of element
  • Can control the style of the element when scrolling the screen in screen capture.

Supports Firefox (WebDriverBrowserType::FIREFOX), Chrome (WebDriverBrowserType::CHROME) and IE (WebDriverBrowserType::IE).

Demo

demo

Requirements

Installation

Via composer.

$ composer require shimabox/screru
$ cd vendor/shimabox/screru
$ cp .env.default .env

Develop.

$ git clone https://github.com/shimabox/screru.git
$ cd screru
$ composer install
  • Copy the .env.default file and create an .env file.

Setting (.env | .env.default)

If you need to change the default settings, copy the .env.default file, create an .env file, and modify the .env file.
The default setting looks at .env.default file.

$ vim .env

// selenium server url
SELENIUM_SERVER_URL='http://localhost: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=''
Key Description Default Example
SELENIUM_SERVER_URL selenium server url. http://localhost:4444/wd/hub
OVERRIDE_DEFAULT_USER_AGENT you can override the default User-agent. Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1
LOCAL_PORT local port
It is for unittest.
8000
ENABLED_CHROME_DRIVER If true it will enable ChromeDriver. true
ENABLED_FIREFOX_DRIVER If true it will enable geckodriver. true
ENABLED_IE_DRIVER If true it will enable IEDriverServer. blank(false)
ENABLED_CHROME_HEADLESS true to start headless chrome. true
ENABLED_FIREFOX_HEADLESS true to start headless firefox. true
IS_PLATFORM_WINDOWS true to platform is windows.
For windows, be sure to set to true.
blank(false)
CHROME_DRIVER_PATH Describe the webdriver path if necessary. blank /home/user/screru/chromedriver
FIREFOX_DRIVER_PATH Describe the webdriver path if necessary. blank /Applications/MAMP/htdocs/screru/geckodriver
IE_DRIVER_PATH Describe the webdriver path if necessary. blank /c/xampp/htdocs/screru/IEDriverServer.exe

Preparation

Download selenium-server-standalone, ChromeDriver, geckodriver, IEDriverServer etc.

Platform selenium-server-standalone ChromeDriver geckodriver IEDriverServer
Mac 3.8.1 81.0.4044.69 0.26.0 -
Windows(64bit) 3.8.1 75.0.3770.90 0.24.0 3.141.59
Linux(CentOS 6.9) 3.8.1 - 0.24.0 -
Linux(Ubuntu trusty) 3.8.1 75.0.3770.90 0.24.0 -

Use downloader.

  • e.g) For Mac.
$ php selenium_downloader.php -p m -d . -s 3.8.1 -c 81.0.4044.69 -g 0.26.0
  • e.g) For 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
  • e.g) For 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

  • Add the path to ChromeDriver and geckodriver to your Path environment variable.
    • e.g) ChromeDriver
    $ mv chromedriver /usr/local/bin/
    $ chmod +x /usr/local/bin/chromedriver
    
    • e.g) geckodriver
    $ mv geckodriver /usr/local/bin/
    $ chmod +x /usr/local/bin/geckodriver
    
  • Or, write the driver's path in the .env file
    Please give execute permission (chmod +x)

e.g)

CHROME_DRIVER_PATH=/Applications/MAMP/htdocs/screru/chromedriver
FIREFOX_DRIVER_PATH=/Applications/MAMP/htdocs/screru/geckodriver

Run selenium-server-standalone.

$ java -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false

## windows(64bit)

.env

  • Edit .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'

The value of IS_PLATFORM_WINDOWS must be set to true.

Run selenium-server-standalone.

$ java -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false

Note.

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.

When this error is displayed, please refer to the following link.

It is solved by setting the security mode of IE Internet option to ON in all zones.

## Linux (CentOS 6.9)

java

  • install
$ sudo yum -y install java
  • version 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

  • install
$ sudo yum -y install firefox

or

$ sudo yum -y update firefox
  • version 60.3.0
$ firefox -v
Mozilla Firefox 60.3.0

Xvfb

  • install
$ sudo yum -y install xorg-x11-server-Xvfb
$ sudo yum -y groupinstall "Japanese Support"

Path

  • Add the path to geckodriver to your Path environment variable.
$ mv geckodriver /usr/local/bin/
$ chmod +x /usr/local/bin/geckodriver
  • Or, write the driver's path in the .env file
    Please give execute permission (chmod +x)

e.g)

FIREFOX_DRIVER_PATH=/home/user/screru/geckodriver

.env

  • Edit .env
ENABLED_FIREFOX_DRIVER=true
ENABLED_CHROME_DRIVER=
ENABLED_IE_DRIVER=

Run selenium-server-standalone.

  1. Run Xvfb & selenium-server-standalone
$ sudo sh start_selenium.sh
  1. Stop Xvfb & selenium-server-standalone & geckodriver
$ sudo sh kill_selenium.sh

## Linux (Ubuntu trusty)

Please refer to this setting.

Usage

  • Basic Usage.
<?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();
$ vim .env

// selenium server url
SELENIUM_SERVER_URL='your selenium server url'
  • When you want to change the default 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'
  • When using with 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 ...
    }
    • If you want to capture when an assertion fails.
      • 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 ...
      }
      • or call the function below.
      $this->enableCaptureWhenAssertionFails();
      
      // To disable, call the following function
      $this->disableCaptureWhenAssertionFails();

Headless Chrome

For the latest chrome, you can use headless mode.

  • Edit .env
// true to start headless chrome
ENABLED_CHROME_HEADLESS=true

Headless Firefox

For the latest firefox, you can use headless mode.

  • Edit .env
// true to start headless firefox
ENABLED_FIREFOX_HEADLESS=true

Example

  • $ php example/assertion_of_title.php
  • $ php example/example.php
    • example/example.php
      1. Transit to designated URL (Google).
      2. Perform fullscreen capture.
      3. Perform screen element capture.
  • $ php example/headless_chrome.php
  • $ php example/headless_firefox.php
  • $ php example/screenshot.php
    • example/screenshot.php
      1. Transit to designated URL (Google).
      2. Perform fullscreen capture.
      3. Perform screen capture.
  • $ php example/element_screenshot.php
  • $ php example/controll_display_state_of_elements.php

Testing

Start PHP's built-in Web Server.

$ php -S 127.0.0.1:8000 -t tests/web

Match port with .env - LOCAL_PORT.

Run test.

$ vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

shimabox/screru 适用场景与选型建议

shimabox/screru 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 71 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「phpunit」 「selenium」 「screenshot」 「php-webdriver」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 shimabox/screru 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 shimabox/screru 我们能提供哪些服务?
定制开发 / 二次开发

基于 shimabox/screru 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 71
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-05