/
LimeSurveyWebDriver.php
182 lines (167 loc) · 4.48 KB
/
LimeSurveyWebDriver.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
<?php
namespace ls\tests;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\Exception\NoSuchElementException;
/**
* Subclass of Facebook webdriver.
* This class contains helper methods to interact with a LimeSurvey
* survey, like filling in questions, going to next question group,
* changing language etc.
*/
class LimeSurveyWebDriver extends RemoteWebDriver
{
/**
* Change language using the <select> element
* on survey welcome page.
* @param string $newLang Like 'de' or 'en'.
* @return void
*/
public function changeLanguageSelect($newLang)
{
// Try with welcome page select first.
$langSelectOption = $this->findElement(
WebDriverBy::cssSelector(
sprintf(
'#lang option[value="%s"]',
$newLang
)
)
);
$langSelectOption->click();
$langSubmit = $this->findElement(
WebDriverBy::cssSelector('button[value=changelang]')
);
$langSubmit->click();
}
/**
* Change language using links in top-right corner.
* @param string $newLang Like 'de' or 'en'.
* @return void
*/
public function changeLanguage($newLang)
{
$langSelect = $this->findElement(
WebDriverBy::cssSelector('.form-change-lang')
);
$langSelect->click();
$langSelectLink = $this->findElement(
WebDriverBy::cssSelector(
sprintf(
'.form-change-lang a[data-limesurvey-lang="%s"]',
$newLang
)
)
);
$langSelectLink->click();
}
/**
* @param string $sgqa Like 123X345X567
* @param string $answer Answer to question.
* @return void
*/
public function answerTextQuestion($sgqa, $answer)
{
$firstQuestion = $this->findElement(
WebDriverBy::cssSelector(
sprintf(
'textarea[name="%s"], input[name="%s"]',
$sgqa,
$sgqa
)
)
);
$firstQuestion->sendKeys($answer);
}
/**
* Go to next question/question group.
* @return void
*/
public function next()
{
$nextButton = $this->findElement(WebDriverBy::id('ls-button-submit'));
$nextButton->click();
}
/**
* Alias for next().
*/
public function submit()
{
$this->next();
}
/**
* @return void
*/
public function clickButton($id)
{
$button = $this->findElement(WebDriverBy::id($id));
$button->click();
}
/**
* Debug method to dump all text in <body></body>.
* @return void
*/
public function dumpBody()
{
$body = $this->findElement(WebDriverBy::tagName('body'));
var_dump('body text = ' . $body->getText());
}
/**
* @param string $id
* @return \Facebook\WebDriver\Remote\RemoteWebElement
*/
public function findById($id)
{
return $this->findElement(WebDriverBy::id($id));
}
/**
* @param string $name Name of <input>
* @return \Facebook\WebDriver\Remote\RemoteWebElement?
*/
public function findByName($name)
{
return $this->findElement(WebDriverBy::name($name));
}
/**
* @param string $css
* @return \Facebook\WebDriver\Remote\RemoteWebElement
*/
public function findByCss($css)
{
return $this->findElement(WebDriverBy::cssSelector($css));
}
/**
* @param string $text
* @return \Facebook\WebDriver\Remote\RemoteWebElement
*/
public function findByLinkText($text)
{
return $this->findElement(WebDriverBy::linkText($text));
}
/**
* @param string $text
* @return \Facebook\WebDriver\Remote\RemoteWebElement
*/
public function findByPartialLinkText($text)
{
return $this->findElement(WebDriverBy::partialLinkText($text));
}
/**
* Click "Close" on notification modal.
*
* @return void
*/
public function dismissModal()
{
try {
// If not clickable, dismiss modal.
$button = $this->findElement(
WebDriverBy::cssSelector('#admin-notification-modal .modal-footer .btn')
);
$button->click();
sleep(1);
} catch (\Exception $ex) {
// Do nothing.
}
}
}