2016-03-20 21 views
0

我已經使用TestNG編寫了以下測試用例,但它正在跳過MakemyTrip測試用例。我不知道爲什麼。沒有依賴關係,測試數據保存在正確的路徑上。TestNG跳過了「MakemytripHome」方法,雖然沒有依賴關係。爲什麼它沒有執行?

爲什麼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

我在期待這是因爲您沒有初始化數據對象。這就是數據爲空的原因。使用下面一個之前循環

data=new Object[rowcount][colcount]; 

請刪除此@SuppressWarnings(「零」),它會顯示數據爲空值,而不上面的代碼。而不是壓制,好把它弄清楚這個問題。

謝謝你, 穆拉利

+0

如果是這樣的話,你會得到一個NPE(我一樣),而不是一個被忽略的測試。只有當數據集爲空時纔會發生這種情況。 –

0

您正在嘗試爲數據[] []未初始化到任何東西從dataProvider中返回null。您需要使用下面的代碼:

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]; 這應該讓你運行測試