【问题标题】:How can i click a span in Behat?如何在 Behat 中单击跨度?
【发布时间】:2015-11-11 11:24:32
【问题描述】:

我正在使用 Behat 测试第三方网店。我在购物车中有一个要删除的项目。一个确认弹出窗口显示询问我是否真的想这样做。该对话框的结构如下所示:

<div>
    <strong class="title">Remove item from shoppingcart</strong>

    <p>Are you sure you want to delete this product?</p>

    <div class="button-container">
        <span class="button" data-confirm="true">Yes</span>
        <span class="button alt right" data-mfp-close-link="true">No</span>
    </div>
</div>

我可以通过以下代码使用 xpath 选择跨度:

public function iConfirmTheWindow()
{
  $session = $this->getSession();
  $element = $session->getPage()->find(
  'xpath',
  $session->getSelectorsHandler()->selectorToXpath('css', 'span.button')
  );
  if (null === $element) {
  throw new \InvalidArgumentException(sprintf('Could not find confirmation window'));
  }

  $element->click();
}

选择有效,但 Behat 似乎无法单击跨度。

 supports clicking on links and submit or reset buttons only. But "span" provided

我需要点击这个项目,我该如何重写我的函数以便它可以被点击?

【问题讨论】:

    标签: html testing click behat mink


    【解决方案1】:

    @bentcoder 的回答并没有什么不同。它使用不同的 a 选择器来查找元素,但 Minkcontext click 功能不支持单击 span 元素。

    我觉得这很奇怪,因为使用 jQuery,您可以将 button 类添加到 span 元素,然后就是您的按钮。

    上下文代码:

    /**
     * @Given I click the :arg1 element
     */
    public function iClickTheElement($selector)
    {
        $page = $this->getSession()->getPage();
        $element = $page->find('css', $selector);
    
        if (empty($element)) {
            throw new Exception("No html element found for the selector ('$selector')");
        }
    
        $element->click();
    }
    

    CLI 输出:

      And I click the "#new_account" element # tests/behat/features/account.feature:14
      Behat\Mink\Driver\GoutteDriver supports clicking on links and submit or reset buttons only. But "span" provided (Behat\Mink\Exception\UnsupportedDriverActionException)
    

    我假设您有一个用于解释 javascript 的 Behat 驱动程序。所以我在功能中添加了@javascript

    像这样:

    @javascript
    Scenario: Create new account
      Given I am logged in as "user" user
      And I am on "/user/settings"
      And I click the ".new_account" element
    

    【讨论】:

      【解决方案2】:

      我使用的代码sn-p是这样的:

      /**
       * @Then /^I click on "([^"]*)"$/
       */
      public function iClickOn($element)
      {
          $page = $this->getSession()->getPage();
          $findName = $page->find("css", $element);
          if (!$findName) {
              throw new Exception($element . " could not be found");
          } else {
              $findName->click();
          }
      }
      

      作为一个例子,我会在我的场景中写这样的东西:

      Feature: Test Click
      
      @javascript
      Scenario: Clicking on spans
      Given I go to "http://docs.behat.org/en/v2.5/"
       And wait "3000"
      When I click on "span:contains('behat.yml')"
       And wait "3000"
      Then I should be on "http://docs.behat.org/en/v2.5/guides/7.config.html"
      

      我希望这会有所帮助。

      【讨论】:

      • 聚会有点晚了:我也试了一下,效果也很好!
      猜你喜欢
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2020-02-13
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多