![](/img/trans.png)
[英]Data driven testing in Selenium using POI and TestNG ..code getting error
[英]how to resolve methodmatcher exception in data driven testing using TestNG?
package testNG;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Datadriven_testing_in_testNG
{
public WebDriver driver;
@DataProvider(name="testData")
public Object[][] readExcel() throws BiffException, IOException
{
File f=new File("C:\\Users\\Akshay\\Desktop\\SELENIUM\\Amazon.xls");
Workbook readwb=Workbook.getWorkbook(f);
Sheet readsht=readwb.getSheet(0);
int noofrows=readsht.getRows();
int noofcolumns=readsht.getColumns();
String inputData[][]= new String[noofrows-1][noofcolumns];
int count=0;
for(int i=1;i<noofrows;i++)
{
for(int j=0;j<noofcolumns;j++)
{
Cell c=readsht.getCell(j,i);
inputData[count][j]=c.getContents();
}
count++;
}
return inputData;
}
@Test(dataProvider="testData")
public static void login(String uname, String password) throws InterruptedException
{
//launch chrome
System.setProperty("webdriver.chrome.driver","C:\\Users\\Akshay\\Desktop\\SELENIUM\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
// navigate to url
driver.get("https://www.amazon.in/");
//click on sign in
driver.findElement(By.xpath("(//span[text()='Sign in'])[3]")).click();
Thread.sleep(5000);
// to enter user name
driver.findElement(By.xpath("//input[@name='email']")).sendKeys(uname);
Thread.sleep(5000);
//to click continue
driver.findElement(By.xpath("//input[@id='ap_email']/following::*[9]")).click();
Thread.sleep(5000);
//to enter password
driver.findElement(By.xpath("//input[@id='ap_password']")).sendKeys(password);
Thread.sleep(5000);
//to click login button
driver.findElement(By.xpath("//input[@id='ap_password']/following::*[6]")).click();
//verification
String actual=driver.getCurrentUrl();
String expected="https://www.amazon.in/ap/cvf/request?arb=19308965-aa56-4383-b19a-acc78826528b";
Assert.assertEquals(actual, expected);
}
@AfterMethod
public void getResult(ITestResult testResult)
{
System.out.println("Testcase name "+testResult.getName());
System.out.println("Testcase Result "+testResult.getStatus());
int status=testResult.getStatus();
if(status==1){
driver.close();
}
else{
//take screenshot
File outfile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(outfile, new File("C:\\Users\\Akshay\\Desktop\\SELENIUM"+testResult.getParameters()[0]+"Defect.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.close();
}
}
這是我編寫的代碼,但它顯示 dataProvider 不匹配錯誤。 我試圖搜索解決方案,但無法得到它控制台中的錯誤消息如下:
失敗:登錄 org.testng.internal.reflect.MethodMatcherException:數據提供者不匹配方法:登錄([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java .lang.String, declaredAnnotations=[]}]) Arguments: [(java.lang.String)akshaymganapati@gmail.com,(java.lang.String)GSaA2509,(java.lang.String)] at org.testng. internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:52) at org.testng.internal.Invoker.injectParameters(Invoker.java:1278) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1171) at org .testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) 在組織。 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(Su iteRunnerWorker.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:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)在 org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
代替測試參數中的字符串 uname 和字符串密碼,使用 Object uname 和 Object 密碼,之后您可以將 Object 轉換為字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.