diff --git a/src/test/java/serenityswag/inventory/AddProductsActions.java b/src/test/java/serenityswag/inventory/AddProductsActions.java new file mode 100644 index 0000000..ed14b46 --- /dev/null +++ b/src/test/java/serenityswag/inventory/AddProductsActions.java @@ -0,0 +1,18 @@ +package serenityswag.inventory; + +import net.serenitybdd.core.pages.WebElementFacade; +import net.serenitybdd.core.steps.UIInteractionSteps; +import net.thucydides.core.annotations.Step; + +import java.util.List; + +public class AddProductsActions extends UIInteractionSteps { + + ProductList productList; + @Step("Add Products'") + public Integer forAllAvailableItems() { + List itemsAdded = productList.availableItems(); + itemsAdded.forEach(WebElementFacade::click); + return itemsAdded.size(); + } +} diff --git a/src/test/java/serenityswag/inventory/CartDetails.java b/src/test/java/serenityswag/inventory/CartDetails.java new file mode 100644 index 0000000..591f080 --- /dev/null +++ b/src/test/java/serenityswag/inventory/CartDetails.java @@ -0,0 +1,16 @@ +package serenityswag.inventory; + +import net.serenitybdd.core.pages.PageObject; +import net.serenitybdd.core.pages.WebElementFacade; +import net.serenitybdd.core.pages.WebElementState; +import net.serenitybdd.core.pages.WithLocator; + +public class CartDetails extends PageObject { + public String productName() { + return $(".inventory_details_name").getText(); + } + + public WebElementFacade cartLink() { + return $(".shopping_cart_link"); + } +} diff --git a/src/test/java/serenityswag/inventory/InventoryPage.java b/src/test/java/serenityswag/inventory/InventoryPage.java index 2e2e961..5505b1d 100644 --- a/src/test/java/serenityswag/inventory/InventoryPage.java +++ b/src/test/java/serenityswag/inventory/InventoryPage.java @@ -6,4 +6,8 @@ public class InventoryPage extends PageObject { public String getHeading() { return $(".title").getText(); } + + public String getShoppingCartBadge() { + return $(".shopping_cart_badge").getText(); + } } diff --git a/src/test/java/serenityswag/inventory/ProductList.java b/src/test/java/serenityswag/inventory/ProductList.java index ea2b119..77e6079 100644 --- a/src/test/java/serenityswag/inventory/ProductList.java +++ b/src/test/java/serenityswag/inventory/ProductList.java @@ -1,6 +1,7 @@ package serenityswag.inventory; import net.serenitybdd.core.pages.PageObject; +import net.serenitybdd.core.pages.WebElementFacade; import org.openqa.selenium.By; import java.util.List; @@ -18,4 +19,12 @@ public String imageTextForProduct(String productName) { public static By productDetailsLinkFor(String itemName) { return By.linkText(itemName); } + + public List availableItems() { + return findAll("[data-test^=\"add-to-cart\"]"); + } + + public List ItemNameAdded() { + return findAll(".inventory_item_name"); + } } diff --git a/src/test/java/serenityswag/inventory/ViewCartDetailsActions.java b/src/test/java/serenityswag/inventory/ViewCartDetailsActions.java new file mode 100644 index 0000000..8242dda --- /dev/null +++ b/src/test/java/serenityswag/inventory/ViewCartDetailsActions.java @@ -0,0 +1,15 @@ +package serenityswag.inventory; + +import net.serenitybdd.core.steps.UIInteractionSteps; +import net.thucydides.core.annotations.Step; + +public class ViewCartDetailsActions extends UIInteractionSteps { + + CartDetails cartDetails; + + @Step("View cart details for added products'") + public void openCart() { + $(cartDetails.cartLink()).click(); + + } +} diff --git a/src/test/java/serenityswag/inventory/WhenAddingAnItemToTheCart.java b/src/test/java/serenityswag/inventory/WhenAddingAnItemToTheCart.java index f6496fb..48c1b53 100644 --- a/src/test/java/serenityswag/inventory/WhenAddingAnItemToTheCart.java +++ b/src/test/java/serenityswag/inventory/WhenAddingAnItemToTheCart.java @@ -1,15 +1,57 @@ package serenityswag.inventory; +import net.serenitybdd.core.Serenity; +import net.serenitybdd.junit.runners.SerenityRunner; +import net.thucydides.core.annotations.Managed; +import net.thucydides.core.annotations.Steps; +import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.openqa.selenium.WebDriver; +import serenityswag.authentication.LoginActions; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static serenityswag.authentication.User.STANDARD_USER; + +@RunWith(SerenityRunner.class) public class WhenAddingAnItemToTheCart { + @Managed(driver = "firefox") + WebDriver driver; + + @Steps + LoginActions login; + @Steps + AddProductsActions addProducts; + @Steps + ProductList productList; + @Steps + ViewCartDetailsActions cartDetailsActions; + List productsAdded; + InventoryPage inventoryPage; + + @Before + public void login() { + login.as(STANDARD_USER); + + } @Test public void theCorrectItemCountShouldBeShown() { + Integer numberOfItemsAdded = addProducts.forAllAvailableItems(); + Serenity.reportThat("The badge in shopping cart logo increases correctly according with items added", + () -> assertThat(inventoryPage.getShoppingCartBadge()).isEqualTo(numberOfItemsAdded.toString())); } @Test public void allTheItemsShouldAppearInTheCart() { - + productsAdded = productList.titles(); + addProducts.forAllAvailableItems(); + cartDetailsActions.openCart(); + List productsCart = productList.titles(); + Serenity.reportThat("The products added are displayed in the cart", + () -> Assert.assertEquals(productsCart, productsAdded)); } }