microsoft / microsoft/playwright-java
[Feature]: JUnit: Allow the user to control the Browser object's lifecycle
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.6k
- Forks
- 298
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 14
Description
🚀 Feature Request
Add an option that will reset the Browser object after each test so cloud services that use BrowserType.connect can work properly. Example: https://www.browserstack.com/docs/automate/playwright/playwright-capabilities#Java
Example
public class Options {
// existing options
public Boolean closeBrowserAfterEachTest;
public Options setCloseBrowserAfterEachTest(Boolean closeBrowser) {
this.closeBrowserAfterEachTest = closeBrowser;
return this;
}
}
Inside of the existing TestWatcher methods, we can check if the user wants to close the browser after each test and do so. Once the next test starts, the Browser will be re-created by the fixture using the existing options. This will allow a new test session to start in BrowserStack.
@Override
public void testSuccessful(ExtensionContext extensionContext) {
saveTraceWhenOn(extensionContext);
closeBrowserContext();
Options options = OptionsExtension.getOptions(extensionContext);
if(options.closeBrowserAfterEachTest) {
BrowserExtension.closeBrowser();
}
}
Motivation
Cloud testing services like BrowserStack launch new sessions using BrowserType.connect. Each call to connect starts a new test session.
This doesn't work properly with current JUnit fixtures because the Browser object is re-used per thread and so all tests that run on the same thread use the same test session in BrowserStack. This becomes one giant test according to BrowserStack.
My suggestion would allow the user to decide when to re-use the browser, which works great locally, but not so well when using cloud testing services.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the JUnit extension's Options, existing TestWatcher methods, BrowserExtension.closeBrowser(), and the fixture that recreates the Browser. Confirm how the option is read and how browser reuse currently works. Done means an opt-in setting closes the Browser after each test and the next test creates a new session with the existing options.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100