3

Below code is for submitting the flight details and multiple rows are getting fetched from the Excel and Multiple Rows are getting passed to DataProvider from ExcelSheet. 'VerifyInvalidLogin' is getting skipped and throwing

Exceptions java.lang.RuntimeException and java.lang.NullPointerException 

while passing the data from Excel sheet

package phptravels;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.DataProvider;

public class FlightBooking {
    public WebDriver driver;
    public WebDriverWait wait;
    String appURL="http://phptravels.net";
    //locators
    private By from_Location = By.id("flights_search_from_location_name");
    private By to_Location = By.id("flights_search_to_location_name");
    private By fromDate = By.id("flights_search_outbound_date");
    private By toDate = By.id("flights_search_inbound_date");
    private By cabinClass = By.id("flights_search_cabin_class");
    private By adult = By.id("flights_search_num_adults");
    private By child = By.id("flights_search_num_children");
    private By bySubmit = By.id("flights_search_num_children");
    //WorkBook initialization
    public static HSSFWorkbook WBook = null;
    public static HSSFSheet WSheet= null;
    public static HSSFRow Row;
    public static HSSFCell cell;
    //Opening Firefox
    @BeforeClass
    public void testSetUp(){
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
        wait=new WebDriverWait(driver,5);
    }
    //Data Provide Test
    @Test(dataProvider="FlightBooking")
        public void VerifyInvalidLogin(String frmloc, String toloc, String Datefrom, String Dateto, String Cabin, String Adult, String Child ) {
            driver.navigate().to(appURL);
            driver.findElement(By.linkText("My Account")).click();
            driver.findElement(By.linkText("Login")).click();
            driver.findElement(By.name("username")).sendKeys("new@gmail.com");
            driver.findElement(By.name("password")).sendKeys("Usha@moti");
            driver.findElement(By.name("password")).submit();
            driver.findElement(By.linkText("Flights")).click();

            /*Switching to ifram*/
            WebElement frame=driver.findElement(By.tagName("iframe"));
            driver.switchTo().frame(frame);
            driver.findElement(from_Location).sendKeys(frmloc);
            driver.findElement(to_Location).sendKeys(toloc);
            driver.findElement(fromDate).sendKeys(Datefrom);
            driver.findElement(toDate).sendKeys(Dateto);
            driver.findElement(cabinClass).sendKeys(Cabin);
            driver.findElement(adult).sendKeys(Adult);
            driver.findElement(child).sendKeys(Child);
            //wait for element to be visible and perform click
                    wait.until(ExpectedConditions.visibilityOfElementLocated(bySubmit));
            driver.findElement(bySubmit).click();
            }
    //Data Provide returning arrayobject
    @DataProvider(name="FlightBooking")
    public Object[][] loginData() throws IOException{
        Object[][] arrayObject = getExcelData("D://Users//priya_v//phptravels.com//Flight.xls","Sheet1");
        return arrayObject;

    }
    @Test
    //getExcelData() method
    private String[][] getExcelData(String fileName, String sheetName) throws IOException {
        String[][] arrayExcelData = null;
        try {
            FileInputStream fis = new FileInputStream(fileName);
            HSSFWorkbook WBook = new HSSFWorkbook(fis);
            HSSFSheet WSheet = WBook.getSheet(sheetName); 

            int totalNoOfRows = WSheet.getLastRowNum();
            System.out.println(totalNoOfRows);
            int totalNoOfCols=WSheet.getRow(0).getLastCellNum();
            System.out.println(totalNoOfCols);

            arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];

            for (int i= 1 ; i < totalNoOfRows; i++) {

                for (int j=0; j < totalNoOfCols; j++) {
                    arrayExcelData[i-1][j] = getCellData(sheetName,i,j);
                }
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        return arrayExcelData;
    }
    //getCellData method
    @Test
        public static String getCellData(String Sheet, int row, int col){

                int index = WBook.getSheetIndex(Sheet);
                WSheet = WBook.getSheetAt(index);
                Row = WSheet.getRow(row);
                if (Row == null)
                return "";
                cell = Row.getCell(col);
                if (cell == null)
                return "";

                switch (cell.getCellType())
                {
                case  Cell.CELL_TYPE_STRING:
                return cell.getStringCellValue();               

                case  Cell.CELL_TYPE_BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());          

                case  Cell.CELL_TYPE_BLANK:
                return "";      

                case  Cell.CELL_TYPE_ERROR:
                return cell.getStringCellValue();           

                case  Cell.CELL_TYPE_NUMERIC:
                return String.valueOf(cell.getNumericCellValue());          

                default:
                return "Cell not found";        

                }


    }
     @Test
        public void tearDown() {
            driver.quit();
        }
}
juherr
  • 5,640
  • 1
  • 21
  • 63

0 Answers0