java - 不稳定的测试 Selenium

标签 java selenium xpath selenium-webdriver

在我的时区下午好。

我开始使用 Selenium 来测试我的 Web 应用程序。我正在将 WebDriver API 与 IEDriverServer.exe 结合使用。操作系统 -> 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

,我想问一下,这怎么可能?

提前致谢 最好的问候

最佳答案

您在这里重复了您的努力。 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']")

关于java - 不稳定的测试 Selenium ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22099124/

相关文章:

java - 在将 volatile 引用设置为引用新创建的对象后,线程是否仍然认为构造函数的效果正在发生?

java - 使用 HashMap 的 Set 迭代器不会产生值/键?

python - 开始玩 selenium,通过 css 方法获取问题

xpath - Xpath返回从第n个开始的所有h2节点

javascript - Xpath 函数在剧作家中不起作用

java - 为什么会出现 "Exception in thread "AWT-EventQueue- 0"java.lang.OutOfMemoryError: Java heap space"错误?

java - 适用于 Chrome 的 Selenium Webdriver 未将屏幕截图保存在指定路径中

python - 使用 Selenium 和 ng-file-upload 自动上传文件

python - 获取元素的最小 XPath

java - Android BLE 写入特性锁定 onCharacteristicWrite/onCharacteristicChange