selenium - not able to acesss parent class object properties in child class in Java -
my question pretty straightforward have parent class , have created new driver object of webdriver class there in subclass able acess driver object value null not able acess object properties parent class
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 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 getting null-pointer driver not initialized please tell me way acess objects properties of super-class
the moment make driver static able acess properties in other classes dont want static
the problem lies in test code.
you initialising webdriver instance via @beforetest annotated method. testng invokes @beforetest method once per <test> tag. below combination (which guessing have) cause nullpointerexception in @test methods of second class.
- you have
signpagetestextendbasetest, lets have class calledsomeflowtestextendsbasetest - you have created
<test>includes bothsignpagetest,someflowtest.
this cause @beforetest executed once either signpagetest (or) someflowtest (depending upon order in occur in <test> tag), because both these classes extend same base class. once testng executes verifypagetitle() via signpagetest not execute same method via someflowtest , webdriver instance someflowtest null.
to fix problem change @beforetest either @beforeclass (gets executed once per test class) or @beforemethod (gets executed every @test method).
Comments
Post a Comment