Commit c83e927b authored by Shaphen Pangburn's avatar Shaphen Pangburn

Refactor code to use explicit wait statements

parent 3c0e1f16
package com.nisum.pompagefactory.practice;
package com.nisum.pompagefactory.practice.pages;
import com.nisum.pompagefactory.practice.pages.HomePage;
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 org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class AccountSettings {
public class AccountSettingsPage {
WebDriver driver;
@FindBy(xpath = "//*[@id=\"input-firstName\"]")
public WebElement accountFirstName;
......@@ -20,42 +21,22 @@ public class AccountSettings {
@FindBy(xpath = "//*[@id=\"phoneNumber\"]")
public WebElement accountPhoneNumber;
public AccountSettings(WebDriver driver) {
public AccountSettingsPage(WebDriver driver) {
this.driver = driver;
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
PageFactory.initElements(this.driver, this);
}
public void goToAccountSettings() throws InterruptedException {
public void goToAccountSettings() {
WebDriverWait wait = new WebDriverWait(driver, 5);
// setup
HomePage home = new HomePage(driver, "https://www.safeway.com/", false);
wait.until(ExpectedConditions.elementToBeClickable(home.getAccountDropdown()));
home.getAccountDropdown().click();
Thread.sleep(1000);
// click account settings button
wait.until(ExpectedConditions.elementToBeClickable(home.getAccountSettingsButton()));
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);
//
// System.out.println(accountSettings.accountFirstName.getAttribute("value"));
// System.out.println(accountSettings.accountLastName.getAttribute("value"));
// System.out.println(accountSettings.accountEmail.getAttribute("value"));
// System.out.println(accountSettings.accountPhoneNumber.getAttribute("value"));
//
// driver.close();
// }
}
package com.nisum.pompagefactory.practice;
package com.nisum.pompagefactory.practice.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
......@@ -20,9 +22,15 @@ public class HomePage {
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 {
public void navigateToLogin() {
WebDriverWait wait = new WebDriverWait(driver, 5);
// click dropdown menu
wait.until(ExpectedConditions.elementToBeClickable(getMenuNav()));
getMenuNav().click();
Thread.sleep(1000);
// click sign in button
wait.until(ExpectedConditions.elementToBeClickable(getSignInButton()));
getSignInButton().click();
}
}
package com.nisum.pompagefactory.practice;
package com.nisum.pompagefactory.practice.pages;
import com.nisum.pompagefactory.practice.pages.HomePage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class LoginPage {
......@@ -21,35 +21,24 @@ public class LoginPage {
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 WebElement getSubmitButton() { return driver.findElement(By.id("btnSignIn")); }
public void loginUser(WebDriver driver, String url, String username, String password) throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 5);
// setup
HomePage home = new HomePage(driver, url, false);
home.navigateToLogin();
// username
// enter credentials
wait.until(ExpectedConditions.elementToBeClickable(getUserEmail()));
getUserEmail().sendKeys(username);
// password
wait.until(ExpectedConditions.elementToBeClickable(getUserPassword()));
getUserPassword().sendKeys(password);
Thread.sleep(1000);
getSignInButton().click();
// click submit button
wait.until(ExpectedConditions.elementToBeClickable(getSubmitButton()));
getSubmitButton().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.tests;
import com.nisum.pompagefactory.practice.AccountSettings;
import com.nisum.pompagefactory.practice.LoginPage;
import com.nisum.pompagefactory.practice.pages.AccountSettingsPage;
import com.nisum.pompagefactory.practice.pages.LoginPage;
import com.nisum.pompagefactory.practice.PropertyValues;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
import java.util.List;
......@@ -15,7 +17,7 @@ public class TestConfirmAccountDetails {
@Test
public void testAccountSettingsInformation() throws InterruptedException, IOException {
// get application.properties values and set ChromeDriver
// get application.properties values and set WebDriver properties
PropertyValues properties = new PropertyValues();
List<String> props = properties.getPropValues();
String email = props.get(2);
......@@ -26,22 +28,22 @@ public class TestConfirmAccountDetails {
System.setProperty("webdriver.chrome.driver", "/Users/spangburn/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 5);
// login
LoginPage loginPage = new LoginPage(driver, "https://www.safeway.com/", true);
loginPage.loginUser(driver,"https://www.safeway.com/", email, password);
Thread.sleep(5000);
// Navigate to account settings
AccountSettings accountSettings = new AccountSettings(driver);
accountSettings.goToAccountSettings();
Thread.sleep(5000);
AccountSettingsPage accountSettingsPage = new AccountSettingsPage(driver);
accountSettingsPage.goToAccountSettings();
// Assert Information is correct
Assertions.assertEquals(firstName, accountSettings.accountFirstName.getAttribute("value"));
Assertions.assertEquals(lastName, accountSettings.accountLastName.getAttribute("value"));
Assertions.assertEquals(email, accountSettings.accountEmail.getAttribute("value"));
Assertions.assertEquals(phoneNumber, accountSettings.accountPhoneNumber.getAttribute("value"));
wait.until(ExpectedConditions.elementToBeClickable(accountSettingsPage.accountFirstName));
Assertions.assertEquals(firstName, accountSettingsPage.accountFirstName.getAttribute("value"));
Assertions.assertEquals(lastName, accountSettingsPage.accountLastName.getAttribute("value"));
Assertions.assertEquals(email, accountSettingsPage.accountEmail.getAttribute("value"));
Assertions.assertEquals(phoneNumber, accountSettingsPage.accountPhoneNumber.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