【问题标题】:Unstable Tests Selenium不稳定的测试硒
【发布时间】:2014-02-28 15:24:59
【问题描述】:

在我的时区下午好。

我开始使用 Selenium 来测试我的 Web 应用程序。我正在使用带有 IEDriverServer.exe 的 WebDriver API。操作系统 -> Windows XP 主要问题是测试不稳定。有时它们运行,有时它们抛出异常。 例如,这是测试不稳定的常见地方。 我必须打开一个新窗口并开始填写一些字段。

    driver.findElement(By.xpath("//input[@name='"+button+"' and @type='button']")).click();//BUTTON THAT OPENS THE NEW WINDOW
                    long initDate = System.currentTimeMillis();
                    while(driver.getWindowHandles().size() <= numberPopUps){
                        Thread.sleep(500);
                        //15 seconds waiting for the pop-up
                        if((System.currentTimeMillis() - initDate) > 15000){
                            throw new Exception("Timeout to open popup");
                        }
                    }
        for(String winHandle : driver.getWindowHandles()){
                    if(!winHandle.equals(mWindow)){
                        driver.switchTo().window(winHandle);
                        break;
                    }   
                }
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);//WAIT THAT THE PAGE COMPLETELY LOAD       

    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='descricaoMov']")));//VERIFY IF THIS INPUT IS ON THE DOM

`driver.findElement(By.xpath("//input[@name='descricaoMov']")).sendKeys("TESTE SELENIUM");`//This is where sometimes the test throws exception saying that is unable to find this element

,我想问这怎么可能?

提前致谢 最好的问候

【问题讨论】:

  • 我想到了一些事情。您的 while 循环只是从 WebDriverWait/FluentWait 复制代码 - 它是为在这种情况下“等待”而设计的。其次,什么版本的IE?三是什么版本的IE驱动?另外,您遇到了什么错误?每次都是一样的吗?如果没有,是什么?它多久失败一次?每2次运行?每 10 次运行?最后,您是否正确设置了保护模式设置?
  • Ie 版本 8 IE DRiver 版本 2.39 发生错误时总是相同的“未找到 WebElement 'input[@name='descricaoMov''” 我没有配置任何保护模式设置,因为在IEDriver 页面规范没有说明任何与 Windows XP 相关的内容,只针对高级版本

标签: java selenium xpath selenium-webdriver


【解决方案1】:

你在这里重复了你的努力。 wait.until 行与下一行所做的完全相同,但 .sendKeys() 除外。试试这个:

WebElement descriaoMov = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='descricaoMov']")));
descriaoMov.sendKeys("TEST SELENIUM");

此外,CSS 选择器在查找元素方面比 XPath 更好。我建议将上面的 xpath 部分更改为:

By.cssSelector("input[name='descriaoMov']")

【讨论】:

  • 请记住,仅仅因为一个元素可能位于 DOM 中,它可能并不总是能够与之交互。如果您的测试在您实际向元素发送键之前继续进行,您也会抛出错误。您可以添加另一个等待,直到元素可见,而不仅仅是加载到 DOM 中。 Thread.Sleep(xx) 的替代方法是,但我会使其更具动态性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 2016-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
  • 2013-11-19
相关资源
最近更新 更多