0
votes
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();
    }

    }

This is the code I have written, but it's showing dataProvider mismatch error. I tried to search for solution but couldn't get it Error message in console is as below:

FAILED: login org.testng.internal.reflect.MethodMatcherException: Data provider mismatch Method: login([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}]) Arguments: [(java.lang.String)[email protected],(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) at org.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(SuiteRunnerWorker.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) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

1

1 Answers

0
votes

Instead of String uname and string password in test parameters, use Object uname and Object password and after that you can convert that Object to string.