Can I make Behat/ZombieJS/Mink simulate a click on a “non-link” element, to fire JS listeners?

ε祈祈猫儿з 提交于 2019-12-01 05:42:43

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

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

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

}
user3540559

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!