/*I have written a @Test method in Selenium which is running twice but i want it to run only once. Basically, i have written @DataProvider method which is returning 2 dimensional array to same @Test method and my @DataProvider is calling another method in other class which also returns a 2 dimensional array. */
//Below is the code from other class i.e., ExcelUtil
package util;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtil {
//Fetching data from excel
public static Object[][] fetchDataFromExcel(String filePath, String sheetName, String methodname){
Map<Object, Object> datamap= new HashMap<Object, Object>();
Object[][] obj = null;
try{
FileInputStream file = new FileInputStream(filePath);
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sh = wb.getSheet(sheetName);
XSSFRow rw = sh.getRow(0);
int totalRows = sh.getLastRowNum();
int cellNum = rw.getLastCellNum();
obj = new Object[totalRows+1][1];
for(int i=0;i<totalRows;i++) {
XSSFRow tstrw = sh.getRow(i);
String txt = tstrw.getCell(0).getStringCellValue();
if(txt.trim().equalsIgnoreCase(methodname.trim())) {
datamap = new HashMap<Object, Object>();
for(int j=0;j<cellNum-1;j++) {
datamap.put(rw.getCell(j+1).getStringCellValue(),
sh.getRow(i).getCell(j+1).getStringCellValue());
}
obj[i][0] = datamap;
//printing two dimensional array
System.out.println("obj["+i+"][0] = " + obj[i][0]);
//break;
}
}
wb.close();
}catch(Exception fetchDataFromExcel) {
fetchDataFromExcel.printStackTrace();
}
return obj;
}
}
//Below is my Test class which consists of @DataProvider and @Test
package com.qa.tests;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import util.ExcelUtil;
import util.LogInPageObject;
public class MyLogoTest {
Method[] methods = MyLogoTest.class.getMethods();
WebDriver driver;
@DataProvider(name = "readDataFromExcel")
public Object[][] dataFromExcel(Method method) {
Object[][] fetchDataFromExcel = null;
try {
String filePath = System.getProperty("user.dir") +
"\\src\\test\\resources\\TestData.xlsx";
//calling fetchDataFromExcel for ExcelUtil class
fetchDataFromExcel = ExcelUtil.fetchDataFromExcel(filePath, "Login", method.getName());
} catch (Exception dataFromExcel) {
dataFromExcel.printStackTrace();
}
return fetchDataFromExcel;
}
@BeforeTest
public void invokeBrowser() {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://automationpractice.com/index.php");
}
//This method below is running two times but i want it should run only once
@Test(dataProvider="readDataFromExcel")
public void logIn(Map<String, String> data) {
try {
driver.findElement(By.xpath(LogInPageObject.signInXpath)).click();
Thread.sleep(10000);
}catch(Exception logIn) {
logIn.printStackTrace();
}
}
@AfterTest
public void afterTest() {
driver.close();
}
}
Output in the Console after running
PASSED: logIn(null)
PASSED: logIn({Username=kumar7000@gmail.com, Password=123456})
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
And below is the data in my excel
Can anyone help me with this, I don't want "PASSED: logIn(null)"
in my output.
The problem is in
Object[][] obj = null;
int totalRows = sh.getLastRowNum();
obj = new Object[totalRows+1][1];
totalRows
is 1, when you initialize obj
you are giving it 2 rows. The first row contains the data from the file, but the second row remains null
(the initial value).
@Test(dataProvider="readDataFromExcel")
See two values, Map
with values and null
, so two tests are being created
logIn({Username=kumar7000@gmail.com, Password=123456})
// and
logIn(null)
Just fix the array initialization
obj = new Object[totalRows][1];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.