Java 需要将DataProvider与Testng一起使用的帮助吗
基类中的数据提供程序Java 需要将DataProvider与Testng一起使用的帮助吗,java,selenium-webdriver,automation,testng,testng-dataprovider,Java,Selenium Webdriver,Automation,Testng,Testng Dataprovider,基类中的数据提供程序 @DataProvider(name = "AAA") public Iterator<Object[]> AAA() throws DataDrivenFrameworkException { String excelResource = System.getProperty("user.dir") + System.getProperty("file.separator") + "resources"
@DataProvider(name = "AAA")
public Iterator<Object[]> AAA()
throws DataDrivenFrameworkException {
String excelResource = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "resources"
+ System.getProperty("file.separator") + "ExcelTestData.xlsx";
ExcelFile excelFile = new ExcelFile(excelResource);
excelFile.setSheetName("SheetNameAAA");
excelFile.setTestCaseHeaderName("TestCaseNameColumn");
List<List<String>> TestData = excelFile
.getDataUsingTestCaseName("TestCase1");
return SeleniumTestNGHelper
.toObjectArrayIterator(TestData);
}
@DataProvider(name = "BBB")
public Iterator<Object[]> BBB()
throws DataDrivenFrameworkException {
String excelResource = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "resources"
+ System.getProperty("file.separator") + "ExcelTestData.xlsx";
ExcelFile excelFile = new ExcelFile(excelResource);
excelFile.setSheetName("SheetNameBBB");
excelFile.setTestCaseHeaderName("TestCaseNameColumn");
List<List<String>> TestData = excelFile
.getDataUsingTestCaseName("TestCase2");
return SeleniumTestNGHelper.toObjectArrayIterator(TestData);
}
@Test(dataProvider = "TestType")
public void PensionPlanPodTests(String UserName, String Password,
String AccountPageTitle, String FullName, String Address,
String DateOfBirth, String PhoneNumber, String ClientNumber,
String InstitutionName, String InstitutionAddress,
String PositionTitle, String Beneficiaries,
String TotalLifeInsuranceAmount) throws InterruptedException {
HomePage HomePG = PageFactory.initElements(driver, HomePage.class);
MainPage MainPG = PageFactory.initElements(driver, MainPage.class);
Map<String, String> logInData = new HashMap<String, String>();
logInData.put("userName", UserName);
logInData.put("password", Password);
HomePG.SignIn(logInData);
// MainPG.CloseTabNotifier();
if (AccountPageTitle.length() > 1) {
MainPG.VerifyPageTitle(AccountPageTitle);
} else {
System.out.println("Not Verifying Page Title ...");
Reporter.log("Not Verifying Page Title ...");
}
Map<String, String> UserInfoData = new HashMap<String, String>();
UserInfoData.put("fullname", FullName);
UserInfoData.put("address", Address);
UserInfoData.put("DOB", DateOfBirth);
UserInfoData.put("Phone", PhoneNumber);
UserInfoData.put("ClientNum", ClientNumber);
MainPG.UserPersonalInformation(UserInfoData);
if (PositionTitle.length() > 1) {
Map<String, String> InstitutionData = new HashMap<String, String>();
InstitutionData.put("institutionName", InstitutionName);
InstitutionData.put("institutionAddress", InstitutionAddress);
InstitutionData.put("positionTitle", PositionTitle);
MainPG.UserInstitutionInformation(InstitutionData);
} else {
System.out
.println("Not Verifying Any Institution Data Because User Do Not Hold Any Position ...");
Reporter.log("Not Verifying Any Institution Data Because User Do Not Hold Any Position ...");
}
if (Beneficiaries.length() > 1) {
MainPG.VerifyLifeInsuranceBeneficiaries(Beneficiaries);
} else {
System.out
.println("Not Verifying Life Insurance Beneficiaries ...");
Reporter.log("Not Verifying Life Insurance Beneficiaries ...");
}
if (TotalLifeInsuranceAmount.length() > 1) {
MainPG.TotalLifeInsuranceAmount(TotalLifeInsuranceAmount);
} else {
System.out.println("Not Verifying Total Life Insurance Amount ...");
Reporter.log("Not Verifying Total Life Insurance Amount ...");
}
MainPG.LogOut();
}
testngxml
<test name="AAA Test Using Chrome" preserve-order="true">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.tests.TestClass" />
</classes>
</test>
想知道是否可以只使用1个数据提供程序而不是2个?我需要将“setSheetName”作为参数传递。想知道如何做到这一点吗?任何反馈都会大有帮助。我需要从文件中的不同页面读取数据
谢谢
*****更新****
基类上的数据提供程序
@Test(description = "AAA Tests", dataProvider = "AAA")
public void testAAA(){
}
@Test(description = "BBB Tests", dataProvider = "BBB")
public void testBBB(){
}
@DataProvider(name = "TestType")
public static Iterator<Object[]> TestType(Method Sheet)
throws DataDrivenFrameworkException {
String SheetName = Sheet.getName();
String excelResource = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "resources"
+ System.getProperty("file.separator") + "ExcelTestData.xlsx";
ExcelFile excelFile = new ExcelFile(excelResource);
excelFile.setSheetName(SheetName);
excelFile.setTestCaseHeaderName("TestCaseNameColumn");
List<List<String>> PensionPlanTestData = excelFile
.getDataUsingTestCaseName("PensionPlanPodTestCase");
return SeleniumTestNGHelper.toObjectArrayIterator(PensionPlanTestData);
}
@DataProvider(name=“TestType”)
公共静态迭代器TestType(方法表)
引发DataDrivenFrameworkException{
String SheetName=Sheet.getName();
字符串excelResource=System.getProperty(“user.dir”)
+System.getProperty(“file.separator”)+“资源”
+System.getProperty(“file.separator”)+“ExcelTestData.xlsx”;
ExcelFile ExcelFile=新的ExcelFile(excelResource);
excelFile.setSheetName(SheetName);
setTestCaseHeaderName(“TestCaseNameColumn”);
列出PensionPlanTestData=excelFile
.getDataUsingTestCaseName(“养老金计划PodTestCase”);
返回SeleniumTestNGHelper.ToObjectArrayInterator(PensionPlantTestData);
}
测试类
@DataProvider(name = "AAA")
public Iterator<Object[]> AAA()
throws DataDrivenFrameworkException {
String excelResource = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "resources"
+ System.getProperty("file.separator") + "ExcelTestData.xlsx";
ExcelFile excelFile = new ExcelFile(excelResource);
excelFile.setSheetName("SheetNameAAA");
excelFile.setTestCaseHeaderName("TestCaseNameColumn");
List<List<String>> TestData = excelFile
.getDataUsingTestCaseName("TestCase1");
return SeleniumTestNGHelper
.toObjectArrayIterator(TestData);
}
@DataProvider(name = "BBB")
public Iterator<Object[]> BBB()
throws DataDrivenFrameworkException {
String excelResource = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "resources"
+ System.getProperty("file.separator") + "ExcelTestData.xlsx";
ExcelFile excelFile = new ExcelFile(excelResource);
excelFile.setSheetName("SheetNameBBB");
excelFile.setTestCaseHeaderName("TestCaseNameColumn");
List<List<String>> TestData = excelFile
.getDataUsingTestCaseName("TestCase2");
return SeleniumTestNGHelper.toObjectArrayIterator(TestData);
}
@Test(dataProvider = "TestType")
public void PensionPlanPodTests(String UserName, String Password,
String AccountPageTitle, String FullName, String Address,
String DateOfBirth, String PhoneNumber, String ClientNumber,
String InstitutionName, String InstitutionAddress,
String PositionTitle, String Beneficiaries,
String TotalLifeInsuranceAmount) throws InterruptedException {
HomePage HomePG = PageFactory.initElements(driver, HomePage.class);
MainPage MainPG = PageFactory.initElements(driver, MainPage.class);
Map<String, String> logInData = new HashMap<String, String>();
logInData.put("userName", UserName);
logInData.put("password", Password);
HomePG.SignIn(logInData);
// MainPG.CloseTabNotifier();
if (AccountPageTitle.length() > 1) {
MainPG.VerifyPageTitle(AccountPageTitle);
} else {
System.out.println("Not Verifying Page Title ...");
Reporter.log("Not Verifying Page Title ...");
}
Map<String, String> UserInfoData = new HashMap<String, String>();
UserInfoData.put("fullname", FullName);
UserInfoData.put("address", Address);
UserInfoData.put("DOB", DateOfBirth);
UserInfoData.put("Phone", PhoneNumber);
UserInfoData.put("ClientNum", ClientNumber);
MainPG.UserPersonalInformation(UserInfoData);
if (PositionTitle.length() > 1) {
Map<String, String> InstitutionData = new HashMap<String, String>();
InstitutionData.put("institutionName", InstitutionName);
InstitutionData.put("institutionAddress", InstitutionAddress);
InstitutionData.put("positionTitle", PositionTitle);
MainPG.UserInstitutionInformation(InstitutionData);
} else {
System.out
.println("Not Verifying Any Institution Data Because User Do Not Hold Any Position ...");
Reporter.log("Not Verifying Any Institution Data Because User Do Not Hold Any Position ...");
}
if (Beneficiaries.length() > 1) {
MainPG.VerifyLifeInsuranceBeneficiaries(Beneficiaries);
} else {
System.out
.println("Not Verifying Life Insurance Beneficiaries ...");
Reporter.log("Not Verifying Life Insurance Beneficiaries ...");
}
if (TotalLifeInsuranceAmount.length() > 1) {
MainPG.TotalLifeInsuranceAmount(TotalLifeInsuranceAmount);
} else {
System.out.println("Not Verifying Total Life Insurance Amount ...");
Reporter.log("Not Verifying Total Life Insurance Amount ...");
}
MainPG.LogOut();
}
@Test(dataProvider=“TestType”)
公共无效PensionPlanPodTests(字符串用户名、字符串密码、,
字符串AccountPageTitle、字符串全名、字符串地址、,
字符串DateOfBirth、字符串PhoneNumber、字符串ClientNumber、,
字符串机构名称,字符串机构地址,
字符串位置标题,字符串受益人,
字符串TotalIfeInsuranceAmount)引发InterruptedException{
HomePage HomePG=PageFactory.initElements(驱动程序,HomePage.class);
MainPage MainPG=PageFactory.initElements(驱动程序,MainPage.class);
Map logInData=新HashMap();
loginda.put(“用户名”,用户名);
loginda.put(“密码”,password);
主页签名(登录数据);
//MainPG.CloseTabNotifier();
如果(AccountPageTitle.length()>1){
主页面验证页面标题(AccountPageTitle);
}否则{
System.out.println(“不验证页面标题…”);
日志(“不验证页面标题…”);
}
Map UserInfoData=newhashmap();
UserInfoData.put(“全名”,全名);
UserInfoData.put(“地址”,address);
UserInfoData.put(“DOB”,出生日期);
UserInfoData.put(“Phone”,PhoneNumber);
UserInfoData.put(“ClientNum”,ClientNumber);
MainPG.用户个人信息(UserInfoData);
if(PositionTitle.length()>1){
Map InstitutionData=新HashMap();
InstitutionData.put(“institutionName”,institutionName);
InstitutionData.put(“institutionAddress”,institutionAddress);
机构数据输入(“职位名称”,职位名称);
主页面用户机构信息(机构数据);
}否则{
系统输出
.println(“不验证任何机构数据,因为用户不持有任何职位…”);
日志(“不验证任何机构数据,因为用户未持有任何职位…”);
}
如果(0.length()>1){
主要页:核实人寿保险受益人(受益人);
}否则{
系统输出
.println(“不核实人寿保险受益人…”);
Reporter.log(“不验证人寿保险受益人…”);
}
如果(TotalIfeInsuranceAmount.length()>1){
主页面Totalifeinsuranceamount(Totalifeinsuranceamount);
}否则{
System.out.println(“未验证人寿保险总金额…”);
Reporter.log(“未验证人寿保险总金额…”);
}
MainPG.LogOut();
}
我现在收到的最新错误:
失败:PensionPlanPodTests
org.testng.TestNGException:
数据提供程序正在尝试传递21个参数,但方法com.tests.DataProviderParametersIntegrationExample#PensionPlanPodTests需要13个参数。您可以有一个数据提供程序,它使用java.lang.reflect.method作为参数。TestNG将通过此参数的当前测试方法。 比如说,如果您有两个名为AAA和BBB的@test方法,您可以读取这些方法名称并将它们用作参数
public class SampleTest {
@Test(dataProvider = "myDP")
public void AAA(String a) {
Assert.assertTrue(true);
}
@Test(dataProvider = "myDP")
public void BBB(String a) {
Assert.assertTrue(true);
}
@DataProvider(name = "myDP")
public Object[][] myDP(Method m) {
String sheetName = m.getName();
return new String[][] { { "a" } };
}
}
@加索卡,非常感谢你的回复。现在我面临另一个问题。我已经更新了上面的代码。我得到的参数编号不匹配。你能告诉我我做错了什么吗?感谢这个问题是不言自明的,您的测试方法只有13个参数,但数据提供程序正在传递21个参数。您可以增加方法参数以接受21个参数,也可以将从DataProvider传递的参数数量减少到13个。感谢您的回复。问题是,不同的工作表有不同数量的字段&我想在所有测试中使用1@DataProvider。您需要如何整合测试中的参数以被所有数据表接受