如何在没有thread.sleep和testng等待的情况下降低selenium自动化测试的速度

v8wbuo2f  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(270)

这个问题在这里已经有答案了

python和selenium:driver.implicitly\u wait()和time.sleep()之间的区别(3个答案)
在selenium中使用隐式等待(3个答案)
如何通过selenium正确配置隐式/显式等待和pageloadtimeout(3个答案)
上个月关门了。
我是java新手,我正在使用selenium和junit来自动化web应用程序。我们的网站需要5-8秒来加载它的预生产环境,因此我在我的方法中使用了thread.sleep。我觉得这不是一个好的选择,因此需要一个代码,这将减缓自动化与控制流的执行步骤。我也没有时间安装testng来使用隐式和显式等待。一个变量来减慢方法的速度就行了,但是怎么做呢?

zz2j4svz

zz2j4svz1#

显式和隐式等待不是testng特性,testng只是用作控制测试流和Assert的测试框架。
进口:

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());

相关问题