0

I have a menu like structure, where hovering on a menu item opens a sub menu after some delay. I have problems to web test this menu using deterministic methods (= no usage of Thread.Sleep) with Selenium.

  • To hover on an element I need to use Selenium's Actions builder class (MoveToElement)
  • To wait for the sub menu to be visible I need to use a WebDriverWait.Until(d => subMenuWebElement.Displayed).

How to combine these two approaches? I haven't found a way to add the WebDriverWait.Until call to the Actions object. What is the recommended way to solve my problem?

I have found various other threads on SOF, however, they either solve only one of the two bullet points above, or have no working answers (e.g. Selenium WebDriver MoveToElement - hidden element, hover and toggleClass).

Hope somebody can help :-)

Community
  • 1
  • 1
D.R.
  • 20,268
  • 21
  • 102
  • 205

2 Answers2

0

You can split the action builder. There is no need to do it as one action.

Build and perform the action to hover. Use the webdriver wait. Then build and perform a new action

Robbie Wareham
  • 3,380
  • 1
  • 20
  • 38
  • I can't see how this should work. I read, that in order to let the mouse hover on an element for a longer time, all the actions must be bundled into a single `Actions` builder. – D.R. Aug 10 '14 at 15:44
  • Wherever you read that, it is wrong. I have actions which require a mouse over to wait 0.15 until the application registers it. Here, I have a builder for mouse over which is then performed, then a sleep, then another builder for the subsequent action. Have you tried seperating your builders? – Robbie Wareham Aug 11 '14 at 02:45
0

try to involve JavascriptExecutor

step #1

String menu_selector="abracadabra";

((JavascriptExecutor)driver).executeScript("$(\'"+menu_selector+"\').hover();");

note: this one works for you provided jQuery is supported.

step #2 then involve fluentWait to wait submenu to appear:

String subMenu_selector="blablabla";


fluentWait(By.cssSelector(subMenu_selector));


   public WebElement fluentWait(final By locator) {
/*
fluent wait impementation documentation:
        http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html
*/
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

                .withTimeout(10, TimeUnit.SECONDS)
                .pollingEvery(10, TimeUnit.MILLISECONDS)

//                .ignoring(NoSuchElementException.class);
                .ignoring(org.openqa.selenium.NoSuchElementException.class);

        WebElement foo = wait.until(
                new Function<WebDriver, WebElement>() {
                    public WebElement apply(WebDriver driver) {
                        return driver.findElement(locator);
                    }
                }
        );
        return foo;
    }

    ;
eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
  • Using JavaScript should be avoided if trying to test a website. A user cannot directly fire JavaScript therefore the test is not verifying a user can interact with the AUT. You should try and use Selenium interacts and fallback on JavaScript execution as a last resort – Robbie Wareham Aug 11 '14 at 02:48