/
TestBaseClassWeb.php
303 lines (269 loc) · 8.83 KB
/
TestBaseClassWeb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* LimeSurvey
* Copyright (C) 2007-2011 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
namespace ls\tests;
use Facebook\WebDriver\Remote\RemoteWebElement;
use Facebook\WebDriver\WebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\Exception\TimeOutException;
use User;
/**
* Class TestBaseClassWeb
* this is the base class for functional tests that need browser simulation
* @package ls\tests
*/
class TestBaseClassWeb extends TestBaseClass
{
/**
* @var int web server port
* TODO this should be in configuration somewhere
*/
public static $webPort = 4444;
/** @var \User $user current user */
protected static $user;
/** @var int $superUserId */
public static $superUserId = 1;
/** @var string $noPermissionsUserPassword */
protected static $noPermissionsUserPassword = 'myHardPassword';
/** @var string $superUserUsername */
public static $superUserUsername = 'admin';
/** @var string $noPermissionsUserUsername */
public static $noPermissionsUserUsername = 'noPermissionsUser';
/** @var User $noPermissionsUser */
public static $noPermissionsUser;
/**
* @var WebDriver $webDriver
*/
protected static $webDriver;
/**
* @var string
*/
protected static $domain;
/** @var string $url current url */
protected $url;
/**
* @throws \Exception
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
$domain = getenv('DOMAIN');
if (empty($domain)) {
echo 'Must specify DOMAIN environment variable to run this test, like "DOMAIN=localhost/limesurvey" or "DOMAIN=limesurvey.localhost".';
exit(12);
}
self::$domain = getenv('DOMAIN');
self::setUpWebDriver();
self::tearDownTestUsers();
self::setUpNoPermissionsUser();
self::deleteLoginTimeout();
}
/**
* @throws \CDbException
*/
public static function tearDownAfterClass()
{
parent::tearDownAfterClass();
self::$webDriver->quit();
self::tearDownTestUsers();
}
/**
* @throws \CDbException
*/
private static function tearDownTestUsers(){
$noPermissionsUser = User::findByUsername(self::$noPermissionsUserUsername);
if($noPermissionsUser){
$noPermissionsUser->delete();
}
}
/**
* @throws \Exception
*/
private static function setUpNoPermissionsUser(){
$user = new User();
$user->users_name =self::$noPermissionsUserUsername;
$user->email = 'no-permissions@example.com';
$user->full_name = 'Iha Veno Permissioons';
$user->setPassword(self::$noPermissionsUserPassword);
if($user->save()){
self::$noPermissionsUser = $user;
}else{
throw new \Exception('Could not create User: '.serialize($user->errors));
}
}
/**
* @throws \Exception
*/
private static function setUpWebDriver(){
self::$webDriver = self::$testHelper->getWebDriver();
if (empty(self::$webDriver)) {
throw new \Exception('Could not connect to remote web driver');
}
// Implicit timout so we don't have to wait manually.
self::$webDriver->manage()->timeouts()->implicitlyWait(5);
// Anyone can preview surveys.
self::$testHelper->enablePreview();
}
/**
* @param string $url
* @return WebDriver
* @throws \Exception
* @internal param array $view
*/
public static function openView($url)
{
if (!is_string($url)) {
throw new \Exception('$url must be a string, is ' . json_encode($url));
}
return self::$webDriver->get($url);
}
/**
* Get URL to admin view.
* @param array $view
* @return string
* @todo Rename to getAdminUrl.
*/
public static function getUrl(array $view)
{
$urlMan = \Yii::app()->urlManager;
$urlMan->setBaseUrl('http://' . self::$domain . '/index.php');
$url = $urlMan->createUrl('admin/' . $view['route']);
return $url;
}
/**
* @param string $userName
* @param string $password
* @return void
* @throws \Exception
* @throws \Facebook\WebDriver\Exception\NoSuchElementException
*/
public static function adminLogin($userName, $password)
{
$url = self::getUrl(['login', 'route'=>'authentication/sa/login']);
self::openView($url);
try {
self::$webDriver->wait(5)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::id('user')
)
);
self::$user = User::findByUsername($userName);
} catch (TimeOutException $ex) {
//$name =__DIR__ . '/_output/loginfailed.png';
$shotName = self::takeScreenShot('FailedLogin');
self::assertTrue(
false,
'Screenshot in '.$shotName . PHP_EOL .
sprintf(
'Could not login on url %s: Could not find element with id "user".',
$url
)
);
}
$userNameField = self::$webDriver->findElement(WebDriverBy::id("user"));
$userNameField->clear()->sendKeys($userName);
$passWordField = self::$webDriver->findElement(WebDriverBy::id("password"));
$passWordField->clear()->sendKeys($password);
$submit = self::$webDriver->findElement(WebDriverBy::name('login_submit'));
$submit->click();
try {
self::$webDriver->wait(2)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::id('welcome-jumbotron')
)
);
} catch (TimeOutException $ex) {
$shotName = self::takeScreenShot('FailedLogin');
self::assertTrue(
false,
' Screenshot in ' . $shotName . PHP_EOL .
'Found no welcome jumbotron after login.'
);
}
}
/**
* Delete failed login attempts.
* @throws \CDbException
*/
protected static function deleteLoginTimeout()
{
$dbo = \Yii::app()->getDb();
$dbo
->createCommand('DELETE FROM {{failed_login_attempts}}')
->execute();
}
/**
* @param WebDriverBy $selector
* @param int $waitSecondsUntil
* @return mixed
* @throws \Exception
*/
protected static function findAndClick($selector,$waitSecondsUntil = 2){
try {
$clickable = self::$webDriver->wait($waitSecondsUntil)->until(
WebDriverExpectedCondition::elementToBeClickable($selector)
);
$clickable->click();
sleep(1);
self::takeScreenShot('AfterGoodClick');
return $clickable;
} catch (\Exception $ex) {
self::takeScreenShot('FailedClick');
}
}
/**
* @param WebDriverBy $selector
* @param int $waitSecondsUntil
* @return WebDriverElement
* @throws \Exception
*/
protected static function find($selector,$waitSecondsUntil = 1){
try {
self::$webDriver->wait($waitSecondsUntil)->until(
WebDriverExpectedCondition::presenceOfElementLocated($selector)
);
$element = self::$webDriver->findElement($selector);
return $element;
} catch (\Exception $ex) {
self::takeScreenShot('FailedFindElement');
throw $ex;
}
}
/**
* @param $name
* @param $view
* @throws \Exception
*/
protected function openAndFindViewTag($name, $view){
$this->url = $this->getUrl($view);
$this->openView($this->url);
return $this->findViewTag($name,$view);
}
/**
* @param string $name
* @return WebDriverElement
* @throws \Exception
*/
protected function findViewTag($name)
{
$element = self::find(WebDriverBy::id('action::' . $name));
return $element;
}
public static function takeScreenShot($name){
$screenshot = self::$webDriver->takeScreenshot();
$filename = self::$screenshotsFolder .'/'.microtime(true).'_'.$name.'.png';
file_put_contents($filename, $screenshot);
return $filename;
}
}