我使用TestNG编写了以下测试用例,但它正在跳过MakemyTrip测试用例。我不知道为什么。没有依赖关系,测试数据保持在正确的路径上。
为什么TestNG没有执行它?
public class Secattempt {
static FileInputStream fis;
static XSSFWorkbook workbook;
static XSSFSheet sheet;
static XSSFRow row;
static XSSFCell cell;
@Test(dataProvider = "getData")
public void MakemytripHome(String departCity, String destCity, String browser) throws AWTException {
Robot robo = new Robot();
WebDriver driver;
if (browser.equals("firefox")) {
driver = new FirefoxDriver();
} else {
System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.makemytrip.com/");
WebElement Depart = driver.findElement(By.id("from_typeahead1"));
WebElement Dest = driver.findElement(By.id("to_typeahead1"));
Depart.clear();
Depart.sendKeys(departCity);
robo.keyPress(KeyEvent.VK_ENTER);
Dest.sendKeys(destCity);
robo.keyPress(KeyEvent.VK_ENTER);
driver.findElement(By.id("flights_submit")).click();
}
@SuppressWarnings("null")
@DataProvider
public Object[][] getData() throws IOException {
Object data[][] = null;
fis = new FileInputStream("D:\\selenium\\data.xlsx");
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
row = sheet.getRow(1);
int rowcount = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
int colcount = row.getLastCellNum();
for (int rnum = 2; rnum <= rowcount; rnum++) {
row = sheet.getRow(rnum);
for (int cnum = 0; cnum < colcount; cnum++) {
cell = row.getCell(cnum);
data[rnum - 2][cnum] = cell.getStringCellValue();
}
}
return data;
}
}
答案 0 :(得分:0)
我期待这是因为你没有初始化数据对象。这就是数据为空的原因。在循环之前使用下面的一个
data=new Object[rowcount][colcount];
请删除此@SuppressWarnings(&#34; null&#34;),它将在没有上述代码的情况下将数据显示为null。而不是抑制,很好地解决问题。
谢谢你, 穆拉利
答案 1 :(得分:0)
您正在尝试从数据提供者返回Null,因为data [] []未初始化为任何内容。您需要使用以下代码:
Object data[][] = null;
fis = new FileInputStream("D:\\selenium\\data.xlsx");
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
row = sheet.getRow(1);
int rowcount = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
int colcount = row.getLastCellNum();
data=new Object[rowcount][colcount];
for (int rnum = 2; rnum <= rowcount; rnum++) {
row = sheet.getRow(rnum);
for (int cnum = 0; cnum < colcount; cnum++) {
cell = row.getCell(cnum);
data[rnum - 2][cnum] = cell.getStringCellValue();
}
}
return data;
观察专栏data=new Object[rowcount][colcount];
这应该让你运行测试