How can I click a space in Behat?

I use Behat to test a third-party online store. I have an item in my shopping cart that I want to delete. A confirmation pop-up window shows that it asks me if I really want to do this. The structure of this dialog box is as follows:

<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 a range 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 selection works, but Behat can't seem to click the spacebar.

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

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

+6
source share
3 answers

I assume you have a Behat driver for interpreting javascript. So I added @javascript to this function:

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 
+1
source

The answer from @bentcoder is no different. It uses a different selector to search for an element, but the Minkcontext click function does not support clicking on span elements.

Which I find rather strange, because with jQuery you can add a button 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) 

UPDATE

I forgot to add @javascript to my script.

+10
source

Used snippet:

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

 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" 

Hope this helps.

+5
source

All Articles