How to create a structure to support reading a csv file for each web page under test

39 views
Skip to first unread message

Steve Harlington

unread,
Jun 4, 2017, 10:42:43 PM6/4/17
to Selenium Users
Hi,

I am not a developer so full examples would be appreciated.

I am running Java/Selenium Webdriver tests within JUnit and have the following code working. The issue is that when extending this to support potentially hundreds of fields that would populate data on web pages under test it will get messy. There must be a better structure to support scaleability. Looking for a solution where each csv file (which would contain data for one web page) could populate a separate collection of data. Then within the tests a clean way to access the data. So how to separate the data collections into separate classes, then use the data within the main test (class)?

package automationFramework;

import java.io.File;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value = Parameterized.class)
public class TestDataA {
   
    public String firstname;
    public String surname;
    public String username;
    public String password;
   
    @Parameters
    public static Collection<Object[]> testData() {
        final List<Object[]> cases = new LinkedList<>();
        // Set location of CSV file
        try (final Scanner reader = new Scanner(new File("C:\\Test_Data\\Data.csv"))) {
            while (reader.hasNext()) {
                final String[] raw = reader.nextLine().trim().split(",");
                final String firstname = raw[0];
                final String surname = raw[1];
                final String username = raw[2];
                final String password = raw[3];

                cases.add(new Object[] { firstname, surname, username, password });
            }
        } catch (Exception e) {
            throw new IllegalStateException("Couldn't load data", e);
        }

        return cases;
    }

    public TestDataA_(String firstname, String surname, String username, String password) {
        this.firstname = firstname;
        this.surname = surname;
        this.username = username;
        this.password = password;
    }
   
    @Test
    public void test() throws Exception {
         System.out.println(firstname);
         System.out.println(surname);
         System.out.println(username);
         System.out.println(password);
       
    }

}

David

unread,
Jun 5, 2017, 2:29:01 PM6/5/17
to Selenium Users
Are you thinking of handling filling in form fields where there are many possibilities of varying form field IDs/names, # of form fields, and form field types? Or many different variations of input data to the form fields but will always be the same form fields to fill in?

If the latter, that's easy, and others can chime in. You simply define multiple CSVs with different data values to test for different data cases, and feed that to your test class in a data driven way.

If the former, I would embed the form field type (text field, checkbox, radio button, select list, etc.) and form field identifier (ID/name/CSS/xpath along with ID type) into the CSV as well so that your CSV would contain the data to fill in for the matching field ID and it's type. If the data structure then would make the CSV cumbersome (especially if you have varying # of fields so your CSV column count would vary), then an alternate data format may be preferable like YAML, XML, or JSON. But in any case, doing it this way, your test class is abstract w/o having to explicitly define all the varying form field names & their types. You dynamically handle them based on lookup up the field ID/type info from the data file along with the data to send/set to that field. Your test class would then be abstract 7 simple, but you'd have a larger more complex data file.

Steve Harlington

unread,
Jun 5, 2017, 11:40:20 PM6/5/17
to Selenium Users
The scenarios and are business scenarios so they tend to traverse through many pages, and each page may have many form fields that need to be populated. I was thinking of having CSV's that reflect the fields on each page, and having the data loaded so it could be used in one or more test. This I cannot work out how to do? I can load a separate CSV for each @Test, but that is not what I am after. What I want is the data all loaded into separate collections or something so any data set can be used by any test, this is so the CSV files are not huge and it is clearer what the data is for.


package automationFramework;

import java.io.File;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import com.applitools.eyes.RectangleSize;

@RunWith(Enclosed.class)
public class TestData {

    @RunWith(Parameterized.class)
    public static class TestDataA extends LifeCycle_Management_TestData{

       
        public String firstname;
        public String surname;
        public String username;
        public String password;

        @Parameters
        public static Collection<Object[]> testDataA() {

            final List<Object[]> cases = new LinkedList<>();
            // Set location of CSV file
            try (final Scanner reader = new Scanner(new File("C:\\Test_Data\\Data.csv"))) {
                while (reader.hasNext()) {
                    final String[] raw = reader.nextLine().trim().split(",");
                    final String firstname = raw[0];
                    final String surname = raw[1];
                    final String username = raw[2];
                    final String password = raw[3];

                    cases.add(new Object[] { firstname, surname, username, password });
                }
            } catch (Exception e) {
                throw new IllegalStateException("Couldn't load data", e);
            }

            return cases;
        }

        public TestDataA(String firstname, String surname, String username, String password) {

            this.firstname = firstname;
            this.surname = surname;
            this.username = username;
            this.password = password;
        }

        @Test
        public void test1() throws Exception {
            String TestName = name.getMethodName();
           
            driver = eyes.open(driver, "Base_Test", "Test: " + TestName, new RectangleSize(1200, 800));
           
            System.out.println("Start Test: " + TestName);
            System.out.println(firstname);
            System.out.println(surname);
            System.out.println(username);
            System.out.println(password);

            driver.get(baseUrl + "/");
           
            driver.findElement(By.id("lst-ib")).sendKeys(firstname);
            driver.findElement(By.id("_fZl")).click();
            eyes.checkWindow("Search Result");
           
            eyes.close();

        }
    }

    @RunWith(Parameterized.class)
    public static class TestDataB extends LifeCycle_Management_TestData{

        public String firstname1;
        public String surname1;
        public String username1;
        public String password1;

        @Parameters
        public static Collection<Object[]> testDataB() {

            final List<Object[]> cases = new LinkedList<>();
            // Set location of CSV file
            try (final Scanner reader = new Scanner(new File("C:\\Test_Data\\Data1.csv"))) {

                while (reader.hasNext()) {
                    final String[] raw = reader.nextLine().trim().split(",");
                    final String firstname1 = raw[0];
                    final String surname1 = raw[1];
                    final String username1 = raw[2];
                    final String password1 = raw[3];

                    cases.add(new Object[] { firstname1, surname1, username1, password1 });

                }
            } catch (Exception e) {
                throw new IllegalStateException("Couldn't load data", e);
            }

            return cases;
        }

        public TestDataB(String firstname1, String surname1, String username1, String password1) {
            this.firstname1 = firstname1;
            this.surname1 = surname1;
            this.username1 = username1;
            this.password1 = password1;
        }

        @Test
        public void test2() throws Exception {
            String TestName = name.getMethodName();
           
            driver = eyes.open(driver, "Base_Test", "Test: " + TestName, new RectangleSize(1200, 800));
           
            System.out.println("Start Test: " + TestName);

            System.out.println(firstname1);
            System.out.println(surname1);
            System.out.println(username1);
            System.out.println(password1);
           
            driver.get(baseUrl + "/");
            driver.findElement(By.id("lst-ib")).sendKeys(firstname1);
            driver.findElement(By.id("_fZl")).click();
            eyes.checkWindow("Search Result");
           
            eyes.close();
Reply all
Reply to author
Forward
0 new messages