Php 是否有一种方法可以在网站自动化过程中自动截取故障的屏幕截图?
我所有的自动化脚本都必须在午夜运行,我在第二天早上的报告中看到失败,无法复制,我向开发人员提出的问题被拒绝为不可生产。我想建立一个环境,这将是一个失败的步骤后拍摄网页截图的能力。 *代码是用小黄瓜语言编写的 *我们使用默认的Mink函数和一些定制的PHP函数 *我们使用Gitlab和Gitlab运行程序执行 我不熟悉Behat和PHP。所以,我还没有尝试过任何东西Php 是否有一种方法可以在网站自动化过程中自动截取故障的屏幕截图?,php,behat,gitlab-ci-runner,gherkin,mink,Php,Behat,Gitlab Ci Runner,Gherkin,Mink,我所有的自动化脚本都必须在午夜运行,我在第二天早上的报告中看到失败,无法复制,我向开发人员提出的问题被拒绝为不可生产。我想建立一个环境,这将是一个失败的步骤后拍摄网页截图的能力。 *代码是用小黄瓜语言编写的 *我们使用默认的Mink函数和一些定制的PHP函数 *我们使用Gitlab和Gitlab运行程序执行 我不熟悉Behat和PHP。所以,我还没有尝试过任何东西 理想情况下,我希望看到带有图像名称、特定行或某种唯一标识符的故障截图 图像可以保存到存储库[云/gitlab/本地系统] 添加一个
- 理想情况下,我希望看到带有图像名称、特定行或某种唯一标识符的故障截图李>
- 图像可以保存到存储库[云/gitlab/本地系统]
/**
*@AfterStep
*/
失败步骤后的公共功能截图(AfterStepScope$scope)
{
如果(TestResult::FAILED===$scope->getTestResult()->getResultCode()){
$driver=$this->minkContext->getSession()->getDriver();
if(!$driver instanceof Behat\Mink\driver\Selenium2Driver){
返回;
}
$page=$this->minkContext->getSession()->getPage()->getContent();
$screenshot=$driver->getScreenshot();
$screenshotFileName=日期('d-m-y')。-'.uniqid()。.png';
$pageFileName=date('d-m-y').-'.uniqid()..html';
//注意:硬编码路径:
$filePath=“/var/www/symfony.dev/”;
文件内容($filePath.$screenshotFileName,$screenshot);
文件内容($filePath.$pageFileName,$page);
打印“截图位置:”。$filePath.$screenshotFileName.“\n”;
打印“HTML转储地址:”.$filePath.$pageFileName.\n”;
}
}
添加一个可以处理该问题的
然后在钩子上你只需要截图
找到一段可能对您有帮助的代码
/**
*@AfterStep
*/
失败步骤后的公共功能截图(AfterStepScope$scope)
{
如果(TestResult::FAILED===$scope->getTestResult()->getResultCode()){
$driver=$this->minkContext->getSession()->getDriver();
if(!$driver instanceof Behat\Mink\driver\Selenium2Driver){
返回;
}
$page=$this->minkContext->getSession()->getPage()->getContent();
$screenshot=$driver->getScreenshot();
$screenshotFileName=日期('d-m-y')。-'.uniqid()。.png';
$pageFileName=date('d-m-y').-'.uniqid()..html';
//注意:硬编码路径:
$filePath=“/var/www/symfony.dev/”;
文件内容($filePath.$screenshotFileName,$screenshot);
文件内容($filePath.$pageFileName,$page);
打印“截图位置:”。$filePath.$screenshotFileName.“\n”;
打印“HTML转储地址:”.$filePath.$pageFileName.\n”;
}
}
Behat中提供了很多现成的扩展,就像您使用Mink库模拟用户与浏览器的交互一样。类似地,还有一个屏幕截图扩展,它可以捕获失败步骤的屏幕截图。您可以在此处找到详细信息-Behat中提供了许多现成的扩展,就像您使用Mink库模拟用户与浏览器的交互一样。类似地,还有一个屏幕截图扩展,它可以捕获失败步骤的屏幕截图。您可以在此处找到详细信息-这为我完成了任务:
public function takeScreenshotAfterFailedStep(AfterStepScope $scope) {
try {
$session = $this->getSession();
if (99 === $scope->getTestResult()->getResultCode()) {
$stepName = $scope->getStep()->getText();
$this->takeScreenshot($stepName);
echo "\n\nYour test may have failed - ";
echo "\nCurrent Page URL: ". $session->getCurrentUrl();
}
} catch(Exception $e) {
$e_msg = "Something went wrong when taking screenshots";
throw new Exception($e_msg, $e->getCode(), $e);
}
}
public function takeScreenshot($stepName=null) {
echo "The current working directory: " . getcwd() . "\n";
$filePath = getcwd(); // default path
date_default_timezone_set('America/New_York');
$fileName = 'screenshot-' . uniqid() . '_' . date('Y-m-d-H-i-s') .'.png';
# if directory path is specified in Behat yml, use it.
if (isset($this->parameters['screen_shot_path'])) {
$filePath = $this->parameters['screen_shot_path'];
}
$this->saveScreenshot($fileName, $filePath);
echo "\nScreenshot taken -> " . $fileName . "\n";
}
这帮我完成了任务:
public function takeScreenshotAfterFailedStep(AfterStepScope $scope) {
try {
$session = $this->getSession();
if (99 === $scope->getTestResult()->getResultCode()) {
$stepName = $scope->getStep()->getText();
$this->takeScreenshot($stepName);
echo "\n\nYour test may have failed - ";
echo "\nCurrent Page URL: ". $session->getCurrentUrl();
}
} catch(Exception $e) {
$e_msg = "Something went wrong when taking screenshots";
throw new Exception($e_msg, $e->getCode(), $e);
}
}
public function takeScreenshot($stepName=null) {
echo "The current working directory: " . getcwd() . "\n";
$filePath = getcwd(); // default path
date_default_timezone_set('America/New_York');
$fileName = 'screenshot-' . uniqid() . '_' . date('Y-m-d-H-i-s') .'.png';
# if directory path is specified in Behat yml, use it.
if (isset($this->parameters['screen_shot_path'])) {
$filePath = $this->parameters['screen_shot_path'];
}
$this->saveScreenshot($fileName, $filePath);
echo "\nScreenshot taken -> " . $fileName . "\n";
}
谢谢,我会尝试的。您好,它失败了,出现了以下错误:Behat\Testwork\Call\Exception\FatalThrowableError:Type错误:传递给FeatureContext::takeScreenshotAfterFailedStep()的参数1必须是AfterStepScope的实例,Behat\Behat\Hook\Scope\AfterStepScope的实例如果输入Hanks,我将尝试它。您好,它失败,出现此错误Behat\Testwork\Call\Exception\FatalThrowableError:类型错误:传递给FeatureContext::takeScreenshotAfterFailedStep()的参数1必须是AfterStepScope的实例,Behat\Behat\Hook\Scope\AfterStepScope的实例给定了“require dev”:{“Behat/Behat”:“^3.3.1”,“bex/Behat screenshot”默认值:extensions:Bex\Behat\ScreenshotExtension:image\u驱动程序:local:screenshot\u目录:C:./Temp/GitFails清除\u screenshot\u目录:true,但失败,出现以下异常:在ExtensionManager.php第194行:无法找到[Behat\Testwork\ServiceContainer\Exception\ExtensionInitializationException]
Bex\Behat\ScreenshotExtension
扩展文件或类。添加了“需要开发”:{“Behat/Behat”:“^3.3.1”,“Bex/Behat屏幕截图”默认值:extensions:Bex\Behat\ScreenshotExtension:image\u驱动程序:local:screenshot\u目录:C:./Temp/GitFails清除\u screenshot\u目录:true,但失败,出现以下异常:在ExtensionManager.php第194行:[Behat\Testwork\ServiceContainer\Exception\ExtensionInitializationException]Bex\Behat\ScreenshotExtension
无法找到扩展文件或类。我相信这里的问题是我没有使用dockerized版本/Github。我在使用gitlab。如果我错了,请纠正我。我相信这里的问题是我没有使用dockerized版本/Github。我在使用gitlab。如果我错了,请纠正我。