如何在页面对象模型中传递相同的驱动程序实例? [英] How to pass the same driver instance in page Object model?

查看:34
本文介绍了如何在页面对象模型中传递相同的驱动程序实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自动化框架使用 selenium + TestNG + PageObject 模型.

My automation framework is using selenium + TestNG + PageObject model.

结构:

我的测试类/测试用例:

My Testng class / test case :

空指针错误

如何将驱动程序实例传递到我的页面对象中?

How can i pass the driver instance into my page objects?

推荐答案

我可以看到您在 @BeforeTest 方法中声明了一个新的 WebDriver 实例.您需要使用您在 @BeforeTest 之外声明的 WebDriver 实例,即您已经声明

I can see you are declaring a new instance of WebDriver inside the @BeforeTest method. You need to use the WebDriver instance that you declared outside the @BeforeTest i.e. you have already declared

 static WebDriver driver;

在您的 @BeforeTest 中使用相同的 driver.所以在before方法中,不要像WebDriver driver = new FirefoxDriver();那样写driver = new FirefoxDriver();

Use the same driver inside your @BeforeTest. So inside the before method, instead of doing WebDriver driver = new FirefoxDriver(); write like driver = new FirefoxDriver();

对其他浏览器类型(即 safari、chrome)执行相同操作.

Do same for other browser types (ie, safari, chrome).

对于页面对象类,您可以执行以下操作:

And for you page object classes, you can do something as follows:

public class TaxPage {

    public static WebDriver driver;

    public TaxPage(WebDriver driver) {
        this.driver = driver;
    }

}

这篇关于如何在页面对象模型中传递相同的驱动程序实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆