-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Expand documentation on fresh browser per test #1671
base: trunk
Are you sure you want to change the base?
Conversation
👷 Deploy request for selenium-dev pending review.Visit the deploys page to approve it
|
PR Description updated to latest commit (7f2aa0a)
|
PR Review
✨ Review tool usage guide:Overview: The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.
See the review usage page for a comprehensive guide on using this tool. |
PR Code Suggestions
✨ Improve tool usage guide:Overview:
See the improve usage page for a comprehensive guide on using this tool. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pytest.fixture(autouse=True, scope='function') | ||
def driver(self, request, page: Page): | ||
# Create the driver | ||
driver = webdriver.Firefox() | ||
|
||
# Return control to the test | ||
yield self.driver | ||
|
||
# Test ends driver quits | ||
driver.quit() | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a code sample for this: https://github.com/SeleniumHQ/seleniumhq.github.io/blob/trunk/examples/python/tests/conftest.py#L15-L21
# This forces the ThreadLocal<WebDriver> variable to call driver.get() every time the driver wants to be used. | ||
|
||
# In general static variables in non-thread safe code can have unintended consequences and increase the maintanance effort in the code base. | ||
|
||
public abstract class BaseTest { | ||
protected ThreadLocal<WebDriver> driver; | ||
... | ||
// Before each test hook | ||
public void setupTest() { | ||
BaseTest.driver = ThreadLocal.withInitial(()->new FirefoxDriver()); | ||
... | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefer to avoid mentioning ThreadDriver
as it has brought more damage than benefit.
Perhaps show here the code examples we have already:
I was wondering about showing ThreadLocal or not given its many issues. I'll remove the references to existing code in examples |
User description
Thanks for contributing to the Selenium site and documentation!
A PR well described will help maintainers to review and merge it quickly
Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, and help reviewers by making them as simple and short as possible.
Description
Added an extended explanation and code examples on how to implement a fresh browser per test. Tried to avoid specific framework code like JUnit vs TestNG.
Motivation and Context
I see a lot of Selenium automation project in the industry with a static WebDriver that gets shared between tests and across all these teams there has always been confusion on how to get started with a fresh browser per test. This is made more prominent with requirements to run tests is parallel and a lot of automation coders not understanding the implications a static driver (without ThreadLocal which brings its own complications) has in attempts to run parallel tests in multi-threaded languages like Java or C#
Types of changes
Checklist
Type
documentation, enhancement
Description
Changes walkthrough
fresh_browser_per_test.en.md
Enhance Documentation with Fresh Browser Per Test Examples
website_and_docs/content/documentation/test_practices/encouraged/fresh_browser_per_test.en.md
implementing a fresh browser per test.
ThreadLocal for thread safety.
fresh_browser_per_test.ja.md
Add Fresh Browser Per Test Examples in Japanese Documentation
website_and_docs/content/documentation/test_practices/encouraged/fresh_browser_per_test.ja.md
execution.
fresh_browser_per_test.pt-br.md
Update Portuguese Documentation with Browser Per Test Strategy
website_and_docs/content/documentation/test_practices/encouraged/fresh_browser_per_test.pt-br.md
instance per test.
for thread safety.
fresh_browser_per_test.zh-cn.md
Enhance Chinese Documentation with New Browser Per Test Examples
website_and_docs/content/documentation/test_practices/encouraged/fresh_browser_per_test.zh-cn.md
ThreadLocal in test isolation.