Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell Behat / Mink to hover over an element on a webpage?

Tags:

bdd

behat

mink

I am new to Behat. I am currently using the Mink Extension and the Selenium2 driver and I would like to know how to specify that the test should hover over an element as part of the Scenario.

For example, here is my scenario:

Scenario: Testing that the Contact Us submenu appears
    Given I am on "/"
    When I hover over "About"
    Then I should see "Contact Us"
like image 853
Peter Meth Avatar asked Aug 28 '13 23:08

Peter Meth


1 Answers

I figured it out, based on this answer https://stackoverflow.com/a/17217598 and just replaced the click() with mouseOver().

Here is my FeatureContext code:

/**
 * @When /^I hover over the element "([^"]*)"$/
 */
public function iHoverOverTheElement($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 hover it
        $element->mouseOver();
}

I have to pass the css selector when I use it, so usage looks like:

...
When I hover over the element ".about"
...
like image 84
Peter Meth Avatar answered Oct 22 '22 08:10

Peter Meth