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();