How can i click a span in Behat?

Robbin Voortman picture Robbin Voortman · Nov 11, 2015 · Viewed 10.7k times · Source

I am using Behat to test an third-party webshop. I have a item in the shoppingcart that i want to delete. A confirmation pop-up shows that asks me if i really want to do it. The structure of this dialog looks as following:

<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>

I was able to select the span using xpath with the following code:

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();
}

The selecting works, but Behat seems to be unable to click the span.

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

I need to click this item, how can i rewrite my function so that it can be clicked?

Answer

Willem Bressers picture Willem Bressers · Nov 12, 2015

The answer from @bentcoder doesn't make any different. It uses a different a selector to find the element, but the Minkcontext click functionality doesn't support clicking on span elements.

Which i find quite strange, because with jQuery you can add the button class to and span element and there is your button.

Context code:

/**
 * @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 output:

  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)

I'm assuming that you have have a Behat driver for interpreting javascript. So i've added @javascript to the feature:

like so:

@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