何时使用隐式等待以及在哪里使用 [英] When To Use Implicit wait and where to Use

查看:374
本文介绍了何时使用隐式等待以及在哪里使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Selenium Webdriver提供的隐式等待方法有些困惑.

  1. 何时使用隐式等待

    a-用于页面加载(使用driver.get时)或用于Ajax PopUp加载就像我说我在编辑框"中输入某些内容,并且正在进行某些查找"或Ajax调用.

  2. 在哪里使用隐式等待

    我应该在所有方法中使用Ajax调用或Page加载发生的任何地方,或者仅在一次就足够的情况下使用(我只是从Selenium RC中获取引用,在这里我们可以使用Selenium.SetSpeed方法).

谢谢, 阿伦

解决方案

  1. 显式等待是您定义的代码,用于在继续执行代码之前先等待特定条件发生.最坏的情况是Thread.sleep(),它将条件设置为要等待的确切时间段.提供了一些方便的方法,可以帮助您编写仅等待所需时间的代码. WebDriverWaitExpectedCondition结合是实现此目的的一种方法.示例如下:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
    

但是,根据语言实现的不同而有所不同.请参见此处有关ExpectedCondition

  1. 一个隐含的等待是告诉WebDriver在尝试查找一个或多个元素(如果它们不立即可用)时轮询DOM一定时间.默认设置为0.设置后,将在WebDriver对象实例的生存期内设置隐式等待.下面是隐式等待的实现:

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

这两个定义均来自 seleniumhq 以及最完美的定义.

toolsQA 中有一个很好的解释.如何以及何时使用它们.加上隐式,显式和FLUENT 等待之间的比较,值得一看.

I am having some confusion regarding Implicit wait method provided by Selenium Webdriver.

  1. When to Use Implicit wait

    a- For Page Load (when Using driver.get) or for Ajax PopUp Load Like let say I am entring something in Edit Box and some Look up or Ajax call is happening.

  2. Where To Use Implicit wait

    Should I use after all the methods wherever Ajax call or Page load happening or only once it is enough (I am just taking the reference from Selenium RC where We can Use Selenium.SetSpeed Method).

Thanks, Arun

解决方案

  1. An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.Example is as follows:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
    

However, depending on the language implementation varies a little bit. See here more about ExpectedCondition

  1. An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance. Below is an implementation of implicit wait:

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

Both of these definitions are from seleniumhq and most perfect definition out there.

There is a great explanation in toolsQA how and when to use them. Plus a comparison between implicit, explicit and FLUENT waits which are worth taking a look.

这篇关于何时使用隐式等待以及在哪里使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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