如何在 Selenium Webdriver 框架中实现 DriverSetup class

How to implement the DriverSetup class in Selenium Webdriver framework

如何在 Selenium Webdriver 框架中实现 DriverSetup class.. 目前我正在为每个 testng 测试 class 在 @BeforeClass 中启动驱动程序,请告诉我如何为所有测试实现通用 driverLaunch/driverSetup class 提前致谢..

在@BeforeTest 或@BeforeSuite 中初始化您的WebDriver,并在@AfterTest 或@AfterSuite 中关闭它。所以在这种情况下,每个测试方法都会在同一个浏览器中得到 运行。

您的意思是所有 classes 的通用设置吗?如果是这样,请创建一个基础 class 并在每个测试 class 中扩展它。在 Base class 中有 @BeforeClass 来做所需的。 有点像:

public class BaseClass {
    WebDriver driver;
    @BeforeClass
    public void setUp() {
        driver = new FirefoxDriver(); // or any driver u want, or based on requirement create a if else scenario
    }
}

并且在测试中class喜欢:

public class TestClass extends BaseClass {
    // your class body with tests here
}

因此,每当您 运行 通过 testng 进行测试时,它将调用 BaseClass 中的 setUp 方法并为您设置浏览器。