Skip to content

Commit

Permalink
implement an API for setting and validating state
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Oct 20, 2022
1 parent 31ee1a6 commit 07ed400
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -78,4 +79,12 @@ public Object waitUntil(Callable<Object> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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) + "]");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public InventoryPage(SauceDemoApp app) {
super(app);
}

public void visit() {
public InventoryPage visit() {
browser.get(URL);
return this;
}

public void viewBoltTShirtProduct() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 07ed400

Please sign in to comment.