SeleniumTestCase.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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 /**
16  * SeleniumTestCase is the base class for test cases that run with Selenium.
17  *
18  * @author ingo herwig <ingo@wemove.com>
19  */
20 abstract class SeleniumTestCase extends \PHPUnit_Extensions_Selenium2TestCase {
21 
22  /**
23  * @see http://getbootstrap.com/css/#grid-media-queries
24  */
25  private $displayWidths = array(
26  /* Extra small devices (phones, less than 768px) */
27  'xsmall' => 480,
28  /* Small devices (tablets, 768px and up) */
29  'small' => 768,
30  /* Medium devices (desktops, 992px and up) */
31  'medium' => 992,
32  /* Large devices (large desktops, 1200px and up) */
33  'large' => 1200,
34  );
35  private $width = 1024;
36 
37  private $databaseTester;
38 
39  protected static function getAppUrl() {
40  return "http://".SERVER_HOST.":".SERVER_PORT;
41  }
42 
43  protected function setUp() {
44  // framework setup
45  TestUtil::initFramework(WCMF_BASE.'app/config/');
46 
47  // database setup
48  $params = TestUtil::createDatabase();
49  $conn = new \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($params['connection'], $params['dbName']);
50  $this->databaseTester = new \PHPUnit_Extensions_Database_DefaultTester($conn);
51  $this->databaseTester->setSetUpOperation(\PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT());
52  $this->databaseTester->setTearDownOperation(\PHPUnit_Extensions_Database_Operation_Factory::NONE());
53  $this->databaseTester->setDataSet($this->getDataSet());
54  $this->databaseTester->onSetUp();
55 
56  // selenium setup
57  $this->setBrowser('firefox');
58  $this->setBrowserUrl(self::getAppUrl());
59  parent::setUp();
60  }
61 
62  public function tearDown() {
63  if ($this->databaseTester) {
64  $this->databaseTester->onTearDown();
65  $this->databaseTester = NULL;
66  }
67  parent::tearDown();
68  }
69 
70  public function setUpPage() {
71  parent::setUpPage();
72 
73  // get window object
74  $window = $this->currentWindow();
75 
76  // set window size
77  $window->size(array(
78  'width' => $this->width,
79  'height' => 768)
80  );
81  }
82 
83  protected function setDisplay($size) {
84  if (isset($this->displayWidths[$size])) {
85  $this->width = $this->displayWidths[$size];
86  $this->setUpPage();
87  }
88  }
89 
90  /**
91  * Wait for a DOM element matching the given xpath
92  * @param $xpath The xpath
93  * @param $wait maximum (in seconds)
94  * @return element|false false on time-out
95  */
96  protected function waitForXpath($xpath, $wait=30) {
97  for ($i=0; $i <= $wait; $i++) {
98  try {
99  $x = $this->byXPath($xpath);
100  return $x;
101  }
102  catch (\Exception $e) {
103  sleep(1);
104  }
105  }
106  return false;
107  }
108 
109  /**
110  * Log into the application
111  * @param $user The username
112  * @param $password The password
113  */
114  protected function login($user, $password) {
115  $this->url(self::getAppUrl());
116  $this->timeouts()->implicitWait(5000);
117  $this->byName('user')->value($user);
118  $this->byName('password')->value($password);
119  $btn = $this->byXPath("//span[contains(text(),'Sign in')]");
120  $btn->click();
121  }
122 }
123 ?>
login($user, $password)
Log into the application.
SeleniumTestCase is the base class for test cases that run with Selenium.
static createDatabase()
Create the test database, if sqlite is configured.
Definition: TestUtil.php:63
waitForXpath($xpath, $wait=30)
Wait for a DOM element matching the given xpath.
Test support classes.
Definition: namespaces.php:100
static initFramework($configPath)
Set up the wcmf framework.
Definition: TestUtil.php:36