Skip to content

[Feature]: JUnit: Allow the user to control the Browser object's lifecycle #1691

Open
@uchagani

Description

@uchagani

🚀 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions