如何点击Behat中的跨度?

时间:2015-11-11 11:24:33

标签: html testing click behat mink

我正在使用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

我需要点击这个项目,如何重写我的功能以便点击它?

3 个答案:

答案 0 :(得分:11)

@bentcoder的答案并没有任何不同。它使用不同的选择器来查找元素,但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添加到我的方案中..

答案 1 :(得分:5)

我使用的代码片段是:

/**
 * @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"

我希望这会有所帮助。

答案 2 :(得分:1)

我假设你有一个Behat驱动程序来解释javascript。所以我已将@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