gpt4 book ai didi

java - 不稳定的测试 Selenium

转载 作者:行者123 更新时间:2023-11-30 09:11:24 27 4
gpt4 key购买 nike

在我的时区下午好。

我开始使用 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

,请问这怎么可能?

提前致谢最好的问候

最佳答案

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com