Implicit & Explicit Waits in Selenium
来源:互联网 发布:用数据脱恐艾滋病 编辑:程序博客网 时间:2024/05/21 10:34
Implicit & Explicit Waits in Selenium
- Details
- Last Updated: Friday, 11 March 2016 07:09
In selenium "Waits" play an important role in executing tests. In this tutorial, you will learn various aspects of both "Implicit" and "Explicit" waits in Selenium.
In this tutorial, you will learn-
Why Do We Need Waits In Selenium?
Implicit Wait
Explicit Wait
Fluent Wait
Why Do We Need Waits In Selenium?
Most of the web applications are developed using Ajax and Javascript. When a page is loaded by the browser the elements which we want to interact with may load at different time intervals.
Not only it makes this difficult to identify the element but also if the element is not located it will throw an "ElementNotVisibleException" exception. Using Waits, we can resolve this problem.
Let's consider a scenario where we have to use both implicit and explicit waits in our test. Assume that implicit wait time is set to 20 seconds and explicit wait time is set to 10 seconds.
Suppose we are trying to find an element which has some "ExpectedConditions "(Explicit Wait), If the element is not located within the time frame defined by the Explicit wait(10 Seconds), It will use the time frame defined by implicit wait(20 seconds) before throwing an "ElementNotVisibleException".
Selenium Web Driver Waits
- Implicit Wait
- Explicit Wait
Implicit Wait
Selenium Web Driver has borrowed the idea of implicit waits from Watir.
The implicit wait will tell to the web driver to wait for certain amount of time before it throws a "No Such Element Exception". The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.
In the below example we have declared an implicit wait with the time frame of 10 seconds. It means that if the element is not located on the web page within that time frame, it will throw an exception.
To declare implicit wait:
Syntax:
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
Explanation of Code
In the above example,
Code line19: Implicit wait will accept 2 parameters, the first parameter will accept the time as an integer value and the second parameter will accept the time measurement in terms of SECONDS, MINUTES, MILISECOND, MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc.
//declaring implicit wait driver.manage().timeouts.implicitWait(10,TimeUnit.SECONDS);
Explicit Wait
The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an "ElementNotVisibleException" exception.
The explicit wait is an intelligent kind of wait, but it can be applied only for specified elements. Explicit wait gives better options than that of an implicit wait as it will wait for dynamically loaded Ajax elements.
Once we declare explicit wait we have to use "ExpectedCondtions" or we can configure how frequently we want to check the condition usingFluent Wait. These days while implementing we are using Thread.Sleep() generally it is not recommended to use
In the below example, we are creating reference wait for "WebDriverWait" class and instantiating using "WebDriver" reference, and we are giving a maximum time frame of 20 seconds.
Syntax:
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);
Explanation of Code
Code line 39: In the above example, wait for the amount of time defined in the "WebDriverWait" class or the "ExpectedConditions" to occur whichever occurs first.
//locating the element on the web page using expected conditionsWebElement guru99seleniumlink;guru99seleniumlink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='java_technologies']/li[3]/a")));guru99seleniumlink.click();
The above java code states that we are waiting for an element for the time frame of 20 seconds as defined in the "WebDriverWait" class on the webpage until the "ExpectedConditions" are met and the condition is "visibilityofElementLocated".
The following are the Expected Conditions that can be used in Explicit Wait
- alertIsPresent()
- elementSelectionStateToBe()
- elementToBeClickable()
- elementToBeSelected()
- frameToBeAvaliableAndSwitchToIt()
- invisibilityOfTheElementLocated()
- invisibilityOfElementWithText()
- presenceOfAllElementsLocatedBy()
- presenceOfElementLocated()
- textToBePresentInElement()
- textToBePresentInElementLocated()
- textToBePresentInElementValue()
- titleIs()
- titleContains()
- visibilityOf()
- visibilityOfAllElements()
- visibilityOfAllElementsLocatedBy()
- visibilityOfElementLocated()
Fluent Wait
The fluent wait is used to tell the web driver to wait for a condition, as well as thefrequency with which we want to check the condition before throwing an "ElementNotVisibleException" exception.
Frequency: Setting up a repeat cycle with the time frame to verify/check the condition at the regular interval of time
Let's consider a scenario where an element is loaded at different intervals of time. The element might load within 10 seconds, 20 seconds or even more then that if we declare an explicit wait of 20 seconds. It will wait till the specified time before throwing an exception. In such scenarios, the fluent wait is the ideal wait to use as this will try to find the element at different frequency until it finds it or the final timer runs out.
Syntax:
Wait wait = new FluentWait(WebDriver reference).withTimeout(timeout, SECONDS).pollingEvery(timeout, SECONDS).ignoring(Exception.class);
Explanation of Code
Code line 39: In the above example, we are declaring a fluent wait with the timeout of 30 seconds and the frequency is set to 5 seconds by ignoring "NoSuchElementException"
//declaring fluent wait with time out of 30 seconds and frequency is set to 5 seconds Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class);
Code Line 46: We have created a new function to identify the Web Element on the page. (Ex: Here Web Element is nothing but the selenium link on the webpage).
Frequency is set to 5 seconds and the maximum time is set to 30 seconds. Thus this means that it will check for the element on the web page at every 5 seconds for the maximum time of 30 seconds. If the element is located within this time frame it will perform the operations else it will throw an" ElementNotVisibleException"
//identifying a web element on the page with frequency set to 5 seconds WebElement clickseleniumlink = wait.until(new Function<WebDriver, WebElement>() {public WebElement apply(WebDriver driver) { return driver.findElement(By.xpath("//*[@id='java_technologies']/li[3]/a"));}});//click on the guru99 selenium link clickseleniumlink.click();
Implicit Waits vs. Explicit Waits
- Implicit Wait time is applied to all the elements in the script
- Explicit Wait time is applied only to those elements which are intended by us
- In Implicit Wait, we need not specify "ExpectedConditions" on the element to be located
- In Explicit Wait, we need to specify "ExpectedConditions" on the element to be located
- It is recommended to use when the elements are located with the time frame specified in implicit wait
- It is recommended to use when the elements are taking long time to load and also for verifying the property of the element like(visibilityOfElementLocated, elementToBeClickable,elementToBeSelected)
Conclusion:
Implicit, Explicit and Fluent Wait are the different waits used in selenium. Usage of these waits are totally based on the elements which are loaded at different intervals of time. It is alwaysnot recommended to use Thread.Sleep() while testing our application or building our framework.
This article is contributed by Chaitanya Pujari
- Implicit & Explicit Waits in Selenium
- python+selenium中的Implicit Waits/Explicit Waits/sleep以及Fluent waits
- WebDriver等待:Explicit and Implicit Waits
- Explicit, Implicit in FTPs
- selenium webdriver - explicit wait vs implicit wait
- Selenium - Waits
- explicit & implicit
- appium Explicit Waits
- implicit和explicit
- explicit 和Implicit 区别
- c# sealed explicit implicit
- explicit和implicit关键字
- explicit和implicit
- implicit explicit测试
- Implicit and Explicit Interface Implementation
- explicit 和 implicit 简单应用
- explicit 和 implicit 简单介绍
- explicit 和 implicit 的含义?
- 网络:HTTP 状态码
- 关于单参构造函数和类型转换函数
- firefox与chrome在border-image属性下的一些区别
- 更新UI的方法
- Caffe学习系列(4):激活层(Activiation Layers)及参数
- Implicit & Explicit Waits in Selenium
- iOS开发 cell重用 封装自定义方法实现
- 算法设计与分析之-最大子段和 (分治)
- Java中类的排序
- 326. Power of Three
- 如何让oracle的select强制走索引
- 欢迎使用CSDN-markdown编辑器
- 关于adb logcat
- firefox插件-HackBar介绍与试用