我有一個在類sample.I中初始化的驅動程序對象,我也想將驅動程序對象傳遞給其他類,但是我得到一個空指針異常。我的代碼是將驅動程序對象的單個實例傳遞給所有其他類(Testng框架)
樣品類
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文件
<!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>
氏當iam調用不使用創建的對象的類時,但是使用testng.xml文件是否有任何可能的方法來創建新的Java實例(對於所有類都是通用的)或使用所有類中的現有實例
你在哪裏創建對象? –
Iam在示例類中的傳輸實例方法中創建對象.. – Vicky
不,我在詢問'driver'實例。你能粘貼你的完整堆棧異常嗎? –