1 | <?php |
||
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 | 111 | public function __construct( |
|
49 | EventDispatcherInterface $event_dispatcher, |
||
50 | DriverFactoryRegistry $driver_factory_registry |
||
51 | ) { |
||
52 | 111 | $this->defaults['driver'] = 'selenium2'; |
|
53 | 111 | $this->defaults['apiUsername'] = ''; |
|
54 | 111 | $this->defaults['apiKey'] = ''; |
|
55 | |||
56 | 111 | parent::__construct($event_dispatcher, $driver_factory_registry); |
|
57 | 111 | } |
|
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 | 14 | public function setApiUsername($api_username) |
|
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 | 14 | public function setApiKey($api_key) |
|
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 | 2 | protected function setApi_username($api_username) |
|
98 | { |
||
99 | 2 | 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 | 2 | protected function setApi_key($api_key) |
|
113 | { |
||
114 | 2 | return $this->setApiKey($api_key); |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Returns port from browser configuration. |
||
119 | * |
||
120 | * @return integer |
||
121 | */ |
||
122 | 14 | public function getPort() |
|
126 | |||
127 | /** |
||
128 | * Returns browser name from browser configuration. |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | 14 | public function getBrowserName() |
|
133 | { |
||
134 | 14 | $browser_name = parent::getBrowserName(); |
|
135 | |||
136 | 14 | 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 | 16 | public function onTestSetup(TestEvent $event) |
|
147 | { |
||
148 | 16 | if ( !$event->validateSubscriber($this->getTestCase()) ) { |
|
149 | return; |
||
150 | } |
||
151 | |||
152 | 16 | parent::onTestSetup($event); |
|
153 | |||
154 | 16 | $desired_capabilities = $this->getDesiredCapabilities(); |
|
155 | 16 | $desired_capabilities[self::NAME_CAPABILITY] = $this->getJobName($event->getTestCase()); |
|
156 | |||
157 | 16 | if ( getenv('BUILD_NUMBER') ) { |
|
158 | 4 | $desired_capabilities[self::BUILD_NUMBER_CAPABILITY] = getenv('BUILD_NUMBER'); // Jenkins. |
|
159 | } |
||
160 | 12 | elseif ( getenv('TRAVIS_BUILD_NUMBER') ) { |
|
161 | 4 | $desired_capabilities[self::BUILD_NUMBER_CAPABILITY] = getenv('TRAVIS_BUILD_NUMBER'); |
|
162 | } |
||
163 | |||
164 | 16 | $this->setDesiredCapabilities($desired_capabilities); |
|
165 | 16 | } |
|
166 | |||
167 | /** |
||
168 | * Returns Job name for API service. |
||
169 | * |
||
170 | * @param BrowserTestCase $test_case Browser test case. |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 16 | protected function getJobName(BrowserTestCase $test_case) |
|
175 | { |
||
176 | 16 | if ( $this->isShared() ) { |
|
177 | 6 | return get_class($test_case); |
|
178 | } |
||
179 | |||
180 | 10 | 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 | 8 | public function onTestEnded(TestEndedEvent $event) |
|
191 | { |
||
192 | 8 | if ( !$event->validateSubscriber($this->getTestCase()) ) { |
|
193 | return; |
||
194 | } |
||
195 | |||
196 | 8 | parent::onTestEnded($event); |
|
197 | |||
198 | 8 | $session = $event->getSession(); |
|
199 | |||
200 | 8 | if ( $session === null || !$session->isStarted() ) { |
|
201 | // Session wasn't used in particular test. |
||
202 | 4 | return; |
|
203 | } |
||
204 | |||
205 | 4 | $test_case = $event->getTestCase(); |
|
206 | |||
207 | 4 | $this->getAPIClient()->updateStatus( |
|
208 | 4 | $this->getSessionId($session), |
|
209 | 2 | $this->getTestStatus($test_case, $event->getTestResult()) |
|
210 | ); |
||
211 | 2 | } |
|
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 | 4 | protected function getSessionId(Session $session) |
|
238 | |||
239 | } |
||
240 |