selenium 隐式等待如何使用,如何通过Selenium正确配置隐式/显式等待和pageLoadTimeout?...

I currently have the following setup, but I'm not sure that my waits (Implicit and pageLoadTimeout) are working. Is this the proper implementation? By putting it in the @Before("@setup"), does it work for every Scenario or Step Definition run? Will the driver wait accordingly, everytime I call a @Given, @When..etc?

@Before("@setup")

public void setUp() {

driver.manage().deleteAllCookies();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

}

Why is it necessary to assign a WebElement to the following wait ,

what does WebElement element receive? Is this the right implementation? -

WebDriverWait wait = new WebDriverWait(driver, 30);

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));

boolean status = element.isDisplayed();

解决方案

implicitlyWait()

implicitlyWait() is to tell the WebDriver instance i.e. driver to poll the HTML DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default wait configuration is set to 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Your code trial is just perfect as in:

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Here you will find a detailed discussion in Using implicit wait in selenium

pageLoadTimeout()

pageLoadTimeout() sets the timespan to wait for a page load to be completed before throwing an error.

Your code trial is just perfect as in:

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

Here you can find a detailed discussion in pageLoadTimeout in Selenium not working

Note : Try to avoid configuring pageLoadTimeout() until and unless the Test Specification explicitly mentions about the same.

Why WebDriverWait?

Modern browsers uses JavaScript, AJAX and React Native where elements within an webpage are loaded dynamically. So to wait for a specific condition to be met before proceeding for the next line of code Explicit Waits i.e. WebDriverWait is the way to proceed ahead.

Note : As per the official documentation of Explicit and Implicit Waits Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.

Your code trial is just perfect to wait for the visibility of an element as in:

WebDriverWait wait = new WebDriverWait(driver, 30);

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));

Your specific questions

Why is it necessary to assign a WebElement to the following wait : WebDriverWait in conjunction with ExpectedConditions not only returns a WebElement but depending on the ExpectedConditions can return void, Boolean, List too.

What does WebElement element receive? : As per your code block where you have used ExpectedConditions as visibilityOfElementLocated(), the WebElement will be returned once the element is present on the DOM Tree of the webpage and is visible. Visibility means that the elements are not only displayed but also has a height and width that is greater than 0.

Is this the right implementation? : Your implementation was near perfect but the last line of code i.e. boolean status = element.isDisplayed(); is redundant as visibilityOfElementLocated() returns the element once the element is visible (i.e. the elements are not only displayed but also has a height and width that is greater than 0).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值