第一个TestNG代码无效

时间:2017-03-07 13:38:34

标签: testng

Selenium - TestNG Code对我不起作用。

package firsttestngpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

定义testng类

public class FirstTestNGFile {
public String baseURL = "http://newtours.demoaut.com/";
public WebDriver driver = new FirefoxDriver();      
@Test

定义方法

public void verifyHomePageTitle() {
    driver.get(baseURL);
    String expectedTitle = "Welcome: Mercury Tours";
    String actualTitle = driver.getTitle();
    Assert.assertEquals(actualTitle, expectedTitle);
    driver.quit();              
}
}

**使用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)

1 个答案:

答案 0 :(得分:1)

您应该尝试在@BeforeSuite中将驱动程序实例化为 -

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();              
    }
}