Commit a1624c3a authored by Shaphen Pangburn's avatar Shaphen Pangburn

Refactor to follow a POM and try Page Factory

parent 7827e359
plugins {
id 'java'
}
......
package com.nisum.pompagefactory.practice;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import java.io.IOException;
import java.util.List;
public class AccountSettings {
WebDriver driver;
@FindBy(id = "input-firstName")
WebElement accountFirstName;
@FindBy(id = "input-lastName")
WebElement accountLastName;
@FindBy(id = "emailIdaccount")
WebElement accountEmail;
@FindBy(id = "phoneNumber")
WebElement accountPhoneNumber;
public AccountSettings(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(this.driver, this);
}
public void goToAccountSettings() throws InterruptedException {
HomePage home = new HomePage(driver, "https://www.safeway.com/", false);
home.getAccountDropdown().click();
Thread.sleep(1000);
home.getAccountSettingsButton().click();
}
public static void main(String[] args) throws InterruptedException, IOException {
// get application.properties values and set ChromeDriver
PropertyValues properties = new PropertyValues();
List<String> props = properties.getPropValues();
System.setProperty("webdriver.chrome.driver", "/Users/spangburn/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
// login
String username = props.get(2);
String password = props.get(3);
LoginPage login = new LoginPage(driver, "https://www.safeway.com/", true);
login.loginUser(driver,"https://www.safeway.com/", username, password);
Thread.sleep(5000);
// Navigate to account settings
AccountSettings accountSettings = new AccountSettings(driver);
accountSettings.goToAccountSettings();
Thread.sleep(5000);
driver.close();
}
}
package com.nisum.pompagefactory.practice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
public class HomePage {
WebDriver driver;
public HomePage(WebDriver driver, String url, Boolean newBrowser) {
this.driver = driver;
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
if (newBrowser){ driver.get(url); }
}
public WebElement getMenuNav() { return driver.findElement(By.linkText("Sign In / Up")); }
public WebElement getSignInButton() { return driver.findElement(By.id("sign-in-modal-link")); }
public WebElement getAccountDropdown() { return driver.findElement(By.linkText("Account")); }
public WebElement getAccountSettingsButton() { return driver.findElement(By.xpath("//*[@id=\"menu\"]/div[1]/ul/li[4]/a")); }
public void navigateToLogin() throws InterruptedException {
getMenuNav().click();
Thread.sleep(1000);
getSignInButton().click();
}
}
package com.nisum.pompagefactory.practice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
class LoginPage {
WebDriver driver;
String url;
public LoginPage(WebDriver driver, String url, Boolean newBrowser) {
this.driver = driver;
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
if (newBrowser){ driver.get(url); }
}
public WebElement getUserEmail() { return driver.findElement(By.id("label-email")); }
public WebElement getUserPassword() { return driver.findElement(By.id("label-password")); }
public WebElement getSignInButton() { return driver.findElement(By.id("btnSignIn")); }
public void loginUser(WebDriver driver, String url, String username, String password) throws InterruptedException {
HomePage home = new HomePage(driver, url, false);
home.navigateToLogin();
// username
getUserEmail().sendKeys(username);
// password
getUserPassword().sendKeys(password);
Thread.sleep(1000);
getSignInButton().click();
}
public static void main(String[] args) throws IOException, InterruptedException {
// get application.properties values and set ChromeDriver
PropertyValues properties = new PropertyValues();
List<String> props = properties.getPropValues();
System.setProperty("webdriver.chrome.driver", "/Users/spangburn/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
// set username and password variables and create login scenario
String username = props.get(2);
String password = props.get(3);
LoginPage login = new LoginPage(driver, "https://www.safeway.com/", true);
login.loginUser(driver,"https://www.safeway.com/", username, password);
// Thread.sleep(5000);
// driver.close();
}
}
package com.nisum.pompagefactory.practice;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
public class PropertyValues {
List<String> results;
InputStream inputStream;
public List<String> getPropValues() throws IOException {
try {
Properties prop = new Properties();
String propFileName = "application.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file " + propFileName + " not found");
}
String firstname = prop.getProperty("firstname");
String lastname = prop.getProperty("lastname");
String email = prop.getProperty("email");
String password = prop.getProperty("password");
String phone = prop.getProperty("phone");
results = Arrays.asList(firstname, lastname, email, password, phone);
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
return results;
}
}
firstname=Chef
lastname=Koo
email=zackroc@gmail.com
password=password123
phone=5103063656
package com.nisum.pompagefactory.practice;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SafewayLogin {
@Test
public void testSafewayLogin() throws InterruptedException {
// setup
System.setProperty("webdriver.chrome.driver", "/Users/spangburn/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.safeway.com/");
Thread.sleep(1000);
// navigate Sign In
WebElement signInDropdown = driver.findElement(By.linkText("Sign In / Up"));
signInDropdown.click();
Thread.sleep(1000);
WebElement signInButton = driver.findElement(By.id("sign-in-modal-link"));
signInButton.click();
Thread.sleep(2000);
// login
WebElement userEmail = driver.findElement(By.id("label-email"));
WebElement userPassword = driver.findElement(By.id("label-password"));
WebElement trySignIn = driver.findElement(By.id("btnSignIn"));
userEmail.sendKeys("zackroc@gmail.com");
userPassword.sendKeys("password123");
trySignIn.click();
Thread.sleep(3000);
// Navigate Account Settings
WebElement accountDropdown = driver.findElement(By.linkText("Account"));
accountDropdown.click();
Thread.sleep(1000);
WebElement accountSettingsButton = driver.findElement(By.xpath("//*[@id=\"menu\"]/div[1]/ul/li[4]/a"));
accountSettingsButton.click();
Thread.sleep(3000);
// Assert Account Information Exists
driver.quit();
}
}
package com.nisum.pompagefactory.practice.tests;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestConfirmAccountDetails {
@Test
public void testSafewayLogin() throws InterruptedException {
// setup
System.setProperty("webdriver.chrome.driver", "/Users/spangburn/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.safeway.com/");
Thread.sleep(1000);
// navigate Sign In
WebElement signInDropdown = driver.findElement(By.linkText("Sign In / Up"));
signInDropdown.click();
Thread.sleep(1000);
WebElement signInButton = driver.findElement(By.id("sign-in-modal-link"));
signInButton.click();
Thread.sleep(2000);
// login
WebElement userEmail = driver.findElement(By.id("label-email"));
WebElement userPassword = driver.findElement(By.id("label-password"));
WebElement trySignIn = driver.findElement(By.id("btnSignIn"));
userEmail.sendKeys("zackroc@gmail.com");
userPassword.sendKeys("password123");
trySignIn.click();
Thread.sleep(3000);
// Navigate Account Settings
WebElement accountDropdown = driver.findElement(By.linkText("Account"));
accountDropdown.click();
Thread.sleep(1000);
WebElement accountSettingsButton = driver.findElement(By.xpath("//*[@id=\"menu\"]/div[1]/ul/li[4]/a"));
accountSettingsButton.click();
Thread.sleep(3000);
// Assert Account Information Exists
WebElement getAccountFirstName = driver.findElement(By.id("input-firstName"));
WebElement getAccountLastName = driver.findElement(By.id("input-lastName"));
WebElement getAccountEmail = driver.findElement(By.id("emailIdaccount"));
WebElement getAccountPhoneNumber = driver.findElement(By.id("phoneNumber"));
Assertions.assertEquals("Chef", getAccountFirstName.getAttribute("value"));
Assertions.assertEquals("Koo", getAccountLastName.getAttribute("value"));
Assertions.assertEquals("zackroc@gmail.com", getAccountEmail.getAttribute("value"));
Assertions.assertEquals("5103063656", getAccountPhoneNumber.getAttribute("value"));
driver.quit();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment