phpbddbehatmink

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


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"

Solution

  • 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"
    ...