Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 无法使用Behat+;在chrome浏览器上执行登录功能;Drupal 8_Selenium_Selenium Webdriver_Selenium Chromedriver_Drupal 8_Behat - Fatal编程技术网

Selenium 无法使用Behat+;在chrome浏览器上执行登录功能;Drupal 8

Selenium 无法使用Behat+;在chrome浏览器上执行登录功能;Drupal 8,selenium,selenium-webdriver,selenium-chromedriver,drupal-8,behat,Selenium,Selenium Webdriver,Selenium Chromedriver,Drupal 8,Behat,只是在Drupal8中使用了Behat,并且在运行登录Behat脚本时遇到了问题。以下是我的代码: 行为 default: suites: default: contexts: - FeatureContext - Drupal\DrupalExtension\Context\DrupalContext - Drupal\DrupalExtension\Context\MinkContext - Drupal

只是在Drupal8中使用了Behat,并且在运行登录Behat脚本时遇到了问题。以下是我的代码:

行为

default:
  suites:
    default:
      contexts:
        - FeatureContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
        - Drupal\DrupalExtension\Context\MessageContext
        - Drupal\DrupalExtension\Context\DrushContext
  extensions:
    Behat\MinkExtension:
      goutte: ~
      base_url: http://localhost:8080/drupal-dev/web
      javascript_session: selenium2
      browser_name: 'chrome'
      selenium2: ~
    Drupal\DrupalExtension:
      blackbox: ~
      api_driver: drupal
      drupal:
        drupal_root: web/
      region_map:
        navigation: ".navbar-header"
        navigation_collapsible: "#navbar-collapse"
        header: ".region-header"
        highlighted: ".highlighted"
        help: ".region-help"
        content: ".region-content"
        sidebar_first: ".region-sidebar-first"
        sidebar_second: ".region-sidebar-second"
        footer: ".footer"
login.feature

@javascript
Feature: login check

Scenario: user login is working
  Given I am on "/user"
  When I fill in "john" for "edit-name"
  And I fill in "john for "edit-pass"
  And I press "Log in"
  Then I should see "Add content"
当我执行vendor\bin\behat时,它将打开chrome浏览器并重定向到用户登录页面,但从第二步开始失败。请参阅以下错误消息:

@javascript
Feature: login check

  Scenario: user login is working             # features\bootstrap\login.feature:4
    Given I am on "/user"                     # Drupal\DrupalExtension\Context\MinkContext::visit()
    When I fill in "john" for "edit-name"   # Drupal\DrupalExtension\Context\MinkContext::fillField()
      Form field with id|name|label|value|placeholder "edit-name" not found. (Behat\Mink\Exception\ElementNotFoundException)
    And I fill in "john" for "edit-pass" # Drupal\DrupalExtension\Context\MinkContext::fillField()
    And I press "Log in"                      # Drupal\DrupalExtension\Context\MinkContext::pressButton()
    Then I should see "Add content"           # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()

--- Failed scenarios:

    features\bootstrap\login.feature:4

1 scenario (1 failed)
5 steps (1 passed, 1 failed, 3 skipped)
0m15.30s (17.82Mb)
如果我将@javasript替换为@api,相同的功能文件工作正常,所有步骤都通过了

如果我将@javasript替换为@api,相同的功能文件工作正常,所有步骤都通过了

Selenium的一个常见问题是,自动浏览器比无头浏览器(在您的例子中是@api,@goutte in Symfony)渲染页面需要更多的时间,因此Behat在尝试查找DOM中尚未渲染的元素时返回错误

解决方案是在尝试填充字段之前添加wait()。您甚至可以在您的上下文中查询驱动程序类型,并仅在类型为Selenium时触发等待

您需要创建自己的上下文文件来添加方法。 创建“MyFeatureContext”(或其他内容),使其继承MinkContext类,将其添加到behat.yml文件中的上下文(而不是MinkContext,因为您仍然从其继承),并添加以下方法:

public function wait(int $ms)
{
    if ($this->getDriver() instanceof Selenium2Driver) {
        return $this->getSession()->wait($ms);
    }
}

/**
 * Fills in form field with specified id|name|label|value
 * Example: When I fill in "username" with: "bwayne"
 * Example: And I fill in "bwayne" for "username"
 *
 * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
 * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with:$/
 * @When /^(?:|I )fill in "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/
 */
public function fillField($field, $value)
{
    $this->wait(1000); # 1000ms or 1sec
    parent::fillField($field, $value);
}
公共函数等待(int$ms)
{
if($this->getDriver()Selenium2Driver实例){
返回$this->getSession()->wait($ms);
}
}
/**
*用指定的id | name | label |值填写表单字段
*示例:当我在“用户名”中填写:“bwayne”
*示例:我在“用户名”中填写“bwayne”
*
*@When/^(?:| I)用“(?P(?:[^“]\\”)填充“(?P(?:[^“]\\”)*”)$/
*@When/^(?:| I)在“(?P(?:[^“]\\”*”)中填入:$/
*@When/^(?:| I)在“(?P(?:[^“]\\”)中填写“(?P(?:[^“]\\”)*”)$/
*/
公共函数fillField($field,$value)
{
$this->等待(1000)#1000毫秒或1秒
父::fillField($field,$value);
}

请注意,注释是从MinkContext::fillField()复制的。

谢谢Olivier!刚刚通过将selenium server版本从3.1更改为2.53解决了这个问题,我认为您需要让behat等待,然后再尝试填充该字段。这应该在上下文文件中完成。你能将你的FeatureContext文件添加到你的问题中吗?我会尝试填充等待步骤,但我认为它不起作用,FeatureContext是默认的。谢谢你提供详细信息!我尝试过在featureContext(扩展了MinkContext)中放置wait,甚至在MinkContext的fillField()方法中添加了硬代码,但没有成功。如果我做错了什么,请提出建议。好的,那么你需要缩小问题的范围。wait()至少可以工作吗?或者在behat执行测试时忽略它?将该值更改为类似999999的大值,然后查看behat是否挂起或是否以相同的速度执行测试。此外,在featureContext文件中添加fillField方法并获取错误消息。加载页面时,是否有任何弹出窗口、模式或任何覆盖该字段的内容?它的纯drupal 8安装启用了默认引导主题。