/*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();
}
}
运行后在控制台中输出
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
===============================================
以下是我的excel中的数据
谁能帮我这个忙,我不想在输出中使用
"PASSED: logIn(null)"
。 最佳答案
问题出在
Object[][] obj = null;
int totalRows = sh.getLastRowNum();
obj = new Object[totalRows+1][1];
totalRows
为1,当您初始化obj
时,将为其分配2行。第一行包含文件中的数据,但第二行保留null
(初始值)。@Test(dataProvider="readDataFromExcel")
查看两个值,带值和
Map
的null
,因此正在创建两个测试logIn({Username=kumar7000@gmail.com, Password=123456})
// and
logIn(null)
只需修复数组初始化
obj = new Object[totalRows][1];