Skip to content

Seperate Browser Sessions

Bernd Weigel edited this page Dec 4, 2024 · 1 revision

Separate Browser Sessions for Test Setup and Cleanup

Overview

This feature allows you to manage separate browser instances for test setup (@Before) and cleanup (@After) methods using dedicated annotations. This is particularly useful when a proper clean up is essential for your testing or when you need to start testing after the set up in a clean session.

Background

An unsolved problem for each testautomation are crashing browser instances. It happens rarely, nowadays but it does and when it does everything which should happen after the browser is closed can't be done. To ensure a proper cleanup of your test system, Neodymium offers a way to use seperate browsers with a clean session to perform setup and cleanup tasks.

Annotations

@StartNewBrowserForSetUp

Indicates that a new browser instance should be created specifically for the @Before method execution.

@StartNewBrowserForSetUp
@BeforeEach
public void setUp() {
    // This will run in a separate browser instance
    // Browser will be automatically closed after setup
}

@StartNewBrowserForCleanUp

Indicates that a new browser instance should be created specifically for the @After method execution.

@StartNewBrowserForCleanUp
@AfterEach
public void cleanUp() {
    // This will run in a separate browser instance
    // Browser will be automatically closed after cleanup
}

Usage Examples

Basic Usage

public class TestExample {
    @StartNewBrowserForSetUp
    @BeforeEach
    public void setUp() {
        // Setup code using dedicated browser
        open("http://example.com");
        // Perform setup operations
    }

    @NeodymiumTest
    public void testFeature() {
        // Main test code using main browser instance
    }

    @StartNewBrowserForCleanUp
    @AfterEach
    public void cleanUp() {
        // Cleanup code using dedicated browser
        open("http://example.com/cleanup");
        // Perform cleanup operations
    }
}

Combined Usage

public class ComplexTestExample {
    @StartNewBrowserForSetUp
    @BeforeEach
    public void prepareTestData() {
        // Setup complex test data
        open("http://admin.example.com");
        // Create necessary test data
    }

    @NeodymiumTest
    public void testUserFlow() {
        // Test runs in its own browser instance
        open("http://example.com");
        // Test user flow
    }

    @StartNewBrowserForCleanUp
    @AfterEach
    public void removeTestData() {
        // Clean up in separate browser
        open("http://admin.example.com");
        // Remove test data
    }
}

This feature supports JUnit4 as well as JUnit5. The examples abofe can be easily transfered to JUnit4 by changing from @BeforeEach to @Before and @AfterEach to @After.

Important Notes

  1. Browser Lifecycle

    • Each annotated method receives a fresh browser instance
    • Browser instances are automatically closed after method execution
  2. Best Practices

    • Use separate browsers when setup/cleanup requires different user sessions or the additional safety net for browser crashes.
    • Consider performance implications of creating multiple browser instances
    • Ensure proper error handling in setup and cleanup methods
  3. Limitations

    • @BeforeAll and @AfterAll (or for JUnit 4 @BeforeClass and @AfterClass) are NOT covered with this, due to the general Neodymium lifecycle.
    • Browser state is not shared between different instances
    • Each new browser instance requires additional system resources, but there are no browsers running in parallel
    • Setup time may increase due to multiple browser launches
Clone this wiki locally