SlideShare a Scribd company logo
YOU ARE DOING IT WRONG
ALL THE TIME
The Great Battle of Selenium Waits
Speaker Info
Yaroslav Pernerovsky,
Ukraine
Automation Team Lead,
GlobalLogic
/in/yaroslav-pernerovsky-0559b51
/yaroslav.pernerovsky
Problem
Implicit waits are a terrible mistake I've made
and I apologies for them
Simon Stewart, d
Creator of WebDriver
Problem
Problem
Problem
Problem
@Test
public void simpleAction() {
driver.get("http://www.google.com");
driver.findElement(By.name("q"))
.sendKeys("Search String" + Keys.ENTER);
driver.findElement(By.cssSelector("h3.r"))
.click();
}
Real Case
Real Case
Real Case
Custom Wait
while(true) {
if (System.currentTimeMillis()-startTime > timeout){
throw new TimeoutException();
}
try {
return driver.findElement(locator);
}
catch (NoSuchElementException e) {}
Thread.sleep(500);
}
WebDriver waits
chromedriver
geckodriver
IEDriverServer
Implicit waits hereExplicit waits here
Implicit Waits
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
Implicit Waits
driver.findElement()
• Wait until element appeared in DOM
• Return first element if more than one present
• Throws NoSuchElementException
Implicit Waits
driver.findElements()
• Wait until at least one element appeared in DOM
• Return collection of all found elements
• Return empty collection if no elements found
Implicit Waits Common Issue
public boolean isElementPresent(By locator) {
return driver.findElements(locator).size() > 0;
}
if (isElementPresent(login))
driver.findElement(login).click();
if (!isElementPresent(login))
driver.findElement(logout).click();
Implicit Waits Common Issue
public boolean isElementNotPresent(By locator) {
driver.manage().timeouts()
.implicitlyWait(0, TimeUnit.SECONDS);
boolean result= driver.findElements(locator).size() > 0;
driver.manage().timeouts()
.implicitlyWait(defaultTimeOut, TimeUnit.SECONDS);
return result;
}
Explicit Waits
WebDriverWait wait = new
WebDriverWait(driver, 10);
wait.until(driver ->
driver.findElement(locator));
wait.until(ExpectedConditions
.presenceOfElementLocated(locator));
http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
What's wrong?
wait.until(driver ->
driver.findElement(locator));
WebDriver Exceptions and RemoteWebDriver
Solution?
wait.until(driver ->
driver.findElements(locator).size() > 0);
Mixed waits issues
What if?
wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
wait.until(ExpectedConditions
.presenceOfElementLocated(locator);
How long will Selenium wait ?
5 15
C: 10 25
Why?
public static ExpectedCondition<WebElement> presenceOfElementLocated(final By locator) {
return new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return findElement(locator, driver);
}
...
private static WebElement findElement(By by, WebDriver driver) {
try {
return driver.findElements(by).stream().findFirst().orElseThrow(
() -> new NoSuchElementException("Cannot locate an element using " + by));
} catch (NoSuchElementException e) {
throw e;
} catch (WebDriverException e) {
log.log(Level.WARNING,
String.format("WebDriverException thrown by findElement(%s)", by), e);
throw e;
}
}
What if?
wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
wait.until(ExpectedConditions
.presenceOfElementLocated(locator);
Which exception will be thrown ?
A: Timeout NoSuchElement
Both None
What if?
wait = new WebDriverWait(driver,10);
driver.manage().timeouts()
.implicitlyWait(5, TimeUnit.SECONDS);
wait.until(ExpectedConditions
.presenceOfElementLocated(locator);
How long will Selenium wait ?
5 15
C: 10 25
What if?
wait = new WebDriverWait(driver,11);
driver.manage().timeouts()
.implicitlyWait(5, TimeUnit.SECONDS);
wait.until(ExpectedConditions
.presenceOfElementLocated(locator);
How long will Selenium wait ?
5 B: 15
11 26
What if?
wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
wait.until(ExpectedConditions.not(
ExpectedConditions.presenceOfElementLocated(locator));
How long will Selenium wait if element is present?
A: 5 15
10 25
What if?
wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
wait.until(ExpectedConditions.not(
ExpectedConditions.presenceOfElementLocated(locator));
How long will Selenium wait if element is not present?
C:
5 15
10 25
Coexistence rules
Use Implicit wait for element presence
Do not use Explicit wait for element presence
Always set implicit timeout lower than explicit
Timeouts must be multiple to each other
Take special care to 'not present' conditions
Side-by-Side comparison
Client Side (500ms)
Can wait for anything
Explicit usage
TimeoutException
Multiple network calls
Explicit Implicit
Driver Side (100ms)
Element appeared in DOM
Works automatically
NoSuchElementException
Single network call
Few words about Selenide
WebDriver Implicit Wait not used at all
Custom implicit waits on client side
Default polling interval - 100ms
Remote WebDriver traffic issues
Questions

More Related Content

What's hot

laravel.pptx
laravel.pptxlaravel.pptx
laravel.pptx
asif290119
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
Manvendra Singh
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
Selenium Locators
Selenium LocatorsSelenium Locators
Selenium Locators
Satyam Pandey
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
priya Nithya
 
Intro to CloudStack API
Intro to CloudStack APIIntro to CloudStack API
Intro to CloudStack API
Sebastien Goasguen
 
Python Templating Engine - Intro to Jinja
Python Templating Engine - Intro to JinjaPython Templating Engine - Intro to Jinja
Python Templating Engine - Intro to Jinja
Eueung Mulyana
 
SignalR Overview
SignalR OverviewSignalR Overview
SignalR Overview
Michael Sukachev
 
Form using html and java script validation
Form using html and java script validationForm using html and java script validation
Form using html and java script validation
Maitree Patel
 
스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해
beom kyun choi
 
Ficheros de ayuda en aplicaciones
Ficheros de ayuda en aplicacionesFicheros de ayuda en aplicaciones
Ficheros de ayuda en aplicaciones
Laura Folgado Galache
 
Javascript
JavascriptJavascript
Javascript
guest03a6e6
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 
Streams in Node.js
Streams in Node.jsStreams in Node.js
Streams in Node.js
Sebastian Springer
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 
Django interview Questions| Edureka
Django interview  Questions| EdurekaDjango interview  Questions| Edureka
Django interview Questions| Edureka
Edureka!
 
Spring Security 5.5 From Taxi to Takeoff
Spring Security 5.5 From Taxi to TakeoffSpring Security 5.5 From Taxi to Takeoff
Spring Security 5.5 From Taxi to Takeoff
VMware Tanzu
 
Amazon Alexa Development Overview
Amazon Alexa Development OverviewAmazon Alexa Development Overview
Amazon Alexa Development Overview
John Brady
 
Revoke-Obfuscation
Revoke-ObfuscationRevoke-Obfuscation
Revoke-Obfuscation
Daniel Bohannon
 

What's hot (20)

laravel.pptx
laravel.pptxlaravel.pptx
laravel.pptx
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Selenium Locators
Selenium LocatorsSelenium Locators
Selenium Locators
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
Intro to CloudStack API
Intro to CloudStack APIIntro to CloudStack API
Intro to CloudStack API
 
Python Templating Engine - Intro to Jinja
Python Templating Engine - Intro to JinjaPython Templating Engine - Intro to Jinja
Python Templating Engine - Intro to Jinja
 
SignalR Overview
SignalR OverviewSignalR Overview
SignalR Overview
 
Form using html and java script validation
Form using html and java script validationForm using html and java script validation
Form using html and java script validation
 
스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해
 
Ficheros de ayuda en aplicaciones
Ficheros de ayuda en aplicacionesFicheros de ayuda en aplicaciones
Ficheros de ayuda en aplicaciones
 
Javascript
JavascriptJavascript
Javascript
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
Streams in Node.js
Streams in Node.jsStreams in Node.js
Streams in Node.js
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
Django interview Questions| Edureka
Django interview  Questions| EdurekaDjango interview  Questions| Edureka
Django interview Questions| Edureka
 
Spring Security 5.5 From Taxi to Takeoff
Spring Security 5.5 From Taxi to TakeoffSpring Security 5.5 From Taxi to Takeoff
Spring Security 5.5 From Taxi to Takeoff
 
Amazon Alexa Development Overview
Amazon Alexa Development OverviewAmazon Alexa Development Overview
Amazon Alexa Development Overview
 
Revoke-Obfuscation
Revoke-ObfuscationRevoke-Obfuscation
Revoke-Obfuscation
 

Similar to Implicit and Explicit waits in Selenium WebDriwer, how to.

WebDriver Waits
WebDriver WaitsWebDriver Waits
WebDriver Waits
Yaroslav Pernerovsky
 
Automation puzzlers
Automation puzzlersAutomation puzzlers
Automation puzzlers
Yaroslav Pernerovsky
 
1 aleksandr gritsevski - attd example using
1   aleksandr gritsevski - attd example using1   aleksandr gritsevski - attd example using
1 aleksandr gritsevski - attd example using
Ievgenii Katsan
 
Java. Explicit and Implicit Wait. Testing Ajax Applications
Java. Explicit and Implicit Wait. Testing Ajax ApplicationsJava. Explicit and Implicit Wait. Testing Ajax Applications
Java. Explicit and Implicit Wait. Testing Ajax Applications
Марія Русин
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
Tomek Kaczanowski
 
Experienced Selenium Interview questions
Experienced Selenium Interview questionsExperienced Selenium Interview questions
Experienced Selenium Interview questions
archana singh
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Libraries
jeresig
 
Web UI test automation instruments
Web UI test automation instrumentsWeb UI test automation instruments
Web UI test automation instruments
Artem Nagornyi
 
Web Crawling with NodeJS
Web Crawling with NodeJSWeb Crawling with NodeJS
Web Crawling with NodeJS
Sylvain Zimmer
 
Component Based Unit Testing ADF with Selenium
Component Based Unit Testing ADF with SeleniumComponent Based Unit Testing ADF with Selenium
Component Based Unit Testing ADF with Selenium
Richard Olrichs
 
Automated Testing ADF with Selenium
Automated Testing ADF with SeleniumAutomated Testing ADF with Selenium
Automated Testing ADF with Selenium
Wilfred van der Deijl
 
Javascript Unit Testing
Javascript Unit TestingJavascript Unit Testing
Javascript Unit Testing
Tom Van Herreweghe
 
Building frameworks over Selenium
Building frameworks over SeleniumBuilding frameworks over Selenium
Building frameworks over Selenium
Cristian COȚOI
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Divakar Gu
 
Test automation
Test  automationTest  automation
Test automation
Kaushik Banerjee
 
Automated testing by Richard Olrichs and Wilfred vd Deijl
Automated testing by Richard Olrichs and Wilfred vd DeijlAutomated testing by Richard Olrichs and Wilfred vd Deijl
Automated testing by Richard Olrichs and Wilfred vd Deijl
Getting value from IoT, Integration and Data Analytics
 
Get Started With Selenium 3 and Selenium 3 Grid
Get Started With Selenium 3 and Selenium 3 GridGet Started With Selenium 3 and Selenium 3 Grid
Get Started With Selenium 3 and Selenium 3 Grid
Daniel Herken
 
Service Workers for Performance
Service Workers for PerformanceService Workers for Performance
Service Workers for Performance
Patrick Meenan
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 

Similar to Implicit and Explicit waits in Selenium WebDriwer, how to. (20)

WebDriver Waits
WebDriver WaitsWebDriver Waits
WebDriver Waits
 
Automation puzzlers
Automation puzzlersAutomation puzzlers
Automation puzzlers
 
1 aleksandr gritsevski - attd example using
1   aleksandr gritsevski - attd example using1   aleksandr gritsevski - attd example using
1 aleksandr gritsevski - attd example using
 
Java. Explicit and Implicit Wait. Testing Ajax Applications
Java. Explicit and Implicit Wait. Testing Ajax ApplicationsJava. Explicit and Implicit Wait. Testing Ajax Applications
Java. Explicit and Implicit Wait. Testing Ajax Applications
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Experienced Selenium Interview questions
Experienced Selenium Interview questionsExperienced Selenium Interview questions
Experienced Selenium Interview questions
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Libraries
 
Web UI test automation instruments
Web UI test automation instrumentsWeb UI test automation instruments
Web UI test automation instruments
 
Web Crawling with NodeJS
Web Crawling with NodeJSWeb Crawling with NodeJS
Web Crawling with NodeJS
 
Component Based Unit Testing ADF with Selenium
Component Based Unit Testing ADF with SeleniumComponent Based Unit Testing ADF with Selenium
Component Based Unit Testing ADF with Selenium
 
Automated Testing ADF with Selenium
Automated Testing ADF with SeleniumAutomated Testing ADF with Selenium
Automated Testing ADF with Selenium
 
Javascript Unit Testing
Javascript Unit TestingJavascript Unit Testing
Javascript Unit Testing
 
Building frameworks over Selenium
Building frameworks over SeleniumBuilding frameworks over Selenium
Building frameworks over Selenium
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Test automation
Test  automationTest  automation
Test automation
 
Automated testing by Richard Olrichs and Wilfred vd Deijl
Automated testing by Richard Olrichs and Wilfred vd DeijlAutomated testing by Richard Olrichs and Wilfred vd Deijl
Automated testing by Richard Olrichs and Wilfred vd Deijl
 
Get Started With Selenium 3 and Selenium 3 Grid
Get Started With Selenium 3 and Selenium 3 GridGet Started With Selenium 3 and Selenium 3 Grid
Get Started With Selenium 3 and Selenium 3 Grid
 
Service Workers for Performance
Service Workers for PerformanceService Workers for Performance
Service Workers for Performance
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 

Recently uploaded

Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
Karya Keeper
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
AnkitaPandya11
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 

Recently uploaded (20)

Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 

Implicit and Explicit waits in Selenium WebDriwer, how to.

  • 1. YOU ARE DOING IT WRONG ALL THE TIME The Great Battle of Selenium Waits
  • 2. Speaker Info Yaroslav Pernerovsky, Ukraine Automation Team Lead, GlobalLogic /in/yaroslav-pernerovsky-0559b51 /yaroslav.pernerovsky
  • 3. Problem Implicit waits are a terrible mistake I've made and I apologies for them Simon Stewart, d Creator of WebDriver
  • 8. @Test public void simpleAction() { driver.get("http://www.google.com"); driver.findElement(By.name("q")) .sendKeys("Search String" + Keys.ENTER); driver.findElement(By.cssSelector("h3.r")) .click(); } Real Case
  • 11. Custom Wait while(true) { if (System.currentTimeMillis()-startTime > timeout){ throw new TimeoutException(); } try { return driver.findElement(locator); } catch (NoSuchElementException e) {} Thread.sleep(500); }
  • 13. Implicit Waits driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
  • 14. Implicit Waits driver.findElement() • Wait until element appeared in DOM • Return first element if more than one present • Throws NoSuchElementException
  • 15. Implicit Waits driver.findElements() • Wait until at least one element appeared in DOM • Return collection of all found elements • Return empty collection if no elements found
  • 16. Implicit Waits Common Issue public boolean isElementPresent(By locator) { return driver.findElements(locator).size() > 0; } if (isElementPresent(login)) driver.findElement(login).click(); if (!isElementPresent(login)) driver.findElement(logout).click();
  • 17. Implicit Waits Common Issue public boolean isElementNotPresent(By locator) { driver.manage().timeouts() .implicitlyWait(0, TimeUnit.SECONDS); boolean result= driver.findElements(locator).size() > 0; driver.manage().timeouts() .implicitlyWait(defaultTimeOut, TimeUnit.SECONDS); return result; }
  • 18. Explicit Waits WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(driver -> driver.findElement(locator)); wait.until(ExpectedConditions .presenceOfElementLocated(locator)); http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
  • 20. WebDriver Exceptions and RemoteWebDriver
  • 23. What if? wait = new WebDriverWait(driver,5); driver.manage().timeouts() .implicitlyWait(10, TimeUnit.SECONDS); wait.until(ExpectedConditions .presenceOfElementLocated(locator); How long will Selenium wait ? 5 15 C: 10 25
  • 24. Why? public static ExpectedCondition<WebElement> presenceOfElementLocated(final By locator) { return new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver driver) { return findElement(locator, driver); } ... private static WebElement findElement(By by, WebDriver driver) { try { return driver.findElements(by).stream().findFirst().orElseThrow( () -> new NoSuchElementException("Cannot locate an element using " + by)); } catch (NoSuchElementException e) { throw e; } catch (WebDriverException e) { log.log(Level.WARNING, String.format("WebDriverException thrown by findElement(%s)", by), e); throw e; } }
  • 25. What if? wait = new WebDriverWait(driver,5); driver.manage().timeouts() .implicitlyWait(10, TimeUnit.SECONDS); wait.until(ExpectedConditions .presenceOfElementLocated(locator); Which exception will be thrown ? A: Timeout NoSuchElement Both None
  • 26. What if? wait = new WebDriverWait(driver,10); driver.manage().timeouts() .implicitlyWait(5, TimeUnit.SECONDS); wait.until(ExpectedConditions .presenceOfElementLocated(locator); How long will Selenium wait ? 5 15 C: 10 25
  • 27. What if? wait = new WebDriverWait(driver,11); driver.manage().timeouts() .implicitlyWait(5, TimeUnit.SECONDS); wait.until(ExpectedConditions .presenceOfElementLocated(locator); How long will Selenium wait ? 5 B: 15 11 26
  • 28. What if? wait = new WebDriverWait(driver,5); driver.manage().timeouts() .implicitlyWait(10, TimeUnit.SECONDS); wait.until(ExpectedConditions.not( ExpectedConditions.presenceOfElementLocated(locator)); How long will Selenium wait if element is present? A: 5 15 10 25
  • 29. What if? wait = new WebDriverWait(driver,5); driver.manage().timeouts() .implicitlyWait(10, TimeUnit.SECONDS); wait.until(ExpectedConditions.not( ExpectedConditions.presenceOfElementLocated(locator)); How long will Selenium wait if element is not present? C: 5 15 10 25
  • 30. Coexistence rules Use Implicit wait for element presence Do not use Explicit wait for element presence Always set implicit timeout lower than explicit Timeouts must be multiple to each other Take special care to 'not present' conditions
  • 31. Side-by-Side comparison Client Side (500ms) Can wait for anything Explicit usage TimeoutException Multiple network calls Explicit Implicit Driver Side (100ms) Element appeared in DOM Works automatically NoSuchElementException Single network call
  • 32. Few words about Selenide WebDriver Implicit Wait not used at all Custom implicit waits on client side Default polling interval - 100ms Remote WebDriver traffic issues