Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a proved mouseOver workaround for FirefoxDriver in Selenium2?

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

like image 389
Juan Paredes Avatar asked Jun 03 '11 20:06

Juan Paredes


2 Answers

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();
like image 89
Andy Tinkham Avatar answered Sep 22 '22 17:09

Andy Tinkham


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);
}
like image 29
Markus Heberling Avatar answered Sep 21 '22 17:09

Markus Heberling