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: 数据提供者不匹配的方法:login([Parameter {index = 0, type = java.lang.String,clarifiedAnnotations = []},参数{index = 1, type = java.lang.String,clarifiedAnnotations = []}])参数: [(java.lang.String)[email protected],(java.lang.String)GSaA2509,(java.lang.String)] 在 org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:52) 在org.testng.internal.Invoker.injectParameters(Invoker.java:1278) 在org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1171) 在 org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 在org.testng.TestRunner.privateRun(TestRunner.java:756)处 org.testng.TestRunner.run(TestRunner.java:610)在 org.testng.SuiteRunner.runTest(SuiteRunner.java:387)在 org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)在 org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)在 org.testng.SuiteRunner.run(SuiteRunner.java:289)在 org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)在 org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)在 org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)在 org.testng.TestNG.runSuitesLocally(TestNG.java:1218)在 org.testng.TestNG.runSuites(TestNG.java:1133)在 org.testng.TestNG.run(TestNG.java:1104)在 org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) 在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
代替测试参数中的String uname和string密码,使用Object uname和Object password,之后您可以将该对象转换为字符串。