| 1 | <?php |
||
| 29 | abstract class ApiBrowserConfiguration extends BrowserConfiguration |
||
|
1 ignored issue
–
show
|
|||
| 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) |
|
| 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) |
|
| 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) |
||
| 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) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns port from browser configuration. |
||
| 119 | * |
||
| 120 | * @return integer |
||
| 121 | */ |
||
| 122 | 18 | public function getPort() |
|
| 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) |
|
| 238 | |||
| 239 | } |
||
| 240 |