Can I make Behat/ZombieJS/Mink simulate a click on a "non-link" element, to fire JS listeners?
Asked Answered
B

4

6

I am using Behat for testing, with the ZombieJS driver, and everything is working very well so far, but there is one thing that bothers me, I can't seem to figure out how to get Behat to simulate a click on a non-link element. I guess this comes from Mink being a "cross-browser" driver, and it doesn't really make sense to click on non-link elements. For my use case though, I need it, because there are several elements on the page that have javascript event listeners attached to them, and I want to test these functionalities.

I tried using

Then I follow "div.sa"

But it fails with the expected error:

01. Link with id|title|alt|text "div.sa" not found.

Since it's not a link element. There is a similar step in Behat:

I press "button.class"

But this one is only for buttons.

Is there a way I could get Behat click on elements that are not tags?

Baltazar answered 13/11, 2012 at 17:46 Comment(0)
P
4

Yes I believe so, but you'll need to write a custom step, something along these lines (if you're using a subclass of MinkContext.

/**
 * @When /^I click the something$/
 */
public function iClickTheSomething() 
{
    $something = $this->getSession()
                      ->getPage()
                      ->find("css", "div.sa")
                      ->click();
}
Plant answered 20/11, 2012 at 23:58 Comment(0)
P
12

With some error handling:

/** Click on the element with the provided xpath query
 *
 * @When /^(?:|I )click on the element "([^"]*)"$/
 */
public function iClickOnTheElement($locator)
{
    $session = $this->getSession(); // get the mink session
    $element = $session->getPage()->find('css', $locator); // runs the actual query and returns the element

    // errors must not pass silently
    if (null === $element) {
        throw new \InvalidArgumentException(sprintf('Could not evaluate CSS selector: "%s"', $locator));
    }

    // ok, let's click on it
    $element->click();
}
Pachyderm answered 20/6, 2013 at 15:22 Comment(0)
P
4

Yes I believe so, but you'll need to write a custom step, something along these lines (if you're using a subclass of MinkContext.

/**
 * @When /^I click the something$/
 */
public function iClickTheSomething() 
{
    $something = $this->getSession()
                      ->getPage()
                      ->find("css", "div.sa")
                      ->click();
}
Plant answered 20/11, 2012 at 23:58 Comment(0)
Y
2

This is my adaptation of the answer by Comstar. The same functionality is achieved, however, it seems simpler to me to do it this way.

/**
 * @Then /^I click on "([^"]*)"$/
 */
public function iClickOn($arg1)
{
    $page = $this->getSession()->getPage();

    $findName = $page->find("css", $arg1);
     if (!$findName){
        throw new Exception($arg1." could not be found");
    }
    else {
        $findName->click();
    }

}
Yun answered 14/7, 2015 at 11:19 Comment(0)
M
1

When I execute a click event, so that the current URL be equal to javascript:

$this->iClickOnTheElementWithXPath(‘//div[@class="'.$arg2.'"]/div/div[@class="edit"]/a[text()="'.$arg1.'"]‘);

In the page html:

<a onclick="editForm('Avatar')" href="javascript:;">Modifier</a>
Michaels answered 23/4, 2014 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.