我是Java新手,我正在使用selenium和Junit来自动化web应用程序。我们的网站需要5-8秒来加载它的预生产环境,因此我在我的方法中使用了Thread.sleep。我认为这不是一个好的选择,因此需要一个代码,它将减慢自动化,并在步骤中控制执行流程。另外,我没有时间安装testng来使用隐式和显式等待。使用变量来降低方法的运行速度是可行的,但如何实现呢?
发布于 2020-12-08 14:16:58
显式和隐式的等待不是TestNG的selenium特性,testng仅用作控制测试流程和断言的测试框架。
导入:
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
代码:
WebDriver driver = new ChromeDriver();
driver.get("https://google.com/ncr");
driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
// Initialize and wait till element(link) became clickable - timeout in 10 seconds
WebElement firstResult = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//a/h3")));
// Print the first result
System.out.println(firstResult.getText());
https://stackoverflow.com/questions/65193785
复制相似问题