From 07ed400cfa886ac82d33ce2f3218b951f78ada6b Mon Sep 17 00:00:00 2001 From: titusfortner Date: Tue, 3 May 2022 01:18:21 -0500 Subject: [PATCH] implement an API for setting and validating state --- .../advancedselenium/saucedemo/Browser.java | 9 +++++ .../saucedemo/SauceDemoApp.java | 21 ++++++----- .../saucedemo/apis/AuthenticateAPI.java | 18 ++++++++++ .../saucedemo/apis/BaseAPI.java | 29 +++++++++++++++ .../saucedemo/apis/CartAPI.java | 19 ++++++++++ .../saucedemo/apis/InventoryAPI.java | 16 +++++++++ .../saucedemo/apis/SauceDemoAPIFactory.java | 35 +++++++++++++++++++ .../saucedemo/pages/HeaderSection.java | 11 ------ .../saucedemo/pages/InventoryPage.java | 3 +- .../saucedemo/tests/CartTest.java | 5 ++- 10 files changed, 143 insertions(+), 23 deletions(-) create mode 100644 src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/AuthenticateAPI.java create mode 100644 src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/BaseAPI.java create mode 100644 src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/CartAPI.java create mode 100644 src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/InventoryAPI.java create mode 100644 src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/SauceDemoAPIFactory.java diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/Browser.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/Browser.java index a8a9139..2ce414a 100644 --- a/src/test/java/com/saucelabs/advancedselenium/saucedemo/Browser.java +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/Browser.java @@ -1,6 +1,7 @@ package com.saucelabs.advancedselenium.saucedemo; import org.openqa.selenium.By; +import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.WebDriverWait; @@ -78,4 +79,12 @@ public Object waitUntil(Callable block) { } }); } + + public void addCookie(String name, String value) { + driver.manage().addCookie(new Cookie(name, value)); + } + + public Cookie getCookie(String name) { + return driver.manage().getCookieNamed(name); + } } diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/SauceDemoApp.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/SauceDemoApp.java index 3872adf..8e9a941 100644 --- a/src/test/java/com/saucelabs/advancedselenium/saucedemo/SauceDemoApp.java +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/SauceDemoApp.java @@ -1,7 +1,7 @@ package com.saucelabs.advancedselenium.saucedemo; -import com.saucelabs.advancedselenium.saucedemo.pages.HeaderSection; -import com.saucelabs.advancedselenium.saucedemo.pages.InventoryPage; +import com.saucelabs.advancedselenium.saucedemo.apis.SauceDemoAPIFactory; +import com.saucelabs.advancedselenium.saucedemo.pages.HomePage; import com.saucelabs.advancedselenium.saucedemo.pages.SauceDemoPageFactory; public class SauceDemoApp { @@ -19,22 +19,27 @@ public SauceDemoPageFactory pages() { return new SauceDemoPageFactory(this); } + public SauceDemoAPIFactory apis() { + return new SauceDemoAPIFactory(browser); + } + public void loginSuccessfully() { - pages().getHomePage().visit().loginSuccessfully(); + pages().getHomePage().visit(); + browser.waitUntil(() -> HomePage.URL.equals(browser.getCurrentUrl())); + apis().getAuthenticateAPI().authenticate(); + pages().getInventoryPage().visit(); } public void addItemToCart() { loginSuccessfully(); - pages().getInventoryPage().addItemSuccessfully(); + apis().getInventoryAPI().addItemSuccessfully(); } public Integer getNumberItemsInCart() { - HeaderSection headerSection = new HeaderSection(this); - return headerSection.getNumberItemsInCart(); + return apis().getCartAPI().getItemCount(); } public Boolean isAuthenticated() { - new InventoryPage(this).visit(); - return InventoryPage.URL.equals(browser.getCurrentUrl()); + return apis().getAuthenticateAPI().isAuthenticated(); } } diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/AuthenticateAPI.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/AuthenticateAPI.java new file mode 100644 index 0000000..85f9b90 --- /dev/null +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/AuthenticateAPI.java @@ -0,0 +1,18 @@ +package com.saucelabs.advancedselenium.saucedemo.apis; + +import com.saucelabs.advancedselenium.saucedemo.Browser; +import com.saucelabs.advancedselenium.saucedemo.data.User; + +public class AuthenticateAPI extends BaseAPI { + public AuthenticateAPI(Browser browser) { + super(browser); + } + + public void authenticate() { + addCookie("session-username", User.valid().getUsername()); + } + + public Boolean isAuthenticated() { + return getCookie("session-username") != null; + } +} diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/BaseAPI.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/BaseAPI.java new file mode 100644 index 0000000..3361545 --- /dev/null +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/BaseAPI.java @@ -0,0 +1,29 @@ +package com.saucelabs.advancedselenium.saucedemo.apis; + +import org.openqa.selenium.Cookie; +import org.openqa.selenium.html5.LocalStorage; +import org.openqa.selenium.remote.RemoteExecuteMethod; +import org.openqa.selenium.remote.html5.RemoteWebStorage; +import com.saucelabs.advancedselenium.saucedemo.Browser; + +public class BaseAPI { + protected final Browser browser; + + public BaseAPI(Browser browser) { + this.browser = browser; + } + + protected void addCookie(String name, String value) { + browser.addCookie(name, value); + } + + protected Cookie getCookie(String name) { + return browser.getCookie(name); + } + + protected LocalStorage getLocalStorage() { + RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(browser.getDriver()); + RemoteWebStorage webStorage = new RemoteWebStorage(executeMethod); + return webStorage.getLocalStorage(); + } +} diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/CartAPI.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/CartAPI.java new file mode 100644 index 0000000..da11a26 --- /dev/null +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/CartAPI.java @@ -0,0 +1,19 @@ +package com.saucelabs.advancedselenium.saucedemo.apis; + +import com.saucelabs.advancedselenium.saucedemo.Browser; + +import java.util.Arrays; + +public class CartAPI extends BaseAPI { + public CartAPI(Browser browser) { + super(browser); + } + + public Integer getItemCount() { + String item = getLocalStorage().getItem("cart-contents"); + if (item == null || "[]".equals(item)) { + return 0; + } + return Arrays.asList(item.substring(1, item.length() - 1).split(",")).size(); + } +} diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/InventoryAPI.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/InventoryAPI.java new file mode 100644 index 0000000..9c2f823 --- /dev/null +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/InventoryAPI.java @@ -0,0 +1,16 @@ +package com.saucelabs.advancedselenium.saucedemo.apis; + +import com.saucelabs.advancedselenium.saucedemo.Browser; + +import java.util.Random; + +public class InventoryAPI extends BaseAPI{ + public InventoryAPI(Browser browser) { + super(browser); + } + + public void addItemSuccessfully() { + int productID = new Random().nextInt(5); + getLocalStorage().setItem("cart-contents", "[" + String.valueOf(productID) + "]"); + } +} diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/SauceDemoAPIFactory.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/SauceDemoAPIFactory.java new file mode 100644 index 0000000..fe0eeb7 --- /dev/null +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/SauceDemoAPIFactory.java @@ -0,0 +1,35 @@ +package com.saucelabs.advancedselenium.saucedemo.apis; + +import com.saucelabs.advancedselenium.saucedemo.Browser; + +public class SauceDemoAPIFactory { + private final Browser browser; + private AuthenticateAPI authenticateAPI; + private InventoryAPI inventoryAPI; + private CartAPI cartAPI; + + public SauceDemoAPIFactory(Browser browser) { + this.browser = browser; + } + + public AuthenticateAPI getAuthenticateAPI() { + if (this.authenticateAPI == null) { + this.authenticateAPI = new AuthenticateAPI(browser); + } + return this.authenticateAPI; + } + + public InventoryAPI getInventoryAPI() { + if (this.inventoryAPI == null) { + this.inventoryAPI = new InventoryAPI(browser); + } + return this.inventoryAPI; + } + + public CartAPI getCartAPI() { + if (this.cartAPI == null) { + this.cartAPI = new CartAPI(browser); + } + return this.cartAPI; + } +} diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/pages/HeaderSection.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/pages/HeaderSection.java index bfad2b1..b637caf 100644 --- a/src/test/java/com/saucelabs/advancedselenium/saucedemo/pages/HeaderSection.java +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/pages/HeaderSection.java @@ -5,26 +5,15 @@ import org.openqa.selenium.TimeoutException; import com.saucelabs.advancedselenium.saucedemo.elements.Button; import com.saucelabs.advancedselenium.saucedemo.elements.Element; -import com.saucelabs.advancedselenium.saucedemo.elements.ElementList; public class HeaderSection extends BasePage { private final Button menuButton = browser.getButton(By.id("react-burger-menu-btn")); private final Element logoutLink = browser.getElement(By.id("logout_sidebar_link")); - private final ElementList shoppingCartItems = browser.getElements(By.className("shopping_cart_badge")); public HeaderSection(SauceDemoApp app) { super(app); } - public Integer getNumberItemsInCart() { - shoppingCartItems.reset(); - if (shoppingCartItems.isEmpty()) { - return 0; - } else { - return Integer.valueOf(shoppingCartItems.getFirst().getText()); - } - } - public void logOutSuccessfully() { menuButton.click(); logoutLink.click(); diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/pages/InventoryPage.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/pages/InventoryPage.java index d2b2ac6..5b1213a 100644 --- a/src/test/java/com/saucelabs/advancedselenium/saucedemo/pages/InventoryPage.java +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/pages/InventoryPage.java @@ -16,8 +16,9 @@ public InventoryPage(SauceDemoApp app) { super(app); } - public void visit() { + public InventoryPage visit() { browser.get(URL); + return this; } public void viewBoltTShirtProduct() { diff --git a/src/test/java/com/saucelabs/advancedselenium/saucedemo/tests/CartTest.java b/src/test/java/com/saucelabs/advancedselenium/saucedemo/tests/CartTest.java index e6fd07f..b01d3f0 100644 --- a/src/test/java/com/saucelabs/advancedselenium/saucedemo/tests/CartTest.java +++ b/src/test/java/com/saucelabs/advancedselenium/saucedemo/tests/CartTest.java @@ -38,9 +38,8 @@ public void addFromInventoryPage() { @Test public void removeFromInventoryPage() { - sauceDemoApp.loginSuccessfully(); - InventoryPage inventoryPage = sauceDemoApp.pages().getInventoryPage(); - inventoryPage.addItemSuccessfully(); + sauceDemoApp.addItemToCart(); + InventoryPage inventoryPage = sauceDemoApp.pages().getInventoryPage().visit(); Assertions.assertDoesNotThrow(inventoryPage::removeItemSuccessfully); }