Selenium Waits 2015 Perfecto Mobile Ltd All Rights
Selenium Waits © 2015, Perfecto Mobile Ltd. All Rights Reserved.
Agenda • Why Wait? • Selenium Wait Commands • Implicit Waits • Explicit Waits • Fluent Waits • Conflicts • Erratic Behavior • Demonstration 12/18/2021 © 2015, Perfecto Mobile Ltd. All Rights Reserved. 2
Why Wait? • When automating an application you must wait for a transaction to complete before proceeding to your next action. • Using sleeps should never be used as an alternative to waits. Sleeps will ALWAYS wait the exact amount of time you specify even if the application is ready to proceed. • Waits only wait as long as necessary which saves you precious time on your execution. • Selenium Offers 3 wait types • Implicit Waits • Explicit Waits • Fluent Waits 12/18/2021 © 2015, Perfecto Mobile Ltd. All Rights Reserved. 3
Different Wait Commands Implicit Waits (Good) Explicit Waits (Better) Fluent Waits (Best) Why Use? If possible don’t use implicit waits Allows you to wait for a specific condition or set of conditions before proceeding Pros • Ease of use • Fine for small applications with little processing time and no dynamic content or behaviors • Great control over wait behavior • Allows control over application polling to check for condition Cons • No control over behavior • False-Positives on successful transactions • Can cause errors or additional wait times when used with Explicit and Fluent waits • Flow control code (conditional) will result in unnecessary waits • No control over application • Requires polling to check for implementation on an as condition needed basis instead of • Requires implementation globally on an as needed basis instead of globally 12/18/2021 © 2015, Perfecto Mobile Ltd. All Rights Reserved. 4
Implicit Wait Implicit waits apply globally to every find element call. Depending on the driver, implicit waits will either ping the application every X milliseconds OR if the implicit wait fails the first time it may wait the duration of the timeout before trying one last time to find the element. Implicit waits only check for the existence of an object. Due to this behavior your application may not be ready to use even if the wait succeeds in locating an object. To resolve conflicts: When using implicit waits with fluent/explicit waits you should set the implicit wait to zero prior to performing the fluent/explicit wait. Once the fluent/explicit wait completes you can set the implicit wait back to its default value. When implicit waits are used you should set the value to zero prior to performing conditional checks on a page. Best practice is to not use implicit waits if possible. Java 12/18/2021 C# © 2015, Perfecto Mobile Ltd. All Rights Reserved. 5
Explicit Waits Unlike implicit waits, explicit waits only apply on a per transaction basis. Explicit waits ping the application every 500 ms checking for the condition of the wait to be true. You can have explicit waits wait for finding an element or some predefined expected conditions. https: //selenium. googlecode. com/git/docs/api/java/org/openqa/selenium/support/ui/Expected. Conditions. html Java 12/18/2021 C# © 2015, Perfecto Mobile Ltd. All Rights Reserved. 6
Fluent Waits Just like explicit waits, fluent waits only apply on a per transaction basis. Fluent waits require that you define the wait between checks of the application for an object/condition as well as the overall timeout of the transaction. Additionally you must tell fluent waits not to throw an exception when they don’t find an object as best practice. You can have fluent waits wait for finding an element or some predefined expected conditions. https: //selenium. googlecode. com/git/docs/api/java/org/openqa/selenium/support/ui/Expected. Conditions. html Java - Setup C# - Setup Java – Find and return element C# - Find and return element 12/18/2021 © 2015, Perfecto Mobile Ltd. All Rights Reserved. 7
Wait Conflicts • Using Implicit and Fluent/Explicit Wait Together • Implicit waits and fluent/explicit waits can conflict • Due to the driver deciding where waits occur (client vs server side) using implicit and explicit/fluent waits together can create race conditions in all circumstances • implicit timeout > fluent/explicit timeout • fluent/explicit timeout > implicit timeout • implicit timeout == fluent/explicit timeout • Implicit waits can conflict with fluent/explicit conditions • Fluent/explicit waits have many different conditions which could be met • Mixing implicit waits with fluent/explicit waits can result in undesired sleeps • Mixing implicit waits with fluent/explicit waits can result in errors returning from one of the implemented waits • If you must mix implicit waits with fluent/explicit waits you should set the implicit wait timeout value to zero in order to resolve any conflict which would arise. 12/18/2021 © 2015, Perfecto Mobile Ltd. All Rights Reserved. 8
Erratic Wait Behavior • Waits can behave differently on a per driver basis • Chrome Driver, Firefox Driver, Perfecto, Etc. • Remote web driver adds complexity to the wait implementation • The remote web driver resides on a server which executes a driver behind the scenes • The driver being utilized makes the decision on how waits behave (server vs client) • This can change over time as the driver goes through version changes • Race conditions may be seen on one driver and not another • Behavior may also differ based on the programming language • Best Practice: • You should test your code on all driver types to ensure response time and/or errors are desirable 12/18/2021 © 2015, Perfecto Mobile Ltd. All Rights Reserved. 9
Thank You © 2015, Perfecto Mobile Ltd. All Rights Reserved.
- Slides: 10