Screru is a library that supplements php-webdriver
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
src
tests
.coveralls.yml
.env.example
.gitignore
.travis.yml
LICENSE
README.md
bootstrap.php
composer.json
kill_selenium.sh
phpunit.xml
start_selenium.sh

README.md

Screru

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

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

Demo

demo

Requirements

Installation

$ composer require shimabox/screru
$ cd screru
$ cp .env.example .env

or

$ git clone https://github.com/shimabox/screru.git
$ cd screru
$ composer install
$ cp .env.example .env

### macOS

  • Operation confirmed in macOS High Sierra 10.13

selenium-server-standalone

geckodriver

$ tar -zxvf geckodriver-v0.21.0-macos.tar.gz
$ mv geckodriver /usr/local/bin/
$ chmod +x /usr/local/bin/geckodriver

chromedriver

$ unzip chromedriver_mac64.zip
$ mv chromedriver /usr/local/bin/
$ chmod +x /usr/local/bin/chromedriver

.env

  • Edit .env
ENABLED_FIREFOX_DRIVER=true
ENABLED_CHROME_DRIVER=true

Run

  1. Run selenium-server-standalone
$ java -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false
  1. Run phpunit
$ vendor/bin/phpunit

### windows(64bit)

selenium-server-standalone

geckodriver.exe

chromedriver.exe

IEDriverServer.exe

.env

  • Edit .env
ENABLED_FIREFOX_DRIVER=true
ENABLED_CHROME_DRIVER=true
ENABLED_IE_DRIVER=true
// true to platform is windows
IS_PLATFORM_WINDOWS=true
// webdriver path for IE
FIREFOX_DRIVER_PATH='your geckodriver.exe path'
CHROME_DRIVER_PATH='your chromedriver.exe path'
IE_DRIVER_PATH='your IEDriverServer.exe path'

Run

  1. Open cmd etc.
  2. Run selenium-server-standalone
$ java -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false
  1. Open a new cmd etc.
  2. Run phpunit
$ vendor/bin/phpunit

### Linux

  • Operation confirmed in 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.2.0
$ firefox -v
Mozilla Firefox 60.2.0

Xvfb

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

selenium-server-standalone

geckodriver

$ tar -zxvf geckodriver-v0.21.0-linux64.tar.gz
$ sudo mv geckodriver /usr/local/bin/
$ sudo chmod +x /usr/local/bin/geckodriver

.env

  • Edit .env
ENABLED_FIREFOX_DRIVER=true

Run

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

Usage

  • Basic Usage
require_once '/vendor/autoload.php';

use SMB\Screru\Factory\DesiredCapabilities;
use SMB\UrlStatus;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\WebDriverBrowserType;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\WebDriverBy;

// Create firefox webdriver
$host = getenv('SELENIUM_SERVER_URL');
$cap = new DesiredCapabilities(WebDriverBrowserType::FIREFOX);
$driver = RemoteWebDriver::create($host, $cap->get());

$url = 'https://www.google.com/webhp?gl=us&hl=en&gws_rd=cr';

// 指定URLへ遷移 (Google)
$driver->get($url);

// 検索Box
$findElement = $driver->findElement(WebDriverBy::name('q'));
// 検索Boxにキーワードを入力して
$findElement->sendKeys('Hello');
// 検索実行
$findElement->submit();

// 検索結果画面のタイトルが 'Hello - Google Search' になるまで10秒間待機する
// 指定したタイトルにならずに10秒以上経ったら
// 'Facebook\WebDriver\Exception\TimeOutException' がthrowされる
$driver->wait(10)->until(
    WebDriverExpectedCondition::titleIs('Hello - Google Search')
);

// Hello - Google Search というタイトルが取得できることを確認する
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');
}

// close window
$driver->close();
  • When you want to change the default selenium server url
$ 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 ...
      }

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/example_1.php
  • $ php example/example_2.php
    • For headless chrome.
  • $ php example/example_3.php
    • For headless firefox.

Testing

Start PHP's built-in Web Server

  • It is necessary for functional test
$ php -S 127.0.0.1:8000 -t tests/functional/web

Run

$ vendor/bin/phpunit
  • To exclude functional tests
$ vendor/bin/phpunit --exclude-group functional

License

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