我正在尝试使用@beforeSuite 和@AfterSuite 在一个浏览器实例中运行我的测试。但是,它运行第一个测试类,但在第二个类时因空指针异常而失败。
下面是我的代码:
LaunchBrowser.java 类
public class LaunchBrower {
protected WebDriver driver;
public WebDriver getDriver() {
return driver;
}
@Parameters({ "browserType", "appURL" })
@BeforeSuite
public void setUp(@Optional String browsertype, @Optional String appURL) {
System.out.println("Launching google chrome with new profile..");
driver = getBrowserType(browsertype);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to(appURL);
}
private WebDriver getBrowserType(String browserType) {
if (driver == null ) {
if (browserType.equalsIgnoreCase("chrome")) {
return new ChromeDriver();
}
else if (browserType.equalsIgnoreCase("InternetExplorer")) {
return new InternetExplorerDriver();
}
}
return driver;
}
@AfterSuite
public void tearDown() {
if (driver != null)
driver.quit();
}
}
LoginPageTest 类
public class LoginPageTest extends LaunchBrower {
protected WebDriver driver;
private LoginPage loginpage;
private MyProfile profilepage;
Logger log = Logger.getLogger("Results:");
@BeforeClass(alwaysRun = true)
public void setUp() {
loginpage = PageFactory.initElements(getDriver(), LoginPage.class);
//loginpage = new LoginPage(driver);
//driver=getDriver();
}
@Test(groups = "LoginPage")
public void verifyLogin() throws InterruptedException {
//LoginPage login = new LoginPage(driver);
System.out.println("Sign In functionality details...");
//System.out.println("Sign In functionality details seee...");
Thread.sleep(10000);
//login.enterUserName("11111111");
Assert.assertEquals("11111111",loginpage.enterUserName("11111111"));
log.debug("Correct Username Pass");
//System.out.println("Correct username...");
Assert.assertEquals("fe9245db",loginpage.enterPassword("fe9245db"));
log.debug("Correct Password Pass");
loginpage.clickOnLogin();
log.debug("Login Pass");
}
}
MyProfileTest java 类
public class MyProfileTest extends LaunchBrower {
protected WebDriver driver;
private MyProfile profilepage;
@BeforeClass(alwaysRun = true)
public void setUp() {
profilepage = PageFactory.initElements(getDriver(), MyProfile.class);
//driver=getDriver();
}
@Test(groups = "Myprofile")
public void verifyMyprofile() throws InterruptedException {
System.out.println("My profile test...");
//MyProfile profile = new MyProfile(driver);
profilepage.ClickToggleLink();
profilepage.ClickMyProfile();
}
}