/
WebDriverTestCase.php
124 lines (100 loc) · 4.12 KB
/
WebDriverTestCase.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
<?php
namespace Sauce\Sausage;
abstract class WebDriverTestCase extends \PHPUnit_Extensions_Selenium2TestCase
{
protected $start_url = '';
protected $is_local_test = false;
public function setUp()
{
$caps = $this->getDesiredCapabilities();
if (!isset($caps['name'])) {
$caps['name'] = get_called_class().'::'.$this->getName();
$this->setDesiredCapabilities($caps);
}
$this->setBrowserUrl($this->start_url);
}
public function setupSpecificBrowser($params)
{
// Setting 'local' gives us nice defaults of localhost:4444
$local = (isset($params['local']) && $params['local']);
$this->is_local_test = $local;
if (!$local)
SauceTestCommon::RequireSauceConfig();
// Give some nice defaults
if (!isset($params['seleniumServerRequestsTimeout']))
$params['seleniumServerRequestsTimeout'] = 60;
if (!isset($params['browserName'])) {
$params['browserName'] = 'chrome';
$params['desiredCapabilities'] = array(
'version' => '',
'platform' => 'VISTA'
);
}
// Set up host
$sauce_host = SAUCE_USERNAME.':'.SAUCE_API_KEY.'@ondemand.saucelabs.com';
$host = isset($params['host']) ? $params['host'] : false;
$this->setHost($host ? $host : ($local ? 'localhost' : $sauce_host));
// Set up port
$port = isset($params['port']) ? $params['port'] : false;
$this->setPort($port ? $port : ($local ? 4444 : 80));
// Set up other params
$this->setBrowser($params['browserName']);
$caps = isset($params['desiredCapabilities']) ? $params['desiredCapabilities'] : array();
$this->setDesiredCapabilities($caps);
$this->setSeleniumServerRequestsTimeout(
$params['seleniumServerRequestsTimeout']);
// If we're using Sauce, make sure we don't try to share browsers
if (!$local && !$host && isset($params['sessionStrategy'])) {
$params['sessionStrategy'] = 'isolated';
}
$this->setUpSessionStrategy($params);
}
public function isTextPresent($text, \PHPUnit_Extensions_Selenium2TestCase_Element $element = NULL)
{
$element = $element ?: $this->byCssSelector('body');
$el_text = str_replace("\n", " ", $element->text());
return strpos($el_text, $text) !== false;
}
public function waitForText($text, \PHPUnit_Extensions_Selenium2TestCase_Element $element = NULL,
$timeout = 10)
{
$element = $element ?: $this->byCssSelector('body');
$test = function() use ($element, $text) {
$el_text = str_replace("\n", " ", $element->text());
return strpos($el_text, $text) !== false;
};
$this->spinWait("Text $text never appeared!", $test, array(), $timeout);
}
public function assertTextPresent($text, \PHPUnit_Extensions_Selenium2TestCase_Element $element = NULL)
{
if ($element === NULL)
$element = $this->byCssSelector('body');
$this->assertContains($text, $element->text());
}
public function assertTextNotPresent($text, \PHPUnit_Extensions_Selenium2TestCase_Element $element = NULL)
{
$element = $element ?: $this->byCssSelector('body');
$this->assertNotContains($text, $element->text());
}
public function sendKeys(\PHPUnit_Extensions_Selenium2TestCase_Element $element, $keys)
{
$element->click();
$this->keys($keys);
}
public function tearDown()
{
if (!$this->is_local_test)
SauceTestCommon::ReportStatus($this->getSessionId(), !$this->hasFailed());
}
public function spinAssert($msg, $test, $args=array(), $timeout=10)
{
list($result, $msg) = SauceTestCommon::SpinAssert($msg, $test, $args, $timeout);
$this->assertTrue($result, $msg);
}
public function spinWait($msg, $test, $args=array(), $timeout=10)
{
list($result, $msg) = SauceTestCommon::SpinAssert($msg, $test, $args, $timeout);
if (!$result)
throw new \Exception($msg);
}
}