-
-
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
base: trunk
Are you sure you want to change the base?
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:
|
||||||||||||||
1509b2a to
02562d4
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.
02562d4 to
d36727b
Compare
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
Enhancement, Tests
Description
Replace hardcoded
Thread.sleep()calls with proper wait conditionsAdd new waiting condition methods for text matching and containment
Initialize test pages with blank page to isolate test state
Reduce polling interval in WebDriverWait for faster test execution
Update test HTML pages to delay element initialization
Diagram Walkthrough
File Walkthrough
1 files
Use diamond operator for generic type inference9 files
Replace sleep loop with elementTextToContain wait conditionReplace sleep with wait conditions in hover and drag testsRemove sleep call and improve test assertionsReplace sleep and polling loops with visibility waitsReplace sleep and polling with visibility wait conditionsReplace sleep with wait conditions in hover testsReplace sleep and polling loops with visibility waitsReplace sleep and polling with visibility wait conditionsReplace sleep with wait conditions in hover tests1 files
Remove unused logger and sleep call from test1 files
Add elementTextToContain and elementTextToMatch methods1 files
New test class for waiting condition methods1 files
Add blank page initialization before each test1 files
Reduce polling interval to 20ms for faster waits5 files
Hide elements initially and show after initializationDelay element initialization with setTimeoutDelay jQuery initialization and show elements after setupAdd h1 header element for test visibilityClear localStorage and sessionStorage on blank page