如何重置Selenium鼠标光标位置? [英] How can I reset the Selenium mouse cursor position?

查看:848
本文介绍了如何重置Selenium鼠标光标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试javascript网络应用程序,使用Selenium2 WebDriver C#API,其行为取决于鼠标悬停.我有一个扩展方法,可以对Web元素执行悬停以保持代码DRY.

I'm testing a javascript web app with some behavior depending on mouse over using Selenium2 WebDriver C# API. I have an extension method that performs a hover on a web element to keep code DRY.

public static void Hover(this IWebElement webElement, IWebDriver driver)
{
    new Actions(driver).MoveToElement(webElement).Perform();
}

现在,我需要一种将鼠标位置重置"为某些独立于元素的自然默认值的方法.我在TearDown()中称其为测试后将驱动程序保持在已知状态.我能想到的最好的是

Now I want a method that "resets" the mouse position to some natural default value independent of element. I call this in TearDown() to keep the driver in a known state after tests. The best I could come up with is

public static void ResetMouseCursor(IWebDriver driver)
{
    new Actions(driver).MoveByOffset(-9999, -9999).Perform();
}

只是将浏览器放在了无人区.有更好的方法吗?

Which just put's the browser in no-man's land. Is there a better way to do this?

推荐答案

与DOM无关的方法似乎是个坏主意. 上面的代码完全存储在Firefox驱动程序中,用于超出文档范围"异常.

A DOM agnostic method seems like a bad idea. The above code completely tanked in Firefox driver for "going outside of document" exception.

我能想到的最好的事情是将鼠标光标设置到页面上最根元素的(0,0)位置,或(在我的情况下是一个持有单页javascript应用程序的IFrame )并使用继承进行初始化.在C#驱动程序中是这样.

The best thing which I could come up with, is to set the mouse cursor to the (0,0) position of the most root element on the page or (which in my case is an IFrame holding the single page javascript app) and use inheritance to do the initialization. In C# driver this is.

public class RootFrame
{
    [FindsBy(How = How.CssSelector, Using = "#root-id")]
    private IWebElement vfrFrame;
    protected IWebDriver driver;

    public VfrElement(IWebDriver driver)
    {
        this.driver = driver;
        PageFactory.InitElements(driver, this);
    }

    public void ResetMouseCursor()
    {
        new Actions(driver).MoveToElement(vfrFrame, 0, 0).Perform();
    }
}

框架中的所有内容都可以继承该类并访问该方法.

Everything in the frame can just inherinant the class and get access to the method.

这篇关于如何重置Selenium鼠标光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆