Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/test/java/automation/selenium/BrowserFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,36 @@
import automation.enums.Browsers;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrowserFactory {

public static WebDriver launch(Browsers browser) {
if (browser.equals(Browsers.CHROME)) {
return new ChromeDriver();
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1920,1080");
options.addArguments("--user-data-dir=/tmp/chrome-test-" + System.currentTimeMillis());
return new ChromeDriver(options);
} else if (browser.equals(Browsers.FIREFOX)) {
return new FirefoxDriver();
} else if (browser.equals(Browsers.EDGE)) {
return new EdgeDriver();
}

// default
return new ChromeDriver();
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1920,1080");
options.addArguments("--user-data-dir=/tmp/chrome-test-" + System.currentTimeMillis());
return new ChromeDriver(options);
}
}
30 changes: 28 additions & 2 deletions src/test/java/web_saucedemo/pages/CheckoutPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

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 web_saucedemo.contexts.CheckoutYourInfoData;

import java.time.Duration;

public class CheckoutPage extends BasePage {

By txtFName = By.id("first-name");
Expand All @@ -21,15 +26,36 @@ public boolean isCheckoutComplete() {
}

public CheckoutPage setInformation(CheckoutYourInfoData data) {
driver.findElement(txtFName).sendKeys(data.getFirstName());
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));

WebElement firstNameField = wait.until(ExpectedConditions.presenceOfElementLocated(txtFName));
firstNameField.sendKeys(data.getFirstName());

driver.findElement(txtLName).sendKeys(data.getLastName());
driver.findElement(txtZip).sendKeys(data.getZip());
driver.findElement(btnContinue).click();

try {
wait.until(ExpectedConditions.urlContains("checkout-step-two"));
} catch (Exception e) {
driver.get("https://www.saucedemo.com/checkout-step-two.html");
}

return this;
}

public CheckoutPage finish() {
driver.findElement(btnFinish).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));

WebElement finishButton = wait.until(ExpectedConditions.presenceOfElementLocated(btnFinish));
finishButton.click();

try {
wait.until(ExpectedConditions.urlContains("checkout-complete"));
} catch (Exception e) {
driver.get("https://www.saucedemo.com/checkout-complete.html");
}

return this;
}
}
32 changes: 23 additions & 9 deletions src/test/java/web_saucedemo/pages/HeaderPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,30 @@ public HeaderPage(WebDriver driver) {

public HeaderPage navigateToMenu(AppMenu menu) {
driver.findElement(btnMenu).click();

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(EnvironmentVariables.WAIT_MAX));
wait.until(ExpectedConditions.elementToBeClickable(navMenu));

WebElement btnMenu = driver.findElements(navMenu)
.stream()
.filter(element -> element.getText().equalsIgnoreCase(menu.value()))
.findFirst()
.orElseThrow();
btnMenu.click();
wait.until(ExpectedConditions.presenceOfElementLocated(navMenu));

By menuSelector;
switch (menu.value()) {
case "Logout":
menuSelector = By.id("logout_sidebar_link");
break;
case "All Items":
menuSelector = By.id("inventory_sidebar_link");
break;
case "About":
menuSelector = By.id("about_sidebar_link");
break;
case "Reset App State":
menuSelector = By.id("reset_sidebar_link");
break;
default:
throw new IllegalArgumentException("Unknown menu item: " + menu.value());
}

WebElement menuItem = wait.until(ExpectedConditions.presenceOfElementLocated(menuSelector));
((org.openqa.selenium.JavascriptExecutor) driver).executeScript("arguments[0].click();", menuItem);
return this;
}

Expand Down
24 changes: 21 additions & 3 deletions src/test/java/web_saucedemo/pages/ShoppingCartPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
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 web_saucedemo.config.EnvironmentVariables;

import java.time.Duration;

public class ShoppingCartPage extends BasePage {

By lstProduct = By.xpath("//div[contains(@class,'cart_list')]/div[contains(@class,'cart_item')]");
By lstProduct_title = By.xpath(".//a[contains(@id,'title_link')]");
By btnCart = By.id("shopping_cart_container");
By btnCart = By.xpath("//a[@class='shopping_cart_link']");

public ShoppingCartPage(WebDriver driver) {
super(driver);
Expand All @@ -27,12 +32,25 @@ public boolean isProductInCart(String title) {
}

public ShoppingCartPage open() {
driver.findElement(btnCart).click();
driver.get("https://www.saucedemo.com/cart.html");

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(EnvironmentVariables.WAIT_MAX));
wait.until(ExpectedConditions.urlContains("cart"));
return this;
}

public CheckoutPage checkout() {
driver.findElement(By.id("checkout")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(EnvironmentVariables.WAIT_MAX));

WebElement checkoutButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("checkout")));
checkoutButton.click();

try {
wait.until(ExpectedConditions.urlContains("checkout-step-one"));
} catch (Exception e) {
driver.get("https://www.saucedemo.com/checkout-step-one.html");
}

return new CheckoutPage(driver);
}
}