【问题标题】:How to implement the DriverSetup class in Selenium Webdriver framework如何在 Selenium Webdriver 框架中实现 DriverSetup 类
【发布时间】:2017-06-21 22:08:49
【问题描述】:

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

【问题讨论】:

  • 如果你想让每个测试通过设置启动不同的浏览器,使用TestNG的@BeforeTest@BeforeMethod来实现。谢谢。

标签: selenium automation webdriver


【解决方案1】:

你的意思是所有班级的通用设置吗?如果是这样,请创建一个基类并在每个测试类中扩展它。在基类中有@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
    }
}

并且在 Testclass 中这样做:

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

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

【讨论】:

  • 感谢您的建议。对我来说效果很好,并且能够在 Listener 类中获取驱动程序实例。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    相关资源
    最近更新 更多