I have a driver object initialized in class sample.I want to pass the driver object to other classes also but i get a null pointer exception. My code is
sample class
public class sample {
WebDriver driver ;
@Test(priority=1)
public void openbrowser(){
System.setProperty("webdriver.chrome.driver",
"/home/ss4u/Desktop/Vignesh/jars/chromedriver");
driver = new ChromeDriver();
driver.get("http://www.google.com");
System.out.println(driver instanceof WebDriver);
}
@Test(priority=2)
public void maximize(){
driver.manage().window().maximize();
}
@Test(priority=3)
public void transfer_instance(){
sampleone obj=new sampleone(driver);
}
}
sampleclassone
public class sampleone {
WebDriver driver;
public sampleone(WebDriver driver){
this.driver=driver;
System.out.println(driver instanceof WebDriver);
System.out.println(this.driver instanceof WebDriver);
System.out.println("constructor2");
}
public sampleone(){
System.out.println("Default constructor called");
}
@Test(priority=1)
public void gettitle(){
System.out.println(this.driver instanceof WebDriver);
System.out.println(driver instanceof WebDriver);
String title=this.driver.getTitle();
System.out.println(this.driver instanceof WebDriver);
System.out.println(title);
Assert.assertEquals(title, "Google");
}
@Test(priority=2)
public void navigate(){
this.driver.get("https:in.yahoo.com");
}
}
Testng xml file
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestNG" verbose="1" >
<test name="sample test">
<classes>
<class name="testsample.sample" />
</classes>
</test>
<test name="sample testone">
<classes>
<class name="testsample.sampleone" />
</classes>
</test>
</suite>
This issue occurs as iam calling the class not using the object created but using the testng.xml file is there any possible way to create a new java instance(common for all classes) or use the existing instance in all classes