开发者

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.fin开发者_JAVA技巧dElement(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


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();


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);
}


Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
action.moveByOffset(1, 1).build().perform();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜