summaryrefslogtreecommitdiff
path: root/tests/test_tools/PradoGenericSelenium2Test.php
blob: ec948f7fcec9fedbe745e4b285eb6d3255aef941 (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
require_once 'PHPUnit/Extensions/Selenium2TestCase.php';

// TODO: stub
class PradoGenericSelenium2Test extends PHPUnit_Extensions_Selenium2TestCase
{
	public static $browsers = array(
/*
		array(
			'name'    => 'Firefox on OSX',
			'browserName' => '*firefox',
			'host'    => '127.0.0.1',
			'port'    => 4444,
		),
*/
		array(
			'name'    => 'Chrome on OSX',
			'browserName' => 'chrome',
			'sessionStrategy' => 'shared',
			'host'    => '127.0.0.1',
			'port'    => 4444,
		),
/*
		array(
			'name'    => 'Firefox on WindowsXP',
			'browserName' => '*firefox',
			'host'    => '127.0.0.1',
			'port'    => 4445,
		),
		array(
			'name'    => 'Internet Explorer 8 on WindowsXP',
			'browserName' => '*iehta',
			'host'    => '127.0.0.1',
			'port'    => 4445,
		)
*/
	);

	static $baseurl='http://127.0.0.1/prado-master/tests/FunctionalTests/';

	static $timeout=5; //seconds
	static $wait=1000; //msecs

	protected function setUp()
	{
		self::shareSession(true);
		$this->setBrowserUrl(static::$baseurl);
		$this->setSeleniumServerRequestsTimeout(static::$timeout);
	}

	protected function verifyTitle($txt)
	{
		$this->assertEquals($txt, $this->title());
	}

	protected function assertTextPresent($txt)
	{
		if(strpos($txt, 'regexp:')===0)
		{
			$this->assertRegExp('/'.substr($txt, 7).'/', $this->source());
		} else {
			$this->assertContains($txt, $this->source());
		}
	}

	protected function verifyAttribute($idattr, $txt)
	{
		list($id, $attr) = explode('@', $idattr);

		$element = $this->getElement($id);
		$value=$element->attribute($attr);

		if(strpos($txt, 'regexp:')===0)
		{
			$this->assertRegExp('/'.substr($txt, 7).'/', $value);
		} else {
			$this->assertEquals($txt, $value);
		}
	}

	protected function assertTextNotPresent($txt)
	{
		$this->assertNotContains($txt, $this->source());
	}

	protected function assertChecked($id)
	{
		$this->assertTrue($this->getElement($id)->selected());
	}

	protected function assertNotChecked($id)
	{
		$this->assertFalse($this->getElement($id)->selected());
	}

	protected function getElement($id)
	{
		if(strpos($id, 'xpath=')===0)
		{
			return $this->byXPath(substr($id, 6));
		} elseif(strpos($id, 'css=')===0) {
			return $this->byCssSelector(substr($id, 4));
		} elseif(strpos($id, 'id=')===0) {
			return $this->byId(substr($id, 3));
		} elseif(strpos($id, 'link=')===0) {
			return $this->byLinkText(substr($id, 5));
		} elseif(strpos($id, 'name=')===0) {
			return $this->byName(substr($id, 5));
		} elseif(strpos($id, '//')===0) {
			return $this->byXPath($id);
		} elseif(strpos($id, '$')!==false) {
			return $this->byName($id);
		} else {
			return $this->byId($id);
		}
	}

	protected function assertText($id, $txt)
	{
		$this->assertEquals($txt, $this->getElement($id)->text());
	}

	protected function assertValue($id, $txt)
	{
		$this->assertEquals($txt, $this->getElement($id)->value());
	}

	protected function assertVisible($id)
	{
		$this->assertTrue($this->getElement($id)->displayed());
	}

	protected function assertNotVisible($id)
	{
		$this->assertFalse($this->getElement($id)->displayed());
	}

	protected function assertElementPresent($id)
	{
		$this->assertTrue($this->getElement($id)!==null);
	}

	protected function assertAlert($txt)
	{
		$this->assertEquals($txt, $this->alertText());
		$this->acceptAlert();
	}

	protected function verifyConfirmation($txt)
	{
		$this->assertAlert($txt);
	}

	protected function verifyConfirmationDismiss($txt)
	{
		$this->assertEquals($txt, $this->alertText());
		$this->dismissAlert();
	}

	protected function assertElementNotPresent($id)
	{
		try {
			$el = $this->getElement($id);
		} catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
			$this->assertEquals(PHPUnit_Extensions_Selenium2TestCase_WebDriverException::NoSuchElement, $e->getCode());
			return;
		}
		$this->fail('The element '.$id.' shouldn\'t exist.');
	}

	protected function type($id, $txt='')
	{
		$element = $this->getElement($id);
		// clear the textbox without using clear() that triggers onchange()
		// the idea is to focus the input, move to the end of the text and hit
		// backspace until the input is empty.
		// on multiline textareas, line feeds can make this difficult, so we mix
		// sequences of end+backspace and start+delete

		$element->click();
		while(strlen($element->value())>0)
		{
			$this->keys(PHPUnit_Extensions_Selenium2TestCase_Keys::END);
			// the number 100 is purely empiric
			for($i=0;$i<100;$i++)
				$this->keys(PHPUnit_Extensions_Selenium2TestCase_Keys::BACKSPACE);

			$this->keys(PHPUnit_Extensions_Selenium2TestCase_Keys::HOME);
			// the number 100 is purely empiric
			for($i=0;$i<100;$i++)
				$this->keys(PHPUnit_Extensions_Selenium2TestCase_Keys::DELETE);
		}

		$element->value($txt);
		// trigger onblur() event
		$this->clickAt('css=body', '1,1');
	}

	protected function click($id, $foo='bar')
	{
		$this->getElement($id)->click();
	}

	protected function clickAt($id, $coords)
	{
//		list($x, $y) = explode(',', $coords);
		$this->moveto(array(
			'element' => $this->getElement($id),
//			'xoffset' => intval($x),
//			'yoffset' => intval($y),
		));

		parent::click();
	}

	protected function mouseOver($id)
	{
		$this->moveto(array(
			'element' => $this->getElement($id),
//			'xoffset' => 1,
//			'yoffset' => 1,
		));
	}

	protected function mouseOut($id)
	{
		$this->moveto(array(
			'element' => $this->getElement('css=body'),
//			'xoffset' => 0,
//			'yoffset' => 0,
		));
	}

	protected function clickAndWait($id, $foo='bar')
	{
		$this->click($id, $foo);
	}

	protected function select($id, $value)
	{
		$select = parent::select($this->getElement($id));
		$select->clearSelectedOptions();

		if(strpos($value, 'label=')===0)
		{
			$select->selectOptionByLabel(substr($value, 6));
		} else {
			$select->selectOptionByLabel($value);
		}
	}

	protected function selectAndWait($id, $value)
	{
		$this->select($id, $value);
	}

	protected function addSelection($id, $value)
	{
		$select = parent::select($this->getElement($id));

		if(strpos($value, 'label=')===0)
		{
			$select->selectOptionByLabel(substr($value, 6));
		} else {
			$select->selectOptionByLabel($value);
		}
	}

	protected function getSelectedLabels($id)
	{
		return parent::select($this->getElement($id))->selectedLabels();
	}

	protected function getSelectOptions($id)
	{
		return parent::select($this->getElement($id))->selectOptionLabels();
	}

	protected function assertSelectedIndex($id, $value)
	{
		$options=parent::select($this->getElement($id))->selectOptionValues();
		$curval=parent::select($this->getElement($id))->selectedValue();

		$i=0;
		foreach($options as $option)
		{
			if($option==$curval)
			{
				$this->assertEquals($i, $value);
				return;
			}
			$i++;
		}
		$this->fail('Current value '.$curval.' not found in: '.implode(',', $options));
	}

	protected function assertSelected($id, $label)
	{
		$this->assertSame($label, parent::select($this->getElement($id))->selectedLabel());
	}

	protected function assertNotSomethingSelected($id)
	{
		$this->assertSame(array(), $this->getSelectedLabels($id));
	}

	protected function assertSelectedValue($id, $index)
	{
		$this->assertSame($index, parent::select($this->getElement($id))->selectedValue());
	}

	protected function runScript($script)
	{
		$this->execute(array(
			'script' => $script,
			'args'   => array()
		));
	}

	protected function assertAlertNotPresent()
	{
		try {
			$foo=$this->alertText();
		} catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
			$this->assertEquals(PHPUnit_Extensions_Selenium2TestCase_WebDriverException::NoAlertOpenError, $e->getCode());
			return;
		}
		$this->fail('Failed asserting no alert is open');
	}

	protected function pause($msec)
	{
		usleep($msec*1000);
	}

}