19

I'm using Selenium Java 2.0b3. I have this code:

...
WebDriver driver = new InternetExplorerDriver();
Selenium seleniumDriver = new WebDriverBackedSelenium(driver, "http://localhost:8088/Sistema/");
...
...
RenderedWebElement menuRegistrar = (RenderedWebElement)driver.findElement(By.xpath("//a[normalize-space()='Registrar']"));
seleniumDriver.mouseOver("//a[normalize-space()='Registrar']"); //makes element visible     
menuRegistrar.click();
seleniumDriver.mouseOut("//a[normalize-space()='Registrar']");
...

Works like a charm with InternetExplorerDriver (with IE 8), but it doesn't with the FirefoxDriver (with Firefox 4). I've tried a lot of things with the code and nothing works. And I must use the FirefoxDriver because the application I'm testing doesn't behave well with IE.

As you might guess, the "Registrar" link is hidden until the mouseOver event triggers.

Any proved workarounds? Thanks for your time...

EDIT: also tried ChromeDriver with Chrome 11. Didn't work either. If there's a workaround that works with Chrome I'll take it!


ANSWER (WORKING CODE with Selenium Java 2.0RC1, Windows 7, Firefox 4): Thanks to Andy Tinkham and Luke Inman-Semerau:

//get the element that shows menu with the mouseOver event
WebElement menu = driver.findElement(By.xpath("//div[@id='nav']/li[3]"));

//the element that I want to click (hidden)
WebElement menuOption = driver.findElement(By.xpath("//a[normalize-space()='Registrar']"));

//build and perform the mouseOver with Advanced User Interactions API
Actions builder = new Actions(driver);    
builder.moveToElement(menu).build().perform();

//then click when menu option is visible
menuOption.click();

NOTE: The Advanced User Interaction API uses NativeEvents on the browsers (which is not supported cross platform). So this code might not work just like that if you change the OS. That's why I added the OS and browser detail. See question in selenium users group

ddavison
  • 28,221
  • 15
  • 85
  • 110
Juan Paredes
  • 769
  • 4
  • 12
  • 22

3 Answers3

18

I would suggest trying the Advanced User Actions API that was added in the 2.0rc1 release yesterday, as it looks like you're using the Selenium 1 API still (going through WebDriverBackedSelenium), and I'm not sure how much Firefox 4 support that provides. I'm not using Java for my Selenium tests, but it looks to me like what you would want to do is something like this:

   Actions builder = new Actions(driver); // Or maybe seleniumDriver? Not sure which one to use

   Actions hoverOverRegistrar = builder.moveToElement(menuRegistrar);

   hoverOverRegistrar.perform();
Jonik
  • 80,077
  • 70
  • 264
  • 372
Andy Tinkham
  • 1,329
  • 8
  • 9
  • Thanks for pointing me to the 2.0rc1! I knew I was using Selenium 1 API because I had already tried with the Selenium 2 API and I kept getting the UnsopportedOperationException (or something like that). But the Advanced User Actions with the rc1 worked perfectly! The working code is in the final edit of the question. Thanks a lot for your time! – Juan Paredes Jun 06 '11 at 14:43
  • 1
    Just let me add one comment about the builder (the example is ok nevertheless): the builder pattern will always return the same object with the additional feature added, so creating a new variable doesn't make sense it looks like the builder is immutable. Either use Actions builder = new ...; builder.move ...; or just use one statement and add the features after the new – Alex Lehmann Feb 23 '12 at 09:33
4

I use this code to get a mouseover event for a specific webelement. It does not need native events.

protected void mouseOver(WebElement element) {
    String code = "var fireOnThis = arguments[0];"
                + "var evObj = document.createEvent('MouseEvents');"
                + "evObj.initEvent( 'mouseover', true, true );"
                + "fireOnThis.dispatchEvent(evObj);";
    ((JavascriptExecutor) driver).executeScript(code, element);
}
Markus Heberling
  • 835
  • 13
  • 18
  • This example works great, Thank you very much @MarkusHeberling !! I am using selenium 1, ff version 3.6 – dav Dec 06 '12 at 08:00
  • After recent versions of Firefox no longer provides it's native events API, this method has worked great for me! – testphreak May 29 '15 at 05:07
1
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
action.moveByOffset(1, 1).build().perform();
Madan
  • 141
  • 2
  • 10