Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jul 10, 2019

where to get client id ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.