Selenium

时间:2016-02-19 07:03:28

标签: c# asp.net selenium nunit

我正在开展产品自动化(Web CMS),其中 element.Click() 显示不一致的行为。基本上我们正在使用,

Selenium + Nunit GUI(单元测试框架) - 在特定环境中从本地运行测试用例

Selenium + Asp.net网络应用程序 - 多个用户可以在不同环境下运行测试用例 这里的环境我的意思是不同的级别(Dev,SIT,QA,Production)。

我的关注 在我的一个测试用例中,我想点击一个按钮。所以为此,我尝试了很少的代码。但都是不一致的行为。这里不一致我的意思是,无论我为单击按钮编写的代码,只能在我的本地或服务器上工作,反之亦然。

第一次尝试: - 我尝试了所有的元素定位器

IWebElement element = driver.FindElement(By.Id("element id goes here"))
Working fine at my local, but not in server

结果 - 失败

第二次尝试: -

driver.FindElement(By.XPath("Element XPath goes here")).SendKeys(Keys.Enter);

在服务器上工作正常,但在本地工作 结果 - 失败

第3次尝试: -

IWebElement element = driver.findElement(By.id("something"));
    IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
                        executor.ExecuteScript("arguments[0].click()", element);

不兼容(本地和服务器) 结果 - 失败

最后,我尝试等待元素可见并执行操作

第4次尝试: -

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
                return wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("element xpath goes here")));


webdriver等待对该元素 (element.click()) 执行操作后 在本地但在服务器中工作正常 结果 - 失败

我正在寻找一个解决方案 ,其中点击按钮不应该是一种不一致的行为。 基本上它应该在 (本地和服务器) 。非常感谢您的帮助。谢谢 仅供参考 - 我在Mozilla Firefox浏览器38.5.2中进行测试

1 个答案:

答案 0 :(得分:0)

我在Win7上使用Clen本地使用Selenium,使用Firefox浏览器在Win10和MacOS上远程使用Selenium,并且还注意到Firefox有时需要对IWebElement.Click()进行特殊处理。所以我自己编写了一个扩展方法,对我来说很好,不管找到元素的定位器是什么:

public static void Click(this IWebElement element, TestTarget target)
{
    if (target.IsFirefox)
    {
        var actions = new Actions(target.Driver);
        actions.MoveToElement(element);

        // special workaround for the FirefoxDriver
        // otherwise sometimes Exception: "Cannot press more then one button or an already pressed button"
        target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.Zero);
        // temporarily disable implicit wait
        actions.Release().Build().Perform();
        target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(MyDefaultTimeoutInSeconds));

        Thread.Sleep(500);
        actions.MoveToElement(element);
        actions.Click().Build().Perform();
    }
    else
    {
        element.Click();
    }
}

如果您希望测试用例具有更稳定的行为,我建议您使用ChromeDriver。它根本不需要任何特殊处理,而且它也比FirefoxDriver快得多。

相关问题