-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Avoid sleep in tests #16713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid sleep in tests #16713
Conversation
|
Thank you, @asolntsev for this code suggestion. The support packages contain example code that many users find helpful, but they do not necessarily represent After reviewing the change, unless it is a critical fix or a feature that is needed for Selenium We actively encourage people to add the wrapper and helper code that makes sense for them to their own frameworks. |
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||||
02562d4 to
d36727b
Compare
d36727b to
cfc922c
Compare
It's a good practice to avoid tests affecting each other: 1. Open "about:blank" page - this stops any current activity / background requests / animations on the previous page 2. Open an empty page which clears sessionStorage, localStorage and cookies. This technique allows reusing the browser between tests, while keeping the tests independent.
Instead of wasting 2 second (which doesn't guarantee the result), just wait for the needed element that should appear as a result of hovering.
if the first try failed, wasting the whole 0.5 second is a huge waste of time. Let's try sooner!
fix the test page usability: make the elements visible only after they are fully initialized. To force revealing potential flaky tests in the future, I've added a small pause for the initialization code. So the tests should wait until the element appears on the screen - then it's ready for manipulations.
it breaks Py/Rb tests that I cannot fix now. Ideally, we should add waiting to these tests as well.
Also, reset the link to static field after test run - to avoid holding heavy objects in memory till the end of all tests.
NB! This change decrease execution time of `SessionHandlingTest` from ~28s to 7s.
cfc922c to
6d0285b
Compare
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||||||||
User description
💥 What does this PR do?
This PR removes most of "SLEEP N" from Java tests.
🔧 Implementation Notes
Instead of just silly waiting for N seconds, it's better to wait for the needed event. E.g. to wait the needed button on the screen etc.
Thus we make our tests faster and more stable.
🔄 Types of changes
PR Type
Bug fix, Enhancement
Description
Replace artificial sleeps with proper wait conditions in tests
Improve test stability by waiting for elements to be visible/ready
Reduce test execution time significantly (e.g., SessionHandlingTest from 28s to 7s)
Add blank page navigation before each test to isolate test state
Refactor test pages to show elements only after initialization
Diagram Walkthrough
File Walkthrough
1 files
Remove explicit type parameters using diamond operator16 files
Replace sleep loop with wait condition for focus eventRemove artificial sleep and unused loggerAdd null checks in tearDown to prevent NPEReplace sleeps with wait conditions for session closureReplace sleep with visibility wait in hover testRemove artificial sleep and improve assertionsReplace sleeps and polling with wait conditionsReplace sleep and polling with visibility waitsAdd null check and reset server referenceAdd null checks in cleanup methodsAdd null checks and reset references in tearDownReplace sleep with visibility wait in hover testReplace sleeps and polling with wait conditionsReplace sleep and polling with visibility waitsReplace sleeps with visibility waits in hover testsAdd null checks in server cleanup8 files
Add new wait conditions and use diamond operatorAdd blank page navigation before each testReduce polling interval for faster test executionHide elements initially and show after initializationHide elements initially and show after jQuery setupHide elements initially and show after jQuery setupAdd h1 header element for test visibilityClear storage on blank page load1 files
Add new test class for waiting conditions