如何通过Selenium正确配置隐式/显式等待和pageLoadTimeout? [英] How to properly configure Implicit / Explicit Waits and pageLoadTimeout through Selenium?

查看:2259
本文介绍了如何通过Selenium正确配置隐式/显式等待和pageLoadTimeout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前具有以下设置,但不确定我的等待(Implicit和pageLoadTimeout)是否正常. 这是正确的实现吗?通过将其放在@Before("@ setup")中,它是否适用于每次运行场景或步骤定义?每次我调用@ Given,@ When..etc时,驱动程序都会相应地等待吗?

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


    }

为什么必须将WebElement分配给以下等待, WebElement元素会收到什么?这是正确的实施方式吗? -

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() 是要告诉 WebDriver 实例,即 driver 来轮询 HTML DOM 在一段时间内尝试查找一个或多个元素(如果不是立即可用)时.默认等待配置设置为 0 .设置后,在 WebDriver 对象实例的生存期内设置隐式等待.

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

您会在在硒中使用隐式等待 a>

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

在这里您可以在 Selenium中的pageLoadTimeout无法正常工作中找到详细的讨论

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

注意:除非除非 Test Specification 明确提及相同内容,否则请尽量避免配置pageLoadTimeout().

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


为什么要使用WebDriverWait?

现代浏览器使用 JavaScript React Native 动态加载网页中元素的位置.因此,在继续执行下一行代码之前,要等待特定条件得到满足显式等待,即 WebDriverWait 是继续进行的方法.


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

在这里您可以找到有关用显式等待(Selenium Webdriver& Java)替换隐式等待

Here you can find a detailed discussion of Replace implicit wait with explicit wait (selenium webdriver & java)

  • 为什么需要将WebElement分配给以下等待时间: WebDriverWait

  • 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.

WebElement元素会收到什么?:根据您在 ExpectedConditions 中将用作 visibilityOfElementLocated() ,则在存在时,将返回 WebElement . "rel =" nofollow noreferrer>网页的DOM树,并且可见. Visibility (可见性)表示元素不仅显示显示,而且其高度和宽度大于 0 .

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.

这是正确的实现吗?:您的实现接近完美,但是最后一行代码(即boolean status = element.isDisplayed();)是多余的,因为一旦visibilityOfElementLocated()返回元素,则该元素为可见(即元素不仅显示 ,而且其高度和宽度大于 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).

这篇关于如何通过Selenium正确配置隐式/显式等待和pageLoadTimeout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆