Test
public class ParameterByITestContextInDataprovider
{
WebDriver driver;
@BeforeTest(groups={"A","B"})
public void setup()
{
//Create gc driver object
System.out.println("In ParameterByMethodInDataprovider....");
String drivePath="E:\\Software_Testing\\Softwars-Jars\\BrowsersWithDrivers\\Chrome"
+ "\\chromedriver_win32_V2.38_supports_Chrome_65_67.exe";
String driverComponent="webdriver.chrome.driver";
System.setProperty(driverComponent,drivePath);
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
}// setup
@Test(dataProvider="SearchProvider",groups="A")
public void testMethodA(String author,String searchKey) throws InterruptedException
{
{
//search google textbox
WebElement searchText = driver.findElement(By.name("q"));
//search a value on it
searchText.sendKeys(searchKey);
System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
Thread.sleep(3000);
String testValue = searchText.getAttribute("value");
System.out.println(testValue +"::::"+searchKey);
searchText.clear();
//verify correct value in searchbox
Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
}//tp
}//tc
@Test(dataProvider="SearchProvider",groups="B")
public void testMethodB(String searchKey) throws InterruptedException
{
{
//find google search box
WebElement searchText = driver.findElement(By.name("q"));
//search a value on it
searchText.sendKeys(searchKey);
System.out.println("Welcome ->Unknown user Your search key is->"+searchKey);
Thread.sleep(3000);
String testValue = searchText.getAttribute("value");
System.out.println(testValue +"::::"+searchKey);
searchText.clear();
//verify correct value in searchbox
Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
}// time pass
}//tc
/*
* Here the DAtaProvider will provide Object array on the basis on ITestContext
* @param c
* @return
*/
@DataProvider(name="SearchProvider")
public Object[][] getDataFromDataprovider(ITestContext c)
{
Object[][] groupArray = null;
for (String group : c.getIncludedGroups())
{
if(group.equalsIgnoreCase("A"))
{
groupArray = new Object[][]
{
{ "Guru99", "India" },
{ "Krishna", "UK" },
{ "Bhupesh", "USA" }
};
break;
}//if if(group.equalsIgnoreCase("A"))
else if(group.equalsIgnoreCase("B"))
{
groupArray = new Object[][]
{
{ "Canada" },
{ "Russia" },
{ "Japan" }
};
}// else if(group.equalsIgnoreCase("B"))
break;
}// for (String group : c.getIncludedGroups())
return groupArray;
}//dp
}//class
Error
[Error] org.testng.TestNGException:
Data Provider public java.lang.Object[][] DP_ParameterTypes.ParameterByITestContextInDataprovider.getDataFromDataprovider(org.testng.ITestContext) must return either Object[][] or Iterator<Object>[], not class [[Ljava.lang.Object;
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:133)
at org.testng.internal.Parameters.handleParameters(Parameters.java:509)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1293)
at org.testng.internal.Invoker.createParameters(Invoker.java:1020)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1110)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
答案 0 :(得分:0)
问题的根本原因基本上是因为null
带注释的方法返回6.14.3
值而不是正确的二维对象数组。
该错误具有误导性。这已在TestNG的较新版本中得到解决,如果您要使用TestNG Data Provider public java.lang.Object[][] DP_ParameterTypes.ParameterByITestContextInDataprovider.getDataFromDataprovider(org.testng.ITestContext) returned a null value
(截至今天的最新发布的TestNG版本),您应该会看到一条错误消息,例如下面的
6.14.3
请执行以下操作:
Object[][] groupArray = new Object[0][0];