PHP code example of shimabox / screru

1. Go to this page and download the library: Download shimabox/screru library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shimabox / screru example snippets



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();

  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 ...
  }
  

    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();