你确定你在“/wiki/Main_Page”页面上吗?
(愚蠢的问题,但你永远不知道......)
我只能建议你一些调试功能或工具来调试
-
TightVNC 一个非常有用的工具,可以在线查看您的场景运行。您只需插入您的 selenium 网址(默认密码为“秘密”)。
- 我在 FeatureContext 中添加了一些函数:
``
/**
* afterTheStep
*
* If the current step is failing, then print the html code of
* the current page and, if the driver is an instance of
* Selenium2Driver, print a screenshot of the current page.
*
* @AfterStep
*
* @param AfterStepScope $scope
* @return BaseFeatureContext
*/
public function afterTheStep(AfterStepScope $scope): ?self
{
if (99 !== $scope->getTestResult()->getResultCode()) {
return null;
}
$filePath = __FILE__ . '../debug/behat/';
// override if is set into behat.yml
$fileName = date('d-m-y').'_'.basename($scope->getFeature()->getfile()).'_'.hash('md5', $scope->getStep()->getText());
$this->takeScreenshot($filePath, $fileName);
$this->takePageContent($filePath, $fileName);
$this->getErrorPosition($filePath, $fileName, $scope);
return $this;
}
/**
* takeScreenshot
*
* save a screenshot of the current page if the driver is
* an instance of Selenium2Driver
*
* @param string $filePath
* @param string $fileName
* @return BaseFeatureContext
*/
private function takeScreenshot(string $filePath, string $fileName): self
{
$driver = $this->getSession()->getDriver();
if (!$driver instanceof Selenium2Driver) {
return $this;
}
$filePath = $filePath.'screenshot/';
$extension = '.png';
$fullPath = $filePath.$fileName.$extension;
if (!file_exists($filePath)) {
mkdir($filePath, 0777, true);
}
if (file_exists($fullPath)) {
unlink($fullPath);
}
$this->saveScreenshot($fileName.$extension, $filePath);
echo(sprintf(
"Result screenshot at: %s \n",
$fullPath
));
return $this;
}
/**
* takePageContent
*
* save the html code of the current page
*
* @param string $fileName
*/
private function takePageContent(string $filePath, string $fileName): self
{
$filePath = $filePath.'report/';
$extension = '.txt';
$fullPath = $filePath.$fileName.$extension;
if (!file_exists($filePath)) {
mkdir($filePath, 0777, true);
}
if (file_exists($fullPath)) {
unlink($fullPath);
}
file_put_contents($fullPath, $this->getSession()->getPage()->GetContent());
echo(sprintf(
"Result HTML page content at: %s \n",
$fullPath
));
return $this;
}
/**
* getErrorPosition
*
* print the position of the error
*
* @param string $filepath
* @param string $filename
* @param AfterStepScope $scope
*/
private function getErrorPosition(string $filePath, string $fileName, AfterStepScope $scope): self
{
// eval(\Psy\sh());
$filePath = $filePath.'position/';
$extension = '.txt';
$fullPath = $filePath.$fileName.$extension;
$fileContent = sprintf(
"Error in file \n'%s'\n
Title: '%s'\n
Description:\n'%s'\n
Line %s : '%s'\n
Exception:\n'%s'\n",
$scope->getFeature()->getFile(),
$scope->getFeature()->getTitle(),
$scope->getFeature()->getDescription(),
$scope->getStep()->getLine(),
$scope->getStep()->getText(),
$scope->getTestResult()->getException()->getMessage()
);
if (!file_exists($filePath)) {
mkdir($filePath, 0777, true);
}
if (file_exists($fullPath)) {
unlink($fullPath);
}
file_put_contents($fullPath, $fileContent);
echo(sprintf(
"Position of error at: %s \n",
$fullPath
));
return $this;
}
``