Reputation: 55
Selenium - TestNG Code is not working for me.
package firsttestngpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;
Define the testng class
public class FirstTestNGFile {
public String baseURL = "http://newtours.demoaut.com/";
public WebDriver driver = new FirefoxDriver();
@Test
Define the method
public void verifyHomePageTitle() {
driver.get(baseURL);
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
driver.quit();
}
}
**I am facing the following error while using testng **
org.testng.TestNGException:
Cannot instantiate class firsttestngpackage.FirstTestNGFile
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
Upvotes: 1
Views: 97
Reputation: 31878
You should try instantiating the driver in @BeforeSuite
as -
public class FirstTestNGFile {
public String baseURL = "http://newtours.demoaut.com/";
public WebDriver driver;
@BeforeSuite
public void SetBrowser(){
driver = new FirefoxDriver();
}
@Test
public void verifyHomePageTitle() {
driver.get(baseURL);
...
driver.quit();
}
}
Upvotes: 1