Join 500+ Software Testing professionals. Solve problem and get your problem solved. Register Now!

0
What is data (Excel Sheet) parameterisation in Selenium WebDriver? How can I do it?

Solved 1 Answers 153 Views Programming
0 1
I want to parameterize my selenium tests using excel sheet. How do I do it?

1 Answer

0
Best answer
0

Data parameterisation is all about reading data from excel/csv file and hence changing behaviour of test based on input.

you can use below code to read CSV file.

package com.kagrana.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * This file helps read data from CSV file.
 * @author mayur
 *
 */

public class ReadCSV {
    private String fileName;
    
    /**
     * Constructor
     * @param fileName
     */
    public ReadCSV(String fileName) {
        this.fileName = fileName;
    }
    /**
     * First value is your key and each row is your value.
     * Size of Hashmap is = no of rows - 1(headers)
     *
     * @return this will return list of hashmap
     */
    public List<HashMap<String, String>> run() {
        List<HashMap<String, String>> mapList = new ArrayList<HashMap<String, String>>();
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";
        try {
            String[] keys;
            br = new BufferedReader(new FileReader(this.fileName));
            HashMap<String, String> map =null;
            // Read first line from CSV file and use it as key
            if ((line = br.readLine()) != null) {
                keys = line.split(cvsSplitBy);
            } else
                return null;
            // Read each line after first line and use it as values
            while ((line = br.readLine()) != null) {
                map = new HashMap<String, String>();
                String[] values = line.split(cvsSplitBy);
                for (int i = 0; i < keys.length; i++) {
                    // Store key and values to hash map
                    map.put(keys[i], values[i]);
                }
                // add hash map to list
                mapList.add(map);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return mapList;
    }
}


Above code is implemented with TestNG.

package com.kagrana.util;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ReadCSVTest {
    private Log log;

    @DataProvider(name = "myDataProvider")
    public Object[][] getData() {
        ReadCSV obj = new ReadCSV("csvsample.csv");
        List<HashMap<String, String>> mapList = obj.run();
        return MiscellaneousFunctions.listHashMapToObject(mapList);
    }

    @Test(dataProvider = "myDataProvider")
    public void test(HashMap<String, String> map) {
        log = new Log();
        Set<String> keys = map.keySet();
        for (String key : keys)
            log.write("Key:\t" + key + "\tValue: " + map.get(key));
        for(int i=0;i<5;i++)
            log.write("\n");
    }

}

answered by (151 points)

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
...