Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add scenarios to check cart badge and content #2

Open
wants to merge 1 commit into
base: action-classes-exercise
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions src/test/java/serenityswag/inventory/AddProductsActions.java
Original file line number Diff line number Diff line change
@@ -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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Integer forAllAvailableItems() {
public int forAllAvailableItems() {

you usually use the primitive version

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning the number of items added feels not good...
i'd rather have

Suggested change
public Integer forAllAvailableItems() {
public void addAllVisibleItems() {

and later assert "visibleItems.size equals cartBadgeNumber"

List<WebElementFacade> itemsAdded = productList.availableItems();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List<WebElementFacade> itemsAdded = productList.availableItems();
List<WebElementFacade> addToCartButtons = productList.addToCartButtons();

name could be better i think. You are not returning the items you can buy but the button to click

itemsAdded.forEach(WebElementFacade::click);
return itemsAdded.size();
}
}
16 changes: 16 additions & 0 deletions src/test/java/serenityswag/inventory/CartDetails.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
4 changes: 4 additions & 0 deletions src/test/java/serenityswag/inventory/InventoryPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public class InventoryPage extends PageObject {
public String getHeading() {
return $(".title").getText();
}

public String getShoppingCartBadge() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public String getShoppingCartBadge() {
public String numberShownOnCart() {

i find the name not great, you are returning text, not a shoppingCartBadge

return $(".shopping_cart_badge").getText();
}
}
9 changes: 9 additions & 0 deletions src/test/java/serenityswag/inventory/ProductList.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,4 +19,12 @@ public String imageTextForProduct(String productName) {
public static By productDetailsLinkFor(String itemName) {
return By.linkText(itemName);
}

public List<WebElementFacade> availableItems() {
return findAll("[data-test^=\"add-to-cart\"]");
}

public List<WebElementFacade> ItemNameAdded() {
return findAll(".inventory_item_name");
}
}
15 changes: 15 additions & 0 deletions src/test/java/serenityswag/inventory/ViewCartDetailsActions.java
Original file line number Diff line number Diff line change
@@ -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();

}
}
Original file line number Diff line number Diff line change
@@ -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<String> productsAdded;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List<String> 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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
productsAdded = productList.titles();
List<String> visibleProductTitles = productList.titles();

addProducts.forAllAvailableItems();
cartDetailsActions.openCart();
List<String> productsCart = productList.titles();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

productList.titles(); this is the same as the first line of this test, though we are on a different page now, that a bit confusing to me. it works because the selector is the same.. but i'd create a new page object for that

Serenity.reportThat("The products added are displayed in the cart",
() -> Assert.assertEquals(productsCart, productsAdded));
}
}