Skip to content

Instantly share code, notes, and snippets.

@magickatt
Last active July 10, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magickatt/cec9cf4e56917bd655fb to your computer and use it in GitHub Desktop.
Save magickatt/cec9cf4e56917bd655fb to your computer and use it in GitHub Desktop.
Upload a screenshot of a failed Behat step to Imgur
<?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;
}
@dstorozhuk
Copy link

where to get client id ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment