Tuesday, December 1, 2015

Selenium Webdriver and CSV File - Data Driven Simple Example

    We know Selenium can handle data input from external sources like flat file, spread sheets (.xls) and CSV files (Comma separated value). This is actually handled by the programming language we are using with Selenium. People refer to it as Data Driven Testing.

    Lets explore how we can use JAVA to do it and how Selenium handles it.

Look at a sample csv file below:

---------------------------sample.csv--------------------------------
username,password
mckiran123,test1234
------------------------------------------------------------------------

The same file is seen as below when you open it in XL.




    We will now write a very simple method of accessing the sample.csv file, read its contents and feed them to our driver (Selenium) so that the username and password are passed to the webpage we are automating.

 Below are the steps:

---------------------------------------------------------------------

        BufferedReader    reader;
        String[] values;
        String username, passwod;
        reader = new BufferedReader(new FileReader("Files/sample.csv"));
        values = reader.readLine().split(",");
        username = values[0];
        passwod = values[1];


       // If you need to pass these details to your driver, her is how you do it
       driver. findElement(By.id("email")).sendKeys(username);
       driver. findElement(By.id("pwd")).sendKeys(password);   

---------------------------------------------------------------------------


    Now, lets try to see how a complete method looks like that read read all the data inside a csv file. Assume you have manu rows and columns with some test data (ex: some bank account information or passenger data), you would want to read the data in some tabular manner and feed that to your driver.

Here is the complete method:

---------------------------------------------------------------------

public ArrayList<String>[] getAllRecords(String filePath) throws IOException {

        BufferedReader reader = null;
        ArrayList<String>[] data = null;
        String row;
        String[] cellData;
        int n = 0;

        try {
            reader = new BufferedReader(new FileReader(filePath));
            row = reader.readLine();
            if (row != null) {
                cellData = row.split(",");
                n = cellData.length;
                data = new ArrayList[n];

                for (int i = 0; i < n; i++)
                    data[i] = new ArrayList<String>();
            }
            while (row != null) {
                if (row.contains(",")) {
                    cellData = row.split(",");
                    if (cellData.length == n || cellData.length > n) {
                        for (int i = 0; i < n; i++) {
                            if (!cellData[i].isEmpty())
                                data[i].add(cellData[i]);
                            else
                                data[i].add("");
                        }
                    }
                    else
                    {
                        for (int i = 0; i < n - 1; i++) {
                            if (!cellData[i].isEmpty())
                                data[i].add(cellData[i]);
                            else
                                data[i].add("");
                        }
                        data[n - 1].add("");
                    }
                }
                else if (row.isEmpty()) {
                    for (int i = 0; i < n; i++)
                        data[i].add("");
                }
                else {
                    for (int i = 0; i < n; i++)
                        if (i == 0)
                            data[i].add(row);
                        else
                            data[i].add("");
                }
                row = reader.readLine();
            }
            return data;
        }
        catch (Exception e) {
            e.printStackTrace();
            return data;
        }
        finally {
            reader.close();
        }
    }
--------------------------------------------------------------------------- 

   To understnad the above method, you might need your Code Java skills brushed up. If you dont want to do it then simply copy paste the code in your project and call your loved Java Gurus to fix errors (if you face any...).

No comments:

Post a Comment