Repository navigation
Expand file tree
/
Copy pathSelenium2.php
More file actions
288 lines (266 loc) · 7.92 KB
/
Copy pathSelenium2.php
File metadata and controls
288 lines (266 loc) · 7.92 KB
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
<?php
namespace Codeception\Module;
/**
* Uses Mink to manipulate Selenium2 WebDriver
*
* Note that all method take CSS selectors to fetch elements.
*
* On test failure the browser window screenshot will be saved to log directory
*
* <div class="alert alert-info">
* One day this module will be replaced with WebDriver module.
* </div>
*
* ## Installation
*
* Download [Selenium2 WebDriver](http://code.google.com/p/selenium/downloads/list?q=selenium-server-standalone-2)
* Launch the daemon: `java -jar selenium-server-standalone-2.xx.xxx.jar`
*
* Don't forget to turn on Db repopulation if you are using database.
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **stable**
* * Contact: codecept@davert.mail.ua
* * relies on [Mink](http://mink.behat.org)
*
* ## Configuration
*
* * url *required* - start url for your app
* * browser *required* - browser that would be launched
* * host - Selenium server host (localhost by default)
* * port - Selenium server port (4444 by default)
* * delay - set delay between actions in milliseconds (1/1000 of second) if they run too fast
* * capabilities - sets Selenium2 [desired capabilities](http://code.google.com/p/selenium/wiki/DesiredCapabilities). Should be a key-value array.
*
* ### Example (`acceptance.suite.yml`)
*
* modules:
* enabled: [Selenium2]
* config:
* Selenium2:
* url: 'http://localhost/'
* browser: firefox
* capabilities:
* unexpectedAlertBehaviour: 'accept'
*
* ## Public Properties
*
* * session - contains Mink Session
* * webDriverSession - contains webDriverSession object, i.e. $session from [php-webdriver](https://github.com/facebook/php-webdriver)
*/
use Behat\Mink\Driver\Selenium2Driver;
use Codeception\Util\MinkJS;
class Selenium2 extends MinkJS
{
protected $requiredFields = array('browser', 'url');
protected $config = array('host' => '127.0.0.1', 'port' => '4444', 'delay' => 0, 'capabilities' => array());
/**
* @var \WebDriver\Session
*/
public $webDriverSession;
public function _initialize() {
$capabilities = array_merge(Selenium2Driver::getDefaultCapabilities(), $this->config['capabilities']);
$capabilities['name'] = 'Codeception Test';
$driver = new Selenium2Driver(
$this->config['browser'],
$capabilities,
sprintf('http://%s:%d/wd/hub',$this->config['host'],$this->config['port'])
);
$this->session = new \Behat\Mink\Session($driver);
}
public function _before(\Codeception\TestCase $test)
{
if ($this->session) {
$this->session->start();
$this->webDriverSession = $this->session->getDriver()->getWebDriverSession();
}
}
public function _failed(\Codeception\TestCase $test, $error) {
$this->_saveScreenshot(\Codeception\Configuration::logDir().basename($test->getFileName()).'.fail.png');
$this->debug("Screenshot was saved into 'log' dir");
}
public function _afterStep(\Codeception\Step $step) {
if ($this->config['delay']) usleep($this->config['delay'] * 1000);
}
/**
* Low-level API method.
* If Codeception commands are not enough, use Selenium WebDriver methods directly
*
* ``` php
* $I->executeInSelenium(function(\WebDriver\Session $webdriver) {
* $webdriver->back();
* });
* ```
*
* Use [WebDriver Session API](https://github.com/facebook/php-webdriver)
* Not recommended this command too be used on regular basis.
* If Codeception lacks important Selenium methods implement then and submit patches.
*
* @param callable $function
*/
public function executeInSelenium(\Closure $function)
{
$function($this->webDriverSession);
}
public function _saveScreenshot($filename)
{
if (!$this->webDriverSession) {
$this->debug("Can't make screenshot, no web driver");
return;
}
$imgData = base64_decode($this->webDriverSession->screenshot());
file_put_contents($filename, $imgData);
}
public function click($link, $context = null, $strict = false) {
$url = $this->session->getCurrentUrl(); // required for popups
$el = $this->findClickable($link, $context, $strict);
$el->click();
}
/**
* Accept alert or confirm popup
*
* Example:
* ``` php
* <?php
* $I->click('Show alert popup');
* $I->acceptPopup();
*
* ```
*/
public function acceptPopup() {
$this->webDriverSession->accept_alert();
}
/**
* Dismiss alert or confirm popup
*
* Example:
* ``` php
* <?php
* $I->click('Show confirm popup');
* $I->cancelPopup();
*
* ```
*/
public function cancelPopup() {
$this->webDriverSession->dismiss_alert();
}
/**
* Checks if popup contains the $text
*
* Example:
* ``` php
* <?php
* $I->click('Show alert popup');
* $I->seeInPopup('Error message');
*
* ```
*
* @param string $text
*/
public function seeInPopup($text) {
$this->assertContains($text, $this->webDriverSession->alert_text());
}
/**
* Check if popup don't contains the $text
*
* Example:
* ``` php
* <?php
* $I->click();
* $I->dontSeeInPopup('Error message');
*
* ```
*
* @param string $text
*/
public function dontSeeInPopup($text) {
$this->assertNotContains($text, $this->webDriverSession->alert_text());
}
/**
* Switch to another window identified by its name.
*
* The window can only be identified by its name. If the $name parameter is blank it will switch to the parent window.
*
* Example:
* ``` html
* <input type="button" value="Open window" onclick="window.open('http://example.com', 'another_window')">
* ```
*
* ``` php
* <?php
* $I->click("Open window");
* # switch to another window
* $I->switchToWindow("another_window");
* # switch to parent window
* $I->switchToWindow();
* ?>
* ```
*
* If the window has no name, the only way to access it is via the `executeInSelenium()` method like so:
*
* ```
* <?php
* $I->executeInSelenium(function (\Webdriver\Session $webdriver) {
* $handles=$webdriver->window_handles();
* $last_window = end($handles);
* $webdriver->focusWindow($last_window);
* });
* ?>
* ```
*
* @param string|null $name
*/
public function switchToWindow($name = null) {
$this->session->getDriver()->switchToWindow($name);
}
/**
* Switch to another frame
*
* Example:
* ``` html
* <iframe name="another_frame" src="http://example.com">
*
* ```
*
* ``` php
* <?php
* # switch to iframe
* $I->switchToIFrame("another_frame");
* # switch to parent page
* $I->switchToIFrame();
*
* ```
*
* @param string|null $name
*/
public function switchToIFrame($name = null) {
$this->session->getDriver()->switchToIFrame($name);
}
/**
* Resize current window
*
* Example:
* ``` php
* <?php
* $I->resizeWindow(800, 600);
*
* ```
*
* @param int $width
* @param int $height
* @author Jaik Dean <jaik@jaikdean.com>
*/
public function resizeWindow($width, $height) {
$this->webDriverSession->window('current')->postSize(array('width' => $width, 'height' => $height));
}
public function seeInTitle($title)
{
$this->assertContains($title, $this->webDriverSession->title(), "page title contains $title");
}
public function dontSeeInTitle($title)
{
$this->assertNotContains($title, $this->webDriverSession->title(), "page title contains $title");
}
}