我正在使用Selenium Webdriver进行自动化。我为此使用TestNG。 FactoryMethod引发附件错误。
请帮我。我之前已经回答过有关相同问题的问题,但没有发现对我有用。
以下是我遇到的错误。
Exception in thread "main" org.testng.TestNGException:
The factory method class automation.actions.test.P2PPortalRegistrationActionTest.createInstances() threw an exception
at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:121)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:158)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:40)
at org.testng.TestRunner.initMethods(TestRunner.java:401)
在调试代码时,我发现com.getDeclaringClass()的此catch块上正在发生异常:-
catch (Throwable t) {
ConstructorOrMethod com = getConstructorOrMethod();
throw new TestNGException("The factory method "
+ com.getDeclaringClass() + "." + com.getName()
+ "() threw an exception", t);
}
存在@Factory批注的代码:- 在代码的下面这一行( P2PPortalRegistrationActionTest test = new P2PPortalRegistrationActionTest(recordData); ),控制权直接转到上述catch块并引发上述错误。
@Factory
public static Object[] createInstances(ITestContext context) throws FileNotFoundException, IOException {
// Get attachment path from parameters
String parametersJson = context.getCurrentXmlTest().getParameter("P2PPortalRegistrationActionParams");
ObjectMapper mapper = new ObjectMapper();
P2PPortalRegistrationActionParams params = mapper.readValue(parametersJson, P2PPortalRegistrationActionParams.class);
// Read excel data
Object[][] data = ExcelFileReader.getExcelData(params.getAttachmentPath());
System.out.println("--Data length is---" +data);
// Created instances of Test class for each record of Test Data
List<P2PPortalRegistrationActionTest> listTestClassInstances = new ArrayList<P2PPortalRegistrationActionTest>();
for (int i = 0; i < data.length; i++) {
@SuppressWarnings("unchecked")
HashMap<String, String> recordData = (HashMap<String, String>) data[i][0];
System.out.println("Record Data---"+recordData);
P2PPortalRegistrationActionTest test = new P2PPortalRegistrationActionTest(recordData);
System.out.println("test after setting record data"+test);
test.setTestInstanceExecutionOrder(i + 1);
listTestClassInstances.add(i, test);
}
return listTestClassInstances.toArray();
}
请帮助我解决此问题。我已经在这里解决了所有与此相关的已回答问题,但没有发现有用的地方。
预先感谢