You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Selenium WebDriver中实现多类共享同一浏览器实例

Solution to Pass Browser Instance Between Classes

Let's break down how to properly share the browser driver instance between your classes, fixing the gaps in your current code structure:

Step 1: Refine the Interface and Base Class

First, we need to give your interface a way to expose the underlying WebDriver instance (right now it's empty, so we can't access the actual browser driver):

public interface InterfaceClass {
    // Add method to retrieve the WebDriver instance
    WebDriver getWebDriver();
}

public class Base {
    // Use a static variable to hold the driver (or use a singleton pattern for better practice)
    public static InterfaceClass driver;

    public void setDriver(InterfaceClass driver) {
        Base.driver = driver;
    }
}

Step 2: Update the Selenium Implementation

Make sure your Seleniumclass properly initializes the browser and implements the interface method:

public class Seleniumclass implements InterfaceClass {
    private WebDriver driver;

    public void initiTest() {
        // Example browser initialization (adjust for your setup)
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Override
    public WebDriver getWebDriver() {
        return this.driver;
    }
}

Step 3: Initialize Driver in First Class

In your FirstClass, initialize the browser driver and store it in the Base class so other classes can access it:

public class FirstClass extends HomePageComponents {
    @BeforeClass
    public void setup() throws IOException {
        Seleniumclass seleniumDriver = new Seleniumclass();
        seleniumDriver.initiTest(); // Launch the browser
        Base.driver = seleniumDriver; // Store the driver in Base class
    }
}

Step 4: Access Driver in Second Class

Now your SecondClass can retrieve the driver from Base and use it. Fix the constructor and setup method:

public class SecondClass extends HomePageComponents {
    private InterfaceClass driver;

    // Constructor to accept the driver instance
    public SecondClass(InterfaceClass driver) {
        this.driver = driver;
        // Verify we have the driver (optional)
        System.out.println("Received driver instance: " + this.driver.getWebDriver());
    }

    @BeforeClass
    public void setup() throws IOException {
        // Retrieve the driver from Base class and pass to SecondClass
        SecondClass asa = new SecondClass(Base.driver);
        // Now you can use asa.driver.getWebDriver() to interact with the browser
    }
}

Key Notes:

  • Static vs Singleton: Using a static variable in Base is simple for test code, but a singleton pattern for Base is more robust for larger projects (avoids accidental overwrites).
  • Interface Purpose: The InterfaceClass acts as a wrapper, so you can swap out Selenium for another automation tool later without changing dependent classes.
  • Test Context: If these are separate TestNG/JUnit test classes, ensure they run in the same test context (e.g., use preserve-order="true" in TestNG) to keep the driver alive between classes.

内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:21:50