From 2099342aa2867b057e99e8e309d41a77809e9214 Mon Sep 17 00:00:00 2001 From: Artiom Bozieac Date: Wed, 6 Mar 2024 22:55:16 +0200 Subject: [PATCH] #80 Add pages for products and cart --- .../selenium/it/pages/BaseHeaderPage.java | 40 +++++++++++++++++++ .../shop/selenium/it/pages/BasePage.java | 11 +++-- .../selenium/it/pages/cart/MyCartPage.java | 37 +++++++++++++++++ .../it/pages/products/AddProductPage.java | 39 ++++++++++++++++++ .../it/pages/products/MyProductsPage.java | 37 +++++++++++++++++ .../it/pages/products/ProductsPage.java | 34 ++++++++++++++++ online-shop-ui/src/app/components/Header.tsx | 8 ++-- .../pages/products/AddProductPage.tsx | 1 + .../pages/products/EditProductPage.tsx | 10 +++-- .../components/pages/products/ProductCard.tsx | 2 +- .../pages/products/ProductsContainer.tsx | 2 +- 11 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/BaseHeaderPage.java create mode 100644 online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/cart/MyCartPage.java create mode 100644 online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/AddProductPage.java create mode 100644 online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/MyProductsPage.java create mode 100644 online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/ProductsPage.java diff --git a/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/BaseHeaderPage.java b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/BaseHeaderPage.java new file mode 100644 index 0000000..4e79bc3 --- /dev/null +++ b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/BaseHeaderPage.java @@ -0,0 +1,40 @@ +package com.yashmerino.online.shop.selenium.it.pages; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Base class for pages that have the header component. + */ +public class BaseHeaderPage extends BasePage { + /** + * My Profile header button. + */ + private final By profileButton = By.id("profile-button"); + + /** + * My Cart header button. + */ + private final By myCartButton = By.id("my-cart-button"); + + /** + * My Products header button. + */ + private final By myProductsButton = By.id("my-products-button"); + + /** + * Add Product header button. + */ + private final By addProductsButton = By.id("add-products-button"); + + /** + * Constructor. + * + * @param driver web driver to perform browser actions. + * @param wait wait web driver to wait until certain conditions met. + */ + public BaseHeaderPage(WebDriver driver, WebDriverWait wait) { + super(driver, wait); + } +} diff --git a/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/BasePage.java b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/BasePage.java index 15b9eb1..96d64e7 100644 --- a/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/BasePage.java +++ b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/BasePage.java @@ -12,17 +12,22 @@ public class BasePage { /** * Web Driver to make browser actions. */ - protected WebDriver driver; + protected final WebDriver driver; /** * Wait web driver to wait until certain conditions met. */ - protected WebDriverWait wait; + protected final WebDriverWait wait; /** * Success alert element. */ - protected By successAlert = By.id("alert-success"); + protected final By successAlert = By.id("alert-success"); + + /** + * Submit button. + */ + protected final By submitButton = By.id("submit-button"); /** * Constructor. diff --git a/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/cart/MyCartPage.java b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/cart/MyCartPage.java new file mode 100644 index 0000000..c07c4f8 --- /dev/null +++ b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/cart/MyCartPage.java @@ -0,0 +1,37 @@ +package com.yashmerino.online.shop.selenium.it.pages.cart; + +import com.yashmerino.online.shop.selenium.it.pages.BaseHeaderPage; +import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Page Object Model that represents my cart page. + */ +public class MyCartPage extends BaseHeaderPage { + /** + * Constructor. + * + * @param driver web driver to perform browser actions. + * @param wait wait web driver to wait until certain conditions met. + */ + public MyCartPage(final WebDriver driver, final WebDriverWait wait) { + super(driver, wait); + } + + /** + * Checks if a product exists. + * + * @param productName is the product's name. + * @return true or false. + */ + public boolean productExists(final String productName) { + try { + driver.findElement(By.xpath(String.format("//h6[contains(string(), %s)]", productName))); + return true; + } catch (NoSuchElementException e) { + return false; + } + } +} diff --git a/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/AddProductPage.java b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/AddProductPage.java new file mode 100644 index 0000000..2ec23b0 --- /dev/null +++ b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/AddProductPage.java @@ -0,0 +1,39 @@ +package com.yashmerino.online.shop.selenium.it.pages.products; + +import com.yashmerino.online.shop.selenium.it.pages.BaseHeaderPage; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Page Object Model that represents add product page. + */ +public class AddProductPage extends BaseHeaderPage { + /** + * Name field. + */ + private final By nameField = By.id("name-field"); + + /** + * Constructor. + * + * @param driver web driver to perform browser actions. + * @param wait wait web driver to wait until certain conditions met. + */ + public AddProductPage(final WebDriver driver, final WebDriverWait wait) { + super(driver, wait); + } + + /** + * Adds a product. + * + * @param productName is the product's name. + */ + public void addProductToCard(final String productName) { + driver.findElement(nameField).sendKeys(productName); + driver.findElement(submitButton).click(); + + wait.until(ExpectedConditions.visibilityOfElementLocated(successAlert)); + } +} diff --git a/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/MyProductsPage.java b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/MyProductsPage.java new file mode 100644 index 0000000..a35f4f1 --- /dev/null +++ b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/MyProductsPage.java @@ -0,0 +1,37 @@ +package com.yashmerino.online.shop.selenium.it.pages.products; + +import com.yashmerino.online.shop.selenium.it.pages.BaseHeaderPage; +import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Page Object Model that represents my products page. + */ +public class MyProductsPage extends BaseHeaderPage { + /** + * Constructor. + * + * @param driver web driver to perform browser actions. + * @param wait wait web driver to wait until certain conditions met. + */ + public MyProductsPage(final WebDriver driver, final WebDriverWait wait) { + super(driver, wait); + } + + /** + * Checks if a product exists. + * + * @param productName is the product's name. + * @return true or false. + */ + public boolean productExists(final String productName) { + try { + driver.findElement(By.xpath(String.format("//h6[contains(string(), %s)]", productName))); + return true; + } catch (NoSuchElementException e) { + return false; + } + } +} diff --git a/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/ProductsPage.java b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/ProductsPage.java new file mode 100644 index 0000000..1d5886f --- /dev/null +++ b/online-shop-it/src/test/java/com/yashmerino/online/shop/selenium/it/pages/products/ProductsPage.java @@ -0,0 +1,34 @@ +package com.yashmerino.online.shop.selenium.it.pages.products; + +import com.yashmerino.online.shop.selenium.it.pages.BaseHeaderPage; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Page Object Model that represents products page. + */ +public class ProductsPage extends BaseHeaderPage { + + /** + * Constructor. + * + * @param driver web driver to perform browser actions. + * @param wait wait web driver to wait until certain conditions met. + */ + public ProductsPage(final WebDriver driver, final WebDriverWait wait) { + super(driver, wait); + } + + /** + * Adds a product to the card. + * + * @param productId is the product's id. + */ + public void addProductToCard(final String productId) { + driver.findElement(By.id("add-product-" + productId)).click(); + + wait.until(ExpectedConditions.visibilityOfElementLocated(successAlert)); + } +} diff --git a/online-shop-ui/src/app/components/Header.tsx b/online-shop-ui/src/app/components/Header.tsx index a971b15..1467d9d 100644 --- a/online-shop-ui/src/app/components/Header.tsx +++ b/online-shop-ui/src/app/components/Header.tsx @@ -199,20 +199,20 @@ const Header = () => { {getTranslation(lang, "online_shop")} - + {// @ts-ignore roles[0].name == "USER" ? - : + : null } {// @ts-ignore roles[0].name == "SELLER" ? - : + : null } {// @ts-ignore roles[0].name == "SELLER" ? - : + : null } {// @ts-ignore diff --git a/online-shop-ui/src/app/components/pages/products/AddProductPage.tsx b/online-shop-ui/src/app/components/pages/products/AddProductPage.tsx index b860f3b..e5d60b7 100644 --- a/online-shop-ui/src/app/components/pages/products/AddProductPage.tsx +++ b/online-shop-ui/src/app/components/pages/products/AddProductPage.tsx @@ -256,6 +256,7 @@ const AddProductPage = () => {