当我使用 CSVReader 时,它显示错误
public class firstclass {
private static WebDriver driver= new FirefoxDriver();
@DataProvider(name="readcredentials")
public Object[][] readcredentials() throws Exception{
CSVReader readunamepass= new CSVReader(new FileReader("D:/My Projects New/BSP/Aut/Credentials.csv"));
List<String[]> list = readunamepass.readAll();
Iterator<String[]> readnext = list.iterator();
while(readnext.hasNext())
{
String[] credential = readnext.next();
System.out.println("" +readnext);
System.out.println("username"+credential[0]+"password" +credential[1]);
f(credential[0], credential[1]);
}
return null;
}
@Test(priority=1, dataProvider="readcredentials")
public void f(String credential, String credential2) throws Exception {
getDriver().get("http://192.168.1.111/lms");
getDriver().findElement(By.id("txtUserId")).sendKeys( credential);
getDriver().findElement(By.id("txtPwd")).sendKeys(credential2);
getDriver().findElement(By.id("btnSign")).click();
//Thread.sleep(100000);
//System.out.println("This is method");
}
public WebDriver getDriver(){
return driver;
}
在此程序中,@dataprovider 从 csv 文件读取用户名和密码并将其传递给测试方法。 我无法提供返回类型,因此我提供了 null。请帮助 更多详情 - 我尝试返回对象,但它向我展示了一些建议。我已经尝试过,但编译时显示错误。看图片Error image
最佳答案
您的数据提供程序返回 null,因此您遇到空指针异常,
It should return
Object[][]
此外,您不需要在 readcredentials() 方法中调用 f(credential[0], credential[1])
方法,而不是您的 readcredentials() 方法应返回 Object[][]
,在你的情况下它可能是 String[][]
有关如何使用带有参数的数据提供程序的更多详细信息,您可以引用testng官方文档:Parameters with DataProviders
public class firstclass {
@DataProvider(name="readcredentials")
public Object[][] readcredentials() throws Exception{
CSVReader readunamepass= new CSVReader(new FileReader("hello.csv"));
List<String[]> list = readunamepass.readAll();
String[][] array = new String[list.size()][];
for(int i=0;i<list.size();i++)
{
array[i] = list.get(i);
}
return array;
}
@Test(priority=1, dataProvider="readcredentials")
public void f(String value, String value1) throws Exception {
System.out.println(value+" "+value1);
//to do
//your logic
}
}
关于java - 显示 java.lang.NullPointerException 在 org.testng.internal.MethodInitationHelper.invokeDataProvider(MethodInitationHelper.java :150) in Testng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35178375/