Skip to content

Commit

Permalink
Data-Driven Testing with JUnit and CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
Woohyeok Kim, Aaron committed Sep 16, 2018
1 parent 7fbee37 commit db53d06
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
118 changes: 118 additions & 0 deletions SeleniumTraining/src/demo/NewAccountDDT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package demo;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import demo.utilities.CSV;
import demo.utilities.DriverFactory;

@RunWith(value = Parameterized.class)
public class NewAccountDDT {
private String name, email, phone, gender, password, country;
private boolean weeklyEmail, monthlyEmail, occasionalEmail = false;
WebDriver driver;
WebElement nameElement, emailElement, phoneElememnt, passwordElement, verifyPasswordElement, countryElement,
femaleRadio, maleRadio, weeklyCheckBox, monthlyCheckBox,submitButton;

// Constructor that parses parameters to the test method
public NewAccountDDT(String name, String email, String phone, String gender, String password, String country,
String weeklyEmail, String monthlyEmail, String occasionalEmail) {
this.name = name;
this.email = email;
this.phone = phone;
this.gender = gender;
this.password = password;
this.country = country;
if (weeklyEmail.equals("TRUE")) this.weeklyEmail = true;
if (monthlyEmail.equals("TRUE")) this.monthlyEmail = true;
if (occasionalEmail.equals("TRUE")) this.occasionalEmail = true;
}

// This is our test method
@Test
public void newAccountTest() {
System.out.println("New Record: " + name + " " + email + " " + phone + " " + gender + " " + password + " " + country + " " + weeklyEmail + " " + monthlyEmail + " " + occasionalEmail);

defineWebElement();

nameElement.sendKeys(name);
emailElement.sendKeys(email);
phoneElememnt.sendKeys(phone); // rel XPath.
passwordElement.sendKeys(password);
verifyPasswordElement.sendKeys(password);

// Radio button
if (gender.equalsIgnoreCase("Female")) {
femaleRadio.click();
} else {
maleRadio.click();
}

// for drop-down
if (weeklyEmail) {
if (!weeklyCheckBox.isSelected()) {
weeklyCheckBox.click();
}
} else {
if (weeklyCheckBox.isSelected()) {
weeklyCheckBox.click();
}
}
submitButton.click();

// 4. Get confirmation & close browser
String result = driver.findElement(By.id("MainContent_lblTransactionResult")).getText();
String expected = "Customer information added successfully";

if (result.contains(expected)) {
System.out.println("RESULT: " + result);
} else {
System.out.println("TEST FAILED");
}
}


// This annotated method is designed to pass parameters into the class via constructor
@Parameters
public static List<String[]> getData() {
return CSV.get("/Users/woohyeok.kim/Downloads/UserAccounts.csv");
}

@Before
public void setUp() {
// 1. Define web driver
driver = DriverFactory.openWebDriver("chrome");
driver.get("http://sdettraining.com/trguitransactions/AccountManagement.aspx");
driver.findElement(By.xpath("//*[@id='ctl01']/div[3]/div[2]/div/div[2]/a")).click();
}

@After
public void tearDown() {
// 5. Close the browser
driver.quit();
}

public void defineWebElement() {
// 2. Open Browser to Account Management Page >> Click and Create Account
nameElement = driver.findElement(By.name("ctl00$MainContent$txtFirstName"));
emailElement = driver.findElement(By.id("MainContent_txtEmail"));
phoneElememnt = driver.findElement(By.xpath("//*[@id='MainContent_txtHomePhone']"));
passwordElement = driver.findElement(By.cssSelector("input[type='password']"));
verifyPasswordElement = driver.findElement(By.name("ctl00$MainContent$txtVerifyPassword"));
countryElement = driver.findElement(By.id("MainContent_menuCountry"));
femaleRadio = driver.findElement(By.name("ctl00$MainContent$Gender"));
maleRadio = driver.findElement(By.id("MainContent_Male"));
weeklyCheckBox = driver.findElement(By.name("ctl00$MainContent$checkWeeklyEmail"));
monthlyCheckBox = driver.findElement(By.id("MainContent_btnSubmit"));
submitButton = driver.findElement(By.id("MainContent_btnSubmit"));
}
}
1 change: 0 additions & 1 deletion SeleniumTraining/src/demo/utilities/CSV.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static List<String[]> get(String filename) {
} catch (IOException e) {
System.out.println("Could not read");
}

return data;
}
}

0 comments on commit db53d06

Please sign in to comment.