1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the phpunit-mink library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/aik099/phpunit-mink |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace aik099\PHPUnit\BrowserConfiguration; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use aik099\PHPUnit\APIClient\IAPIClient; |
15
|
|
|
use aik099\PHPUnit\BrowserTestCase; |
16
|
|
|
use aik099\PHPUnit\Event\TestEndedEvent; |
17
|
|
|
use aik099\PHPUnit\Event\TestEvent; |
18
|
|
|
use aik099\PHPUnit\MinkDriver\DriverFactoryRegistry; |
19
|
|
|
use Behat\Mink\Driver\Selenium2Driver; |
20
|
|
|
use Behat\Mink\Session; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Browser configuration tailored to use with API-based service. |
25
|
|
|
* |
26
|
|
|
* @method string getApiUsername() Returns API username. |
27
|
|
|
* @method string getApiKey() Returns API key. |
28
|
|
|
*/ |
29
|
|
|
abstract class ApiBrowserConfiguration extends BrowserConfiguration |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The build number. |
34
|
|
|
*/ |
35
|
|
|
const BUILD_NUMBER_CAPABILITY = 'build'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The test name. |
39
|
|
|
*/ |
40
|
|
|
const NAME_CAPABILITY = 'name'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates browser configuration. |
44
|
|
|
* |
45
|
|
|
* @param EventDispatcherInterface $event_dispatcher Event dispatcher. |
46
|
|
|
* @param DriverFactoryRegistry $driver_factory_registry Driver factory registry. |
47
|
|
|
*/ |
48
|
113 |
|
public function __construct( |
49
|
|
|
EventDispatcherInterface $event_dispatcher, |
50
|
|
|
DriverFactoryRegistry $driver_factory_registry |
51
|
|
|
) { |
52
|
113 |
|
$this->defaults['driver'] = 'selenium2'; |
53
|
113 |
|
$this->defaults['apiUsername'] = ''; |
54
|
113 |
|
$this->defaults['apiKey'] = ''; |
55
|
|
|
|
56
|
113 |
|
parent::__construct($event_dispatcher, $driver_factory_registry); |
57
|
113 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Sets API username. |
61
|
|
|
* |
62
|
|
|
* To be called from TestCase::setUp(). |
63
|
|
|
* |
64
|
|
|
* @param string $api_username API username. |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
15 |
|
public function setApiUsername($api_username) |
69
|
|
|
{ |
70
|
15 |
|
return $this->setParameter('apiUsername', $api_username); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sets API key. |
75
|
|
|
* |
76
|
|
|
* To be called from TestCase::setUp(). |
77
|
|
|
* |
78
|
|
|
* @param string $api_key API key. |
79
|
|
|
* |
80
|
|
|
* @return self |
81
|
|
|
*/ |
82
|
15 |
|
public function setApiKey($api_key) |
83
|
|
|
{ |
84
|
15 |
|
return $this->setParameter('apiKey', $api_key); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets API username. |
89
|
|
|
* |
90
|
|
|
* Used internally to to allow using "api_username" parameter and avoid BC break. |
91
|
|
|
* |
92
|
|
|
* @param string $api_username API username. |
93
|
|
|
* |
94
|
|
|
* @return self |
95
|
|
|
* @deprecated |
96
|
|
|
*/ |
97
|
|
|
protected function setApi_username($api_username) |
98
|
|
|
{ |
99
|
|
|
return $this->setApiUsername($api_username); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sets API key. |
104
|
|
|
* |
105
|
|
|
* Used internally to to allow using "api_key" parameter and avoid BC break. |
106
|
|
|
* |
107
|
|
|
* @param string $api_key API key. |
108
|
|
|
* |
109
|
|
|
* @return self |
110
|
|
|
* @deprecated |
111
|
|
|
*/ |
112
|
|
|
protected function setApi_key($api_key) |
113
|
|
|
{ |
114
|
|
|
return $this->setApiKey($api_key); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns port from browser configuration. |
119
|
|
|
* |
120
|
|
|
* @return integer |
121
|
|
|
*/ |
122
|
18 |
|
public function getPort() |
123
|
|
|
{ |
124
|
18 |
|
return 80; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns browser name from browser configuration. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
18 |
|
public function getBrowserName() |
133
|
|
|
{ |
134
|
18 |
|
$browser_name = parent::getBrowserName(); |
135
|
|
|
|
136
|
18 |
|
return strlen($browser_name) ? $browser_name : 'chrome'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Hook, called from "BrowserTestCase::setUp" method. |
141
|
|
|
* |
142
|
|
|
* @param TestEvent $event Test event. |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
24 |
|
public function onTestSetup(TestEvent $event) |
147
|
|
|
{ |
148
|
24 |
|
if ( !$event->validateSubscriber($this->getTestCase()) ) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
24 |
|
parent::onTestSetup($event); |
153
|
|
|
|
154
|
24 |
|
$desired_capabilities = $this->getDesiredCapabilities(); |
155
|
24 |
|
$desired_capabilities[self::NAME_CAPABILITY] = $this->getJobName($event->getTestCase()); |
156
|
|
|
|
157
|
24 |
|
if ( getenv('BUILD_NUMBER') ) { |
158
|
4 |
|
$desired_capabilities[self::BUILD_NUMBER_CAPABILITY] = getenv('BUILD_NUMBER'); // Jenkins. |
159
|
4 |
|
} |
160
|
20 |
|
elseif ( getenv('TRAVIS_BUILD_NUMBER') ) { |
161
|
4 |
|
$desired_capabilities[self::BUILD_NUMBER_CAPABILITY] = getenv('TRAVIS_BUILD_NUMBER'); |
162
|
4 |
|
} |
163
|
|
|
|
164
|
24 |
|
$this->setDesiredCapabilities($desired_capabilities); |
165
|
24 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns Job name for API service. |
169
|
|
|
* |
170
|
|
|
* @param BrowserTestCase $test_case Browser test case. |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
24 |
|
protected function getJobName(BrowserTestCase $test_case) |
175
|
|
|
{ |
176
|
24 |
|
if ( $this->isShared() ) { |
177
|
8 |
|
return get_class($test_case); |
178
|
|
|
} |
179
|
|
|
|
180
|
16 |
|
return $test_case->toString(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Hook, called from "BrowserTestCase::run" method. |
185
|
|
|
* |
186
|
|
|
* @param TestEndedEvent $event Test ended event. |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
10 |
|
public function onTestEnded(TestEndedEvent $event) |
191
|
|
|
{ |
192
|
10 |
|
if ( !$event->validateSubscriber($this->getTestCase()) ) { |
193
|
1 |
|
return; |
194
|
|
|
} |
195
|
|
|
|
196
|
10 |
|
parent::onTestEnded($event); |
197
|
|
|
|
198
|
10 |
|
$session = $event->getSession(); |
199
|
|
|
|
200
|
10 |
|
if ( $session === null || !$session->isStarted() ) { |
201
|
|
|
// Session wasn't used in particular test. |
202
|
4 |
|
return; |
203
|
|
|
} |
204
|
|
|
|
205
|
6 |
|
$test_case = $event->getTestCase(); |
206
|
|
|
|
207
|
6 |
|
$this->getAPIClient()->updateStatus( |
208
|
6 |
|
$this->getSessionId($session), |
209
|
4 |
|
$this->getTestStatus($test_case, $event->getTestResult()) |
210
|
4 |
|
); |
211
|
4 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns API class for service interaction. |
215
|
|
|
* |
216
|
|
|
* @return IAPIClient |
217
|
|
|
*/ |
218
|
|
|
public abstract function getAPIClient(); |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get Selenium2 current session id. |
222
|
|
|
* |
223
|
|
|
* @param Session $session Session. |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
* @throws \RuntimeException When session was created using an unsupported driver. |
227
|
|
|
*/ |
228
|
6 |
|
protected function getSessionId(Session $session) |
229
|
|
|
{ |
230
|
6 |
|
$driver = $session->getDriver(); |
231
|
|
|
|
232
|
6 |
|
if ( $driver instanceof Selenium2Driver ) { |
233
|
4 |
|
return $driver->getWebDriverSessionId(); |
234
|
|
|
} |
235
|
|
|
|
236
|
2 |
|
throw new \RuntimeException('Unsupported session driver'); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
} |
240
|
|
|
|