首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在没有Thread.sleep和TestNG等待的情况下降低Selenium自动化测试的速度

如何在没有Thread.sleep和TestNG等待的情况下降低Selenium自动化测试的速度
EN

Stack Overflow用户
提问于 2020-12-08 13:51:06
回答 1查看 188关注 0票数 1

我是Java新手,我正在使用selenium和Junit来自动化web应用程序。我们的网站需要5-8秒来加载它的预生产环境,因此我在我的方法中使用了Thread.sleep。我认为这不是一个好的选择,因此需要一个代码,它将减慢自动化,并在步骤中控制执行流程。另外,我没有时间安装testng来使用隐式和显式等待。使用变量来降低方法的运行速度是可行的,但如何实现呢?

EN

回答 1

Stack Overflow用户

发布于 2020-12-08 14:16:58

显式和隐式的等待不是TestNG的selenium特性,testng仅用作控制测试流程和断言的测试框架。

导入:

代码语言:javascript
运行
复制
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;

代码:

代码语言:javascript
运行
复制
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());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65193785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档