Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.

Automatically score tests #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions lib/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Drewberry™
*
* @author Wagner Silva <wagnerjsilva@gmail.com>
* @link <https://twitter.com/wagnerjsilva>
* @version 1.0
*/

namespace CBT;

class Api
{
protected $user;
protected $pass;
protected $url = 'https://crossbrowsertesting.com/api/v3/selenium';

public function __construct($user, $password)
{

$this->setUser($user);
$this->setPass($password);
}

protected function getUser()
{
return $this->user;
}

protected function getPass()
{
return $this->pass;
}

protected function setUser($user)
{
$this->user = $user;
}

protected function setPass($pass)
{
$this->pass = $pass;
}

protected function call($session_id, $method = 'GET', $params = false)
{
$apiResult = new \stdClass();
$process = curl_init();
switch ($method) {
case "POST":
curl_setopt($process, CURLOPT_POST, 1);
if ($params) {
curl_setopt($process, CURLOPT_POSTFIELDS, $params);
curl_setopt($process, CURLOPT_HTTPHEADER, array('User-Agent: php')); //important
}
break;
case "PUT":
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "PUT");
if ($params) {
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($process, CURLOPT_HTTPHEADER, array('User-Agent: php')); //important
}
break;
case 'DELETE':
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
if ($params) {
$this->url = sprintf("%s?%s", $this->url, http_build_query($params));
}
}
// Optional Authentication:
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, $this->getUser().":".$this->getPass());
curl_setopt($process, CURLOPT_URL, $this->url.'/'.$session_id);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
$apiResult->content = curl_exec($process);
$apiResult->httpResponse = curl_getinfo($process);
$apiResult->errorMessage = curl_error($process);
$apiResult->params = $params;
curl_close($process);
$paramsString = $params ? http_build_query($params) : '';
$response = json_decode($apiResult->content);
if ($apiResult->httpResponse['http_code'] != 200) {
$message = 'Error calling "'.$apiResult->httpResponse['url'].'" ';
$message .= (isset($paramsString) ? 'with params "'.$paramsString.'" ' : ' ');
$message .= '. Returned HTTP status '.$apiResult->httpResponse['http_code'].' ';
$message .= (isset($apiResult->errorMessage) ? $apiResult->errorMessage : ' ');
$message .= (isset($response->message) ? $response->message : ' ');
die($message);
} else {
$response = json_decode($apiResult->content);
if (isset($response->status)) {
die('Error calling "'.$apiResult->httpResponse['url'].'"'.(isset($paramsString) ? 'with params "'.$paramsString.'"' : '').'". '.$response->message);
}
}
return $response;
}

public function score ($session_id, $score){
return $this->call($session_id, 'PUT', ['score' => $score, 'action' => 'set_score']);
}
}
58 changes: 45 additions & 13 deletions lib/CBTContext.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,81 @@
<?php

require 'vendor/autoload.php';

class CBTContext extends Behat\Behat\Context\BehatContext
{
protected $CONFIG;
protected static $driver;
protected static $url;
private static $api = null;

public function __construct($parameters){
public function __construct($parameters)
{
$GLOBALS['CONFIG'] = $parameters["cbt"];

$GLOBALS['CBT_USERNAME'] = getenv('CBT_USERNAME');
if(!$GLOBALS['CBT_USERNAME']) $GLOBALS['CBT_USERNAME'] = $GLOBALS['CONFIG']['user'];
if (!$GLOBALS['CBT_USERNAME'])
$GLOBALS['CBT_USERNAME'] = $GLOBALS['CONFIG']['user'];

$GLOBALS['CBT_AUTHKEY'] = getenv('CBT_AUTHKEY');
if(!$GLOBALS['CBT_AUTHKEY']) $GLOBALS['CBT_AUTHKEY'] = $GLOBALS['CONFIG']['key'];
if (!$GLOBALS['CBT_AUTHKEY'])
$GLOBALS['CBT_AUTHKEY'] = $GLOBALS['CONFIG']['key'];

$GLOBALS['CBT_URL'] = getenv('CBT_URL');
if (!$GLOBALS['CBT_URL'])
$GLOBALS['CBT_URL'] = $GLOBALS['CONFIG']['base_url'];


self::$api = new CBT\Api($parameters['cbt']['user'], $parameters['cbt']['key']);
}

/** @BeforeFeature */
public static function setup()
{
$CONFIG = $GLOBALS["CONFIG"];
$CONFIG = $GLOBALS["CONFIG"];

# Each parallel test we are running will contain
$test_run_id = getenv("TEST_RUN_ID") ? getenv("TEST_RUN_ID") : 0;

# Each parallel test we are running will contain
$test_run_id = getenv("TEST_RUN_ID") ? getenv("TEST_RUN_ID") : 0;

# build the webdriver hub URL (e.g. https://username:authkey@crossbrowsertesting.com:80/wd/hub)
$url = "https://" . $GLOBALS["CBT_USERNAME"] . ":" . $GLOBALS["CBT_AUTHKEY"] . "@" . $CONFIG["server"] ."/wd/hub";
$url = "https://".$GLOBALS["CBT_USERNAME"].":".$GLOBALS["CBT_AUTHKEY"]."@".$CONFIG["server"]."/wd/hub";

# get the capabilities for this test_run_id
# caps contains the os, browser, and resolution
$browserCaps = $CONFIG["browsers"][$test_run_id];
# pull in capabilities that we want applied to all tests
foreach ($CONFIG["capabilities"] as $capName => $capValue) {
if(!array_key_exists($capName, $browserCaps))
if (!array_key_exists($capName, $browserCaps))
$browserCaps[$capName] = $capValue;
}

self::$driver = RemoteWebDriver::create($url, $browserCaps, 120000, 120000);
self::$url = $GLOBALS["CBT_URL"];
}

private static function scoreTest($status)
{
return self::$api->score(self::$driver->getSessionID(), $status);
}

/** @AfterFeature */
public static function tearDown()
public static function tearDown(Behat\Behat\Event\FeatureEvent $scope)
{

/*
* getResult() - returns the resulting (highest)
* feature run code:
* 4 when the feature has failed steps,
* 3 when the feature has undefined steps,
* 2 when the feature has pending steps,
* 0 when all steps are passing.
*/

if ($scope->getResult() === 0) {
self::scoreTest('pass');
} else {
self::scoreTest('fail');
}

self::$driver->quit();
}
}
?>
}