How can i click a span in Behat?
Asked Answered
A

2

7

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?

Archaeornis answered 11/11, 2015 at 11:24 Comment(0)
L
12

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
Lenticularis answered 12/11, 2015 at 13:26 Comment(1)
"Which i find quite strange" - could this be because clicking an element that is not a link or a button has no effect in non-JavaScript?Kirsten
G
5

The code snippet I use is this:

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

As an example, I would write something like this in my scenario:

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"

I hope this helps.

Goods answered 8/12, 2015 at 12:26 Comment(1)
A little late on the party: i gave this one a try too and it works perfectly fine too!Archaeornis

© 2022 - 2024 — McMap. All rights reserved.