Last active
July 10, 2019 13:15
-
-
Save magickatt/cec9cf4e56917bd655fb to your computer and use it in GitHub Desktop.
Upload a screenshot of a failed Behat step to Imgur
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @AfterStep | |
*/ | |
public function takeScreenshotAfterFailedStep($event) | |
{ | |
if ($event->getTestResult()->getResultCode() === \Behat\Testwork\Tester\Result\TestResult::FAILED) { | |
$driver = $this->getSession()->getDriver(); | |
if ($driver instanceof \Behat\Mink\Driver\Selenium2Driver) { | |
$stepText = $event->getStep()->getText(); | |
$imageTitle = preg_replace('#[^a-zA-Z0-9\._-]#', '', $stepText); | |
$screenshotUri = $this->uploadScreenshot($imageTitle, $driver->getScreenshot()); | |
print "Screenshot for '{$stepText}' uploaded to $screenshotUri"; | |
} | |
} | |
} | |
/** | |
* Upload a screenshot to Imgur | |
* @param string $title | |
* @param string $imageData | |
*/ | |
private function uploadScreenshot($title, $imageData) | |
{ | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => "https://api.imgur.com/3/image", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => "image=".urlencode(base64_encode($imageData))."&title=$title", | |
CURLOPT_HTTPHEADER => array( | |
"authorization: Client-ID yourclientidgoeshere", | |
"cache-control: no-cache", | |
"content-type: application/x-www-form-urlencoded" | |
), | |
)); | |
$response = curl_exec($curl); | |
$error = curl_error($curl); | |
curl_close($curl); | |
$payload = json_decode($response); | |
if ($error || property_exists($payload, 'error')) { | |
return; | |
} | |
return $payload->data->link; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where to get client id ?