How to pass the same driver instance in page Object model?

五迷三道 提交于 2019-12-13 11:08:43

问题


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

Structure :

My Testng class / test case :

nullpointer error

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


回答1:


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;

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

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;
    }

}



回答2:


Create a class like below and pass WebDriver in parametrize constructor and Call driver like Page.driver whenever you need it

  public class Page 
    {
        public static WebDriver driver;

        public Page(WebDriver driver)
        {
            Page.driver = driver;
        }
    }

Hope it will help you :)



来源:https://stackoverflow.com/questions/33100961/how-to-pass-the-same-driver-instance-in-page-object-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!