-
Notifications
You must be signed in to change notification settings - Fork 988
/
TestBaseClassWeb.php
250 lines (225 loc) · 7.52 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
<?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\WebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\Exception\TimeOutException;
use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\Exception\UnrecognizedExceptionException;
/**
* 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 LimeSurveyWebDriver $webDriver
*/
protected static $webDriver;
/**
* @var string
*/
protected static $domain;
/**
* @throws \Exception
*/
public static function setUpBeforeClass(): void
{
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::$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();
}
/**
* @return void
*/
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
self::$webDriver->quit();
}
/**
* @param $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');
//this is for testing new controllers (REFACTORING Controllers)
if(isset($view['noAdminInFront']) && $view['noAdminInFront']){
$url = $urlMan->createUrl($view['route']);
}else {
$url = $urlMan->createUrl('admin/' . $view['route']);
}
return $url;
}
/**
* @return string
*/
protected function getSurveyUrl($lang = 'en')
{
$urlMan = \Yii::app()->urlManager;
$urlMan->setBaseUrl('http://' . self::$domain . '/index.php');
$url = $urlMan->createUrl(
'survey/index',
[
'sid' => self::$surveyId,
'newtest' => 'Y',
'lang' => $lang
]
);
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')
)
);
} catch (TimeOutException $ex) {
//$name =__DIR__ . '/_output/loginfailed.png';
$screenshot = self::$webDriver->takeScreenshot();
$filename = self::$screenshotsFolder .'/FailedLogin.png';
file_put_contents($filename, $screenshot);
self::assertTrue(
false,
' Screenshot in ' . $filename . 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();
self::ignoreAdminNotification();
self::ignoreAdminNotification();
/*
try {
sleep(1);
self::$webDriver->wait(5)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::id('welcome-jumbotron')
)
);
} catch (TimeOutException $ex) {
$screenshot = self::$webDriver->takeScreenshot();
$filename = self::$screenshotsFolder .'/FailedLogin.png';
file_put_contents($filename, $screenshot);
self::assertTrue(
false,
' Screenshot in ' . $filename . 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();
}
protected function waitForElementShim(&$driver, $CSSelementSelectorString, $timeout = 10) {
$element = false;
$timeoutCounter = 0;
do {
try{
$element = $driver->findElement(WebDriverBy::cssSelector($CSSelementSelectorString));
} catch(NoSuchElementException $exception) {
$timeoutCounter++;
sleep(1);
}
} while($element === false && $timeoutCounter < $timeout);
if($element === false) {
throw new NoSuchElementException("Element not in scope after ".$timeout." seconds");
}
return $element;
}
/**
* @return void
*/
protected static function ignoreAdminNotification()
{
// Ignore password warning.
try {
$button = self::$webDriver->wait(1)->until(
WebDriverExpectedCondition::elementToBeClickable(
WebDriverBy::cssSelector('#admin-notification-modal button.btn-default')
)
);
$button->click();
} catch (TimeOutException $ex) {
// Do nothing.
} catch (NoSuchElementException $ex) {
// Do nothing.
} catch (UnrecognizedExceptionException $ex) {
// Do nothing.
}
}
}