Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
648 views
in Programming by
recategorized by
I want to parameterize my selenium tests using excel sheet. How do I do it?

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
 
Best answer

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");
    }

}


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...