Behat / Mink / Selenium2无法访问页面上的元素

时间:2018-04-19 05:38:26

标签: php selenium-webdriver behat mink

我正在尝试通过Behat docs Wikipedia示例。 Goutte按预期工作,但当我尝试使用硒2时,我得到了 Field() Form field with id|name|label|value|placeholder "search" not found. (Behat\Mink\Exception\ElementNotFoundException)

这是有问题的功能
Scenario: Searching for a page with autocompletion Given I am on "/wiki/Main_Page" When I fill in "search" with "Behavior Driv" And I wait for the suggestion box to appear Then I should see "Behavior Driven Development"

my behat.yml

default: extensions: Behat\MinkExtension: browser_name: firefox base_url: http://en.wikipedia.org #goutte: ~ selenium2: browser: firefox 当我从“搜索”更改要搜索的值时,我得到相同的错误。

更新:composer.json { "name": "company/behat", "description": "test project for behat", "authors": [ { "name": "Anonymous", "email": "anon@anon.com.au" } ], "require": { "behat/behat": "^3.3", "behat/mink-goutte-driver": "^1.2", "behat/mink-selenium2-driver": "^1.3", "behat/mink-extension": "^2.3", "behat/mink": "^1.7", "peridot-php/webdriver-manager": "dev-master" }, "config": { "bin-dir": "bin/" } }

1 个答案:

答案 0 :(得分:0)

您确定自己在“/ wiki / Main_Page”页面上吗? (愚蠢的问题但你永远不知道......)

我只能提出一些调试功能或调试工具

  • TightVNC这是一个非常有用的工具,可以让你的Scenario运行起来。您只需插入您的selenium URL(默认密码为'secret')。
  • 我在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;
}

``