I am trying to perform data driven testing in TestNG.
But I am getting error:
FAILED: loginWay2sms
org.testng.internal.reflect.MethodMatcherException: Data provider mismatch.
What's wrong in my script.
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.WebDriverException;
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 location
File f=new File("C:\\Users\\Dell\\Desktop\\SELENIUM\\New folder\\akshay\\src\\akshay\\src\\testNG\\Waytosms testdata.xls");
//reading workbook
Workbook w=Workbook.getWorkbook(f);
//reading sheet
Sheet s=w.getSheet(0);
//Storing no.of rows and columns
int noofRows=s.getRows();
int noofCols=s.getColumns();
//for getting the input data from excel
String inputData[][]=new String[noofRows-1][noofCols];
int count=0;
for(int i=1;i<noofRows;i++){
for(int j=0;j<noofCols;j++){
Cell c=s.getCell(j,i);
inputData[count][j]=c.getContents();
}
count++;
}
return inputData;
}
@Test(dataProvider="testData")
public void loginways2sms(String uname,String pwd) throws InterruptedException
{//open browser
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Dell\\Desktop\\SELENIUM\\chromedriver.exe");
driver=new ChromeDriver();
//navigate to url
driver.get("https://www.edureka.co/");
Thread.sleep(4000);
//for username
driver.findElement(By.id("mobileNo")).sendKeys(uname);
//for password
driver.findElement(By.id("password")).sendKeys(pwd);
//for clicking on login
driver.findElement(By.xpath("(//button[contains(text(),'Login')])[1]")).click();
//verification point
String eurl="https://www.edureka.co/blog/pmp-certification/";
Assert.assertEquals(driver.getCurrentUrl(), eurl);
}
@AfterMethod
public void getTestResult(ITestResult testResult){
System.out.println("Test case name: "+testResult.getName());
System.out.println("Test case Result: "+testResult.getStatus());
//if status=1-->pass, status=2--->fail
int status=testResult.getStatus();
if(status==1){
driver.close();
}
else
{
try {
//take screenshot
File outfile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(outfile, new File("C:\\Users\\Dell\\Desktop\\SELENIUM\\New folder\\akshay\\src\\akshay\\src\\testNG"+testResult.getParameters()[0]+"Defect.jpg"));
} catch (WebDriverException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
driver.close();
}