From 05761ff3506938a4766725be68703e7da38e43c1 Mon Sep 17 00:00:00 2001 From: Wagner Silva Date: Mon, 11 Dec 2017 13:23:26 +0000 Subject: [PATCH 1/2] Automatically score tests --- lib/Api.php | 106 +++++++++++++++++++++++++++++++++++++++++++++ lib/CBTContext.php | 58 +++++++++++++++++++------ 2 files changed, 151 insertions(+), 13 deletions(-) create mode 100644 lib/Api.php diff --git a/lib/Api.php b/lib/Api.php new file mode 100644 index 0000000..688d2b6 --- /dev/null +++ b/lib/Api.php @@ -0,0 +1,106 @@ + + * @link + * @version 1.0 + */ + +namespace Drewberry; + +require 'vendor/autoload.php'; + +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']); + } +} \ No newline at end of file diff --git a/lib/CBTContext.php b/lib/CBTContext.php index fed917f..63ac0a3 100644 --- a/lib/CBTContext.php +++ b/lib/CBTContext.php @@ -1,49 +1,81 @@ $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(); } -} -?> +} \ No newline at end of file From 795f442b5f30b888cce91dae314473ab8b5f750d Mon Sep 17 00:00:00 2001 From: Wagner Silva Date: Thu, 14 Dec 2017 16:40:05 +0000 Subject: [PATCH 2/2] Correcting namespace and author --- lib/Api.php | 8 +++----- lib/CBTContext.php | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/Api.php b/lib/Api.php index 688d2b6..d794e20 100644 --- a/lib/Api.php +++ b/lib/Api.php @@ -2,14 +2,12 @@ /** * Drewberry™ * - * @author Wagner Silva - * @link + * @author Wagner Silva + * @link * @version 1.0 */ -namespace Drewberry; - -require 'vendor/autoload.php'; +namespace CBT; class Api { diff --git a/lib/CBTContext.php b/lib/CBTContext.php index 63ac0a3..1918cfb 100644 --- a/lib/CBTContext.php +++ b/lib/CBTContext.php @@ -25,7 +25,7 @@ public function __construct($parameters) $GLOBALS['CBT_URL'] = $GLOBALS['CONFIG']['base_url']; - self::$api = new Drewberry\Api($parameters['cbt']['user'], $parameters['cbt']['key']); + self::$api = new CBT\Api($parameters['cbt']['user'], $parameters['cbt']['key']); } /** @BeforeFeature */