2

I'm working in selenium with page-object pattern.

My senior developer said: no hard-coded in your automation script. data or parameters in a program in such a way that they cannot be altered without modifying the program. So I am keeping my test data in a property file.

I can do validate a single test case. what is everyone doing to write multiple test cases to automate in a single property file? Is it possible to write a list statement in common for all class files ?

I'm not following any selenium framework. Is data driven framework better to use multiple test data in property file?

Anybody please help me..

This is the drivermanager class

public class DriverManager {
    public static WebDriver driver;
    String baseUrl;
    public Properties prop;

    public DriverManager() throws IOException {
        // Get baseUrl property
        File file = new File("config.properties");
        FileInputStream fileInput = null;
        try {
            fileInput = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //load properties file
        prop = new Properties();
        try {
            prop.load(fileInput);
        } catch (IOException e) {
            e.printStackTrace();
        }

// List or arraylist


        // Start driver
        System.setProperty("webdriver.chrome.driver", "/home/naveen/chromedriver");
        driver = new ChromeDriver();

        baseUrl = prop.getProperty("baseurl");
        driver.get(baseUrl);
        driver.manage().window().maximize();
    }
}

property file

url="http://"
nam=x,y,z,a
pas=q,w,e,r,t
username=q,w,e,r,
name=e,r,t,y,u
address=q,w,e,r,t
phone,1,2,3,,5

What is everyone doing to automate this multiple cases ? which is the easiest way that everyone should following ? I know how to automate a single valid test case and I can't do with multiple test cases . What is everyone doing in this situation ? somebody please help me

1

Note: the Excelhelper class is not the best. consider tweaking it

consider using excel and returning your data in a multi dimensional array based on sheet name, then using TestNG pass in a @Dataprovider

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelHelper {

private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow Row;

// This method is to set the File path and to open the Excel file, Pass
// Excel Path and Sheet name as Arguments to this method
@SuppressWarnings("deprecation")
public static Object[][] getExcelData(String FilePath, String SheetName) throws Exception {
    String[][] tabArray = null;

    try {
        // Access the required test data sheet
        FileInputStream ExcelFile = new FileInputStream(FilePath);
        ExcelWBook = new XSSFWorkbook(ExcelFile);
        ExcelWSheet = ExcelWBook.getSheet(SheetName);

        int totalNoOfRows = ExcelWSheet.getLastRowNum();
        int totalNoOfCols = ExcelWSheet.getRow(1).getPhysicalNumberOfCells();


        tabArray = new String[totalNoOfRows][totalNoOfCols];

        for (int i = 1; i <= totalNoOfRows; i++) {
            for (int j = 0; j < totalNoOfCols; j++) {
                Cell = ExcelWSheet.getRow(i).getCell(j);

                int cel_Type = Cell.getCellType();
                switch (cel_Type) {
                case XSSFCell.CELL_TYPE_NUMERIC: // 0
                    if (DateUtil.isCellDateFormatted(Cell)) {
                        DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
                        tabArray[i - 1][j] = df.format(Cell.getDateCellValue());
                    } else {
                        tabArray[i - 1][j] = String.format("%d", (long) Cell.getNumericCellValue());
                    }
                    break;

                case XSSFCell.CELL_TYPE_STRING: // 1
                    tabArray[i - 1][j] = Cell.getStringCellValue();
                    break;
                }

            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("Could not read the Excel sheet");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Could not read the Excel sheet");
        e.printStackTrace();
    }
    return tabArray;
}
}

TestNG Test:

@Test(dataProvider="exampleDataProvider", groups = { "XYZTest" })
public void example(String caseName,String var1, String var2, String   expectedResult) {
    Assert.assertTrue(somePageObject.MethodToDoSomethingWithTestData(var1, var2).equals(expectedResult));
}

Method for dataprovider: (Referencing an .xlsx file in /resources)

@DataProvider
  public Object[][] exampleDataProvider() throws Exception{
        Object[][] arrayObject = ExcelHelper.getExcelData("resources/TestData.xlsx", "nameOfYourSheet");
        return arrayObject;
    }

A sheet in your .xlsx file as follows: (ensure sheet name matches .getExcelData() secondary paramater Example sheet, sheetname = nameOfYourSheet

Simply add a new sheet, new data provider method and populate the data according for any future data driven tests, based on my example the testNG test will execute twice for a total of 2 tests and we will be ensuring our expected results are correct in a wealth of scenarios (handled by a PageObject method which takes our paramaters and acts upon them. Here we keep our tests simple and maintainable, if test data changes its a one place change, anybody can add new tests by simply editing a spreadsheet and as long as your methods are robust and designed with all scenarios in mind they will execute said tests.

1

Yeah, separate test data from test cases is a good idea. You will achieve better flexibility (as your senior dev said) and better maintainability.

What I do is:

I need to test multiple cases, so when I am executing a login page test. It will test muliple cases and when I execute my user register module it will test multiple cases by this single class looping statement. I got an information in using list or arraylist is a better option.

  • Yeah, there is a better option. You could consider TestNG framework. Using it, instead of using looping to run various test cases, you can use tags to run different test cases. Please follow this link, TestNG tutorial
  • 1
    I edited please check – user24314 Mar 7 '17 at 7:34
  • @comrade, can you please elaborate what you want to achieve by having a loop? – Yu Zhang Mar 7 '17 at 7:47
  • 1
    I need to test multiple cases, so when I am executing a login page test. It will test muliple cases and when I execute my user register module it will test multiple cases by this single class looping statement. I got an information in using list or arraylist is better option. – user24314 Mar 7 '17 at 8:23
  • @comrade, aha, I see what you want to achieve. Will update my answer soon. – Yu Zhang Mar 7 '17 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy