0

My question is pretty straightforward I have parent class and I have created a new driver object of webdriver class there In my subclass I am able to access the driver object but its value is null I am not able to access the object properties My parent Class is

public class BaseTest {

    public static  WebDriver driver;
    @BeforeTest
    @Parameters("browser")
    public void  VerifyPageTitle(String browser)
    {
        if(browser.equalsIgnoreCase("chrome")){
            System.setProperty("webdriver.chrome.driver","C://chromedriver_win32(1)//chromedriver_win32//chromedriver.exe");
             driver = new ChromeDriver();
        }
        if(browser.equalsIgnoreCase("IE")){
            System.setProperty("webdriver.ie.driver","C://IEDriverServer_x64_3.3.0//IEDriverServer.exe");
            driver = new InternetExplorerDriver();
        }
        driver.manage().window().maximize();
    }
}

in my sub-class code

public class SignPageTest extends BaseTest{

    SignPage obj ;
    Boolean stu;
    @Test
    public void navigateToSignPage(){
    obj = new SignPage(driver);
    stu = obj.navigateToSignIn();
    }
    @Test(priority=2)
    public void getHandles(){
        stu = obj.handlingWindows();
    }

}

I am getting null-pointer as my driver is not initialized So please tell me the way to simply access the objects properties of super-class

the moment I make my driver as static I am able to access its properties in all other classes but I don't want it to be static

Cœur
  • 37,241
  • 25
  • 195
  • 267
abishek kachroo
  • 35
  • 1
  • 11
  • at which line are you getting null pointer exception? – Gaurang Shah Aug 18 '17 at 12:50
  • See the driver that I am able to acess in the sub-class is not even initialized the moment that object is used to perform any operations there would be null pointer In my case its in my page class – abishek kachroo Aug 18 '17 at 14:07
  • Possible duplicate of https://stackoverflow.com/questions/22582522/accessing-parent-class-static-field-using-child-class-name-doesnt-load-the-chil – Tarun Lalwani Aug 18 '17 at 15:17

1 Answers1

1

The problem lies in your test code.

You are initialising a WebDriver instance via an @BeforeTest annotated method. TestNG invokes a @BeforeTest method only ONCE per <test> tag. So the below combination (which am guessing is what you have) will cause NullPointerException in the @Test methods of the second class.

  • You have SignPageTest extend BaseTest and lets say you have another class called SomeFlowTest which also extends BaseTest
  • You have created a <test> that includes both SignPageTest and SomeFlowTest.

This would cause the @BeforeTest to be executed only once for either SignPageTest (or) SomeFlowTest (depending upon the order in which they occur in your <test> tag), because both these classes extend from the same base class. So once TestNG executes VerifyPageTitle() via SignPageTest it will not execute the same method via SomeFlowTest and so the WebDriver instance for SomeFlowTest is null.

To fix this problem change @BeforeTest to either @BeforeClass (gets executed once per test class) or @BeforeMethod (gets executed for every @Test method).

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • You are 100% right , you have exactly told the problem I have tried that too but there is a problem with this practice , Ur new driver will be created everytime so in this case for every class or method new browser will be created I dont want the new browser be created , I want to use the same webdriver. – abishek kachroo Aug 19 '17 at 14:33
  • @abishekkachroo - If you want a WebDriver instance to be created once and shared everywhere else, then you should be trying to leverage a Singleton pattern, wherein the first invocation to it would cause the browser to be instantiated and all the rest of the calls would just use the already created ones. The creation call can be triggered from within a `@BeforeTest` method, and the rest of the code can just invoke the `getter()` method. – Krishnan Mahadevan Aug 19 '17 at 14:36