-2

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

Structure : enter image description here

My Testng class / test case :

enter image description here

nullpointer error

enter image description here

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

santoshaa
  • 173
  • 2
  • 4
  • 10

2 Answers2

2

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

}
AGill
  • 768
  • 8
  • 17
  • Thanks a lot, this was the solution i was looking for. Why so many down votes on my question? It's a valid one. – santoshaa Oct 14 '15 at 07:38
  • Take a look at the SO regulations for 'How to Ask'. For example , posting the actual code instead of screen shots would be a better approach. Try to make it easy for the people trying to help. – AGill Oct 14 '15 at 21:27
0

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 :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • If i write all the code inside a main method, my test is executing fine. But if i use , BeforeTest and Test annotations, i am getting null pointer exception. What is going wrong? – santoshaa Oct 13 '15 at 14:10
  • The reason may be you not declare the webdriver outside of you BeforeTest as I have did it public static WebDriver driver; -> this is outside of any function. The reason you have to do this because as per you function end jvm delete all related variable for same. so if you want to use it define the WebDriver instance outside of your before test function – Shubham Jain Oct 13 '15 at 14:17