Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 6c24959

Browse files
authored
Merge pull request #422 from OndraM/feature/saucelabs-status
Report test results to SauceLabs API
2 parents c4ec06d + 3c4c512 commit 6c24959

File tree

6 files changed

+112
-20
lines changed

6 files changed

+112
-20
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ before_script:
7777
- php -S localhost:8000 -t tests/functional/web/ &>>./logs/php-server.log &
7878

7979
script:
80-
- ./vendor/bin/phpunit --coverage-clover ./logs/coverage-clover.xml
80+
- if [ -n "$SAUCELABS" ]; then EXTRA_PARAMS="--exclude-group exclude-saucelabs"; fi
81+
- ./vendor/bin/phpunit --coverage-clover ./logs/coverage-clover.xml $EXTRA_PARAMS
8182

8283
after_script:
8384
- cat ./logs/selenium.log

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"autoload-dev": {
3232
"psr-4": {
33-
"Facebook\\WebDriver\\": "tests/unit"
33+
"Facebook\\WebDriver\\": ["tests/unit", "tests/functional"]
3434
},
3535
"classmap": ["tests/functional/"]
3636
}

phpunit.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@
2929
<directory suffix=".php">./lib</directory>
3030
</whitelist>
3131
</filter>
32+
33+
<listeners>
34+
<listener class="Facebook\WebDriver\ReportSauceLabsStatusListener"/>
35+
</listeners>
36+
3237
</phpunit>

tests/functional/RemoteWebDriverTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ public function testShouldGetSessionId()
7575
}
7676

7777
/**
78+
* @group exclude-saucelabs
7879
* @covers ::getAllSessions
7980
*/
8081
public function testShouldGetAllSessions()
8182
{
82-
$this->skipOnSauceLabs('getAllSessions() is not supported on SauceLabs');
83-
8483
$sessions = RemoteWebDriver::getAllSessions($this->serverUrl);
8584

8685
$this->assertInternalType('array', $sessions);
@@ -92,14 +91,13 @@ public function testShouldGetAllSessions()
9291
}
9392

9493
/**
94+
* @group exclude-saucelabs
9595
* @covers ::getAllSessions
9696
* @covers ::getCommandExecutor
9797
* @covers ::quit
9898
*/
9999
public function testShouldQuitAndUnsetExecutor()
100100
{
101-
$this->skipOnSauceLabs('getAllSessions() is not supported on SauceLabs');
102-
103101
$this->assertCount(1, RemoteWebDriver::getAllSessions($this->serverUrl));
104102
$this->assertInstanceOf(HttpCommandExecutor::class, $this->driver->getCommandExecutor());
105103

@@ -169,7 +167,7 @@ function(){document.getElementById("id_test").innerHTML = "Text changed by scrip
169167
$this->assertSame('Test by ID', $element->getText());
170168

171169
// If we wait, the script should be executed
172-
usleep(550000); // wait 550 ms
170+
usleep(1000000); // wait 1000 ms
173171
$this->assertSame('Text changed by script', $element->getText());
174172
}
175173

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace Facebook\WebDriver;
17+
18+
use Facebook\WebDriver\Remote\RemoteWebDriver;
19+
20+
class ReportSauceLabsStatusListener extends \PHPUnit_Framework_BaseTestListener
21+
{
22+
public function endTest(\PHPUnit_Framework_Test $test, $time)
23+
{
24+
if (!$test instanceof WebDriverTestCase || !$test->driver instanceof RemoteWebDriver) {
25+
return;
26+
}
27+
28+
/** @var WebDriverTestCase $test */
29+
if (!$test->isSauceLabsBuild()) {
30+
return;
31+
}
32+
33+
$testStatus = $test->getStatus();
34+
35+
if ($this->testWasSkippedOrIncomplete($testStatus)) {
36+
return;
37+
}
38+
39+
$endpointUrl = sprintf(
40+
'https://saucelabs.com/rest/v1/%s/jobs/%s',
41+
getenv('SAUCE_USERNAME'),
42+
$test->driver->getSessionID()
43+
);
44+
45+
$data = [
46+
'passed' => ($testStatus === \PHPUnit_Runner_BaseTestRunner::STATUS_PASSED),
47+
'custom-data' => ['message' => $test->getStatusMessage()],
48+
];
49+
50+
$this->submitToSauceLabs($endpointUrl, $data);
51+
}
52+
53+
/**
54+
* @param int $testStatus
55+
* @return bool
56+
*/
57+
private function testWasSkippedOrIncomplete($testStatus)
58+
{
59+
if ($testStatus === \PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED
60+
|| $testStatus === \PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE) {
61+
return true;
62+
}
63+
64+
return false;
65+
}
66+
67+
/**
68+
* @param string $url
69+
* @param array $data
70+
*/
71+
private function submitToSauceLabs($url, array $data)
72+
{
73+
$curl = curl_init($url);
74+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
75+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
76+
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
77+
curl_setopt($curl, CURLOPT_USERPWD, getenv('SAUCE_USERNAME') . ':' . getenv('SAUCE_ACCESS_KEY'));
78+
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
79+
80+
curl_exec($curl);
81+
82+
if (curl_errno($curl)) {
83+
throw new \Exception(sprintf('Error publishing test results to SauceLabs: %s', curl_error($curl)));
84+
}
85+
86+
curl_close($curl);
87+
}
88+
}

tests/functional/WebDriverTestCase.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@
2525
*/
2626
class WebDriverTestCase extends \PHPUnit_Framework_TestCase
2727
{
28+
/** @var RemoteWebDriver $driver */
29+
public $driver;
2830
/** @var bool Indicate whether WebDriver should be created on setUp */
2931
protected $createWebDriver = true;
3032
/** @var string */
3133
protected $serverUrl = 'http://localhost:4444/wd/hub';
32-
/** @var RemoteWebDriver $driver */
33-
protected $driver;
3434
/** @var DesiredCapabilities */
3535
protected $desiredCapabilities;
3636

3737
protected function setUp()
3838
{
3939
$this->desiredCapabilities = new DesiredCapabilities();
4040

41-
if (getenv('SAUCELABS')) {
41+
if ($this->isSauceLabsBuild()) {
4242
$this->setUpSauceLabs();
4343
} else {
4444
if (getenv('BROWSER_NAME')) {
@@ -66,6 +66,14 @@ protected function tearDown()
6666
}
6767
}
6868

69+
/**
70+
* @return bool
71+
*/
72+
public function isSauceLabsBuild()
73+
{
74+
return getenv('SAUCELABS') ? true : false;
75+
}
76+
6977
/**
7078
* Get the URL of given test HTML on running webserver.
7179
*
@@ -87,20 +95,12 @@ protected function setUpSauceLabs()
8795
$this->desiredCapabilities->setBrowserName(getenv('BROWSER_NAME'));
8896
$this->desiredCapabilities->setVersion(getenv('VERSION'));
8997
$this->desiredCapabilities->setPlatform(getenv('PLATFORM'));
98+
$this->desiredCapabilities->setCapability('name', get_class($this) . '::' . $this->getName());
99+
$this->desiredCapabilities->setCapability('tags', [get_class($this)]);
90100

91101
if (getenv('TRAVIS_JOB_NUMBER')) {
92102
$this->desiredCapabilities->setCapability('tunnel-identifier', getenv('TRAVIS_JOB_NUMBER'));
93103
$this->desiredCapabilities->setCapability('build', getenv('TRAVIS_JOB_NUMBER'));
94104
}
95105
}
96-
97-
/**
98-
* @param string $message
99-
*/
100-
protected function skipOnSauceLabs($message = 'Not supported by SauceLabs')
101-
{
102-
if (getenv('SAUCELABS')) {
103-
$this->markTestSkipped($message);
104-
}
105-
}
106106
}

0 commit comments

Comments
 (0)