SeleniumTestCase.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\test\lib;
12 
14 
15 use Facebook\WebDriver\WebDriverBy;
16 use Facebook\WebDriver\WebDriverDimension;
17 use Facebook\WebDriver\WebDriverExpectedCondition;
18 use Facebook\WebDriver\Remote\DesiredCapabilities;
19 use Facebook\WebDriver\Remote\RemoteWebDriver;
20 
21 if (!class_exists('\Facebook\WebDriver\Remote\RemoteWebDriver')) {
22  throw new ConfigurationException(
23  'SeleniumTestCase requires Facebook\WebDriver. '.
24  'If you are using composer, add facebook/webdriver '.
25  'as dependency to your project');
26 }
27 
28 /**
29  * SeleniumTestCase is the base class for test cases that run with Selenium.
30  *
31  * @author ingo herwig <ingo@wemove.com>
32  */
33 abstract class SeleniumTestCase extends DatabaseTestCase {
34  use TestTrait;
35 
36  /**
37  * @see https://getbootstrap.com/docs/4.1/layout/overview/#responsive-breakpoints
38  */
39  private static $displayWidths = [
40  /* Small devices (landscape phones, 576px and up) */
41  'small' => 576,
42  /* Medium devices (tablets, 768px and up) */
43  'medium' => 768,
44  /* Large devices (desktops, 992px and up) */
45  'large' => 992,
46  /* Extra large devices (large desktops, 1200px and up) */
47  'xlarge' => 1200,
48  ];
49  private static $height = 768;
50 
51  protected $driver = null;
52 
53  protected static function getAppUrl() {
54  if (!defined('TEST_SERVER')) {
55  throw new \RuntimeException("Constant TEST_SERVER not defined, e.g. define(TEST_SERVER, 'localhost:8500')");
56  }
57  return "http://".TEST_SERVER;
58  }
59 
60  protected static function getSeleniumUrl() {
61  if (!defined('SELENIUM_SERVER')) {
62  throw new \RuntimeException("Constant SELENIUM_SERVER not defined, e.g. define(SELENIUM_SERVER, 'localhost:8001')");
63  }
64  return "http://".SELENIUM_SERVER;
65  }
66 
67  protected function takeScreenShot($filename) {
68  $screenshot = $this->driver->takeScreenshot();
69  file_put_contents('log/'.$filename.'.png', $screenshot);
70  }
71 
72  public function setUp() {
73  $this->driver = RemoteWebDriver::create(self::getSeleniumUrl(), DesiredCapabilities::phantomjs());
74  $this->setDisplay('large');
75  parent::setUp();
76  }
77 
78  protected function setDisplay($size) {
79  if (isset(self::$displayWidths[$size])) {
80  $this->setWindowSize(self::$displayWidths[$size], self::$height);
81  }
82  }
83 
84  protected function setWindowSize($width, $height) {
85  $this->driver->manage()->window()->setSize(new WebDriverDimension($width, $height));
86  }
87 
88  /**
89  * Wait
90  * @param $ms
91  */
92  protected function wait($seconds) {
93  $this->driver->manage()->timeouts()->implicitlyWait($seconds);
94  }
95 
96  /**
97  * Get the DOM element matching the given xpath
98  * @param $xpath The xpath
99  * @return element|false
100  */
101  protected function byXpath($xpath) {
102  return $this->driver->findElement(WebDriverBy::xpath($xpath));
103  }
104 
105  /**
106  * Log into the application
107  * @param $user The username
108  * @param $password The password
109  */
110  protected function login($user, $password) {
111  $this->driver->get(self::getAppUrl());
112  $this->driver->wait(10, 1000)->until(
113  WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::name('user'))
114  );
115  $this->takeScreenShot('login');
116  $this->driver->findElement(WebDriverBy::name('user'))->sendKeys($user);
117  $this->driver->findElement(WebDriverBy::name('password'))->sendKeys($password);
118  $this->byXpath("//span[contains(text(),'Sign in')]")->click();
119  }
120 }
121 ?>
byXpath($xpath)
Get the DOM element matching the given xpath.
login($user, $password)
Log into the application.
ConfigurationException signals an exception in the configuration.
Test support classes.
Definition: namespaces.php:100
DatabaseTestCase is the base class for test cases that need database support.
SeleniumTestCase is the base class for test cases that run with Selenium.