如何为数据提供者@Test方法创建WebDriver实例

时间:2013-02-25 09:09:48

标签: webdriver testng

我正在@Test中运行浏览器实例(dataprovider ..方法。但是我无法将驱动程序实例传递给Test方法,

这是我的代码,在最后一个语句中我得到一个零点异常。

我的问题是,如何将驱动程序实例传递给测试方法

@Test (dataProviderClass=GetData.class)

public class createUserPageTest {

private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
private MailCode openMail;

@BeforeMethod
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = “http://demo.mysite.com”;
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl + “/pageLink/Home.action”);
}
@Test(dataProvider=”RegisterUserStage1″)
public void NewUsrStage1(String name,String email,String password)
{
CreateUserPage createuser = PageFactory.initElements(driver, CreateUserPage.class);
createuser.CreateUserStage1(driver,name,email,password);
try {
openMail.testValidateEmail(driver);
——-
——-

显示的错误消息是

java.lang.NullPointerException
    at demoname.org.createUserPageTest.NewUsrStage1(createUserPageTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

我的DataProvider名称如下

@DataProvider(name="RegisterUserStage1")
    public static Object[][] getTestData() throws Exception
    {
        Object retObject[][]=getTableArray("src\\resource\\TestData.xls", 0, "NewUserStage1");
        return retObject;
    }

1 个答案:

答案 0 :(得分:0)

代码存在多个问题。 正如AJ正确地指出了初始化的对象。