Skip to content

Commit bab0907

Browse files
committed
Report test results to saucelabs API to show the results on their dashboard
1 parent 8900b1f commit bab0907

File tree

4 files changed

+106
-5
lines changed

4 files changed

+106
-5
lines changed

Diff for: composer.json

+1-1
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
}

Diff for: phpunit.xml.dist

+5
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>

Diff for: tests/functional/ReportSauceLabsStatusListener.php

+88
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+
}

Diff for: tests/functional/WebDriverTestCase.php

+12-4
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
*
@@ -101,7 +109,7 @@ protected function setUpSauceLabs()
101109
*/
102110
protected function skipOnSauceLabs($message = 'Not supported by SauceLabs')
103111
{
104-
if (getenv('SAUCELABS')) {
112+
if ($this->isSauceLabsBuild()) {
105113
$this->markTestSkipped($message);
106114
}
107115
}

0 commit comments

Comments
 (0)