Skip to content

Commit

Permalink
Automated cart functionality test scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Ganesh Hubale <ganeshhubale03@gmail.com>
  • Loading branch information
ganeshhubale committed Jan 8, 2025
1 parent e8988ba commit c13cd80
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 6 deletions.
105 changes: 105 additions & 0 deletions cypress/e2e/products/cart.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const items = require("../../fixtures/items.json");
const cart = require("../../pages/cartPage");

describe("Cart Functionality", () => {

beforeEach("login", () => {
cy.login()
});

afterEach("CleanUp", () => {
cy.clearCookies();
cy.clearLocalStorage();
});

it("Add a Single Item to the Cart", () => {
// Choose item and Click on it. For example - Item name - Sauce Labs Backpack
const itemName = items.sauceLabsBackpack.name;

cart.addSingleItem(itemName);

// Go to cart
cart.goToCart();

// Confirm only one item in cart
cy.get("div[data-test=inventory-item-name]").then((items) => {
const totalItems = [...items].map((item) => item.textContent.trim())

expect(totalItems.length).to.equal(1);
expect(totalItems[0]).to.equals(itemName)
});
});

it("Add Multiple Items to the Cart", () => {
// Add multiple items to cart
const itemNames = Object.values(items).map(item => item.name);

cart.addMultipleItems(itemNames);

// Go to cart
cart.goToCart();

// Confirm multiple items added in cart
cy.get("div[data-test=inventory-item-name]").then((items) => {
const totalItems = [...items].map((item) => item.textContent.trim())

expect(totalItems.length).to.equal(itemNames.length);
});
});

it("Remove an Item from the Cart", () => {
// Add multiple items to cart
const itemNames = Object.values(items).map(item => item.name);

cart.addMultipleItems(itemNames);

// Go to cart
cart.goToCart();

// Remove an item from cart and check count reduced
cart.removeFromCart([itemNames[0]]);

// Confirm total count reduced by one
cy.get("div[data-test=inventory-item-name]").then((items) => {
const totalItems = [...items].map((item) => item.textContent.trim())

expect(totalItems.length).to.equal(itemNames.length-1);
});
});

it("Empty the Cart", () => {
// Add multiple items to cart
const itemNames = Object.values(items).map(item => item.name);

cart.addMultipleItems(itemNames);

// Go to cart
cart.goToCart();

// Remove an item from cart and check count reduced
cart.removeFromCart(itemNames);

// Confirm the cart is empty
cy.get("div[data-test=inventory-item-name]").should("not.exist");
});

it("Verify Cart Persistence", () => {
// Add multiple items to cart
const itemNames = Object.values(items).map(item => item.name);

cart.addMultipleItems(itemNames);

// Reload the inventory
cy.reload();

// Go to cart
cart.goToCart();

// Confirm total count of items is equal to total items in cart
cy.get("div[data-test=inventory-item-name]").then((items) => {
const totalItems = [...items].map((item) => item.textContent.trim())

expect(totalItems.length).to.equal(itemNames.length);
});
});
});
10 changes: 5 additions & 5 deletions cypress/e2e/products/sort-items.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const inventoryPage = require("../../pages/inventoryPage")
describe("Sort product items", () => {
beforeEach("login", () => {
cy.login()
})
});

afterEach(() => {
cy.clearCookies();
Expand All @@ -25,7 +25,7 @@ describe("Sort product items", () => {
const sortedNames = [...itemNames].sort();
expect(itemNames).to.deep.equal(sortedNames);
});
})
});

it("Verify sort items by Name: Z-A", () => {
inventoryPage.sort_by_name("za")
Expand All @@ -41,7 +41,7 @@ describe("Sort product items", () => {
const sortedNames = [...itemNames].sort((a,b) => b.localeCompare(a));
expect(itemNames).to.deep.equal(sortedNames);
});
})
});

it("Verify sort items by price: low to high", () => {
inventoryPage.sort_by_price("lohi")
Expand All @@ -57,7 +57,7 @@ describe("Sort product items", () => {
const sortedPrices = [...itemPrices].sort((a, b) => a - b);;
expect(itemPrices).to.deep.equal(sortedPrices);
});
})
});

it("Verify sort items by price: high to low", () => {
inventoryPage.sort_by_price("hilo")
Expand All @@ -73,5 +73,5 @@ describe("Sort product items", () => {
const sortedPrices = [...itemPrices].sort((a, b) => b - a);
expect(itemPrices).to.deep.equal(sortedPrices);
});
})
});
});
11 changes: 11 additions & 0 deletions cypress/fixtures/items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"sauceLabsBackpack": {
"name": "Sauce Labs Backpack"
},
"sauceLabsBikelight": {
"name": "Sauce Labs Bike Light"
},
"testallthings": {
"name": "Test.allTheThings() T-Shirt (Red)"
}
}
36 changes: 36 additions & 0 deletions cypress/pages/cartPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Cart{

elements = {
itemName: () => cy.get("div[data-test=inventory-item-name]"),
addToCart: () => cy.get("button[id=add-to-cart]"),
cartBtn: () => cy.get("div[id=shopping_cart_container]")
}

addSingleItem(name){
// Click on item
this.elements.itemName().contains(name).click();

// Add to card
this.elements.addToCart().click()
};

addMultipleItems(itemNames){
itemNames.forEach((itemName) => {
const name = itemName.trim().toLowerCase().replace(/\s+/g, '-');
cy.get(`button[id='add-to-cart-${name}']`).click();
});
};

removeFromCart(itemNames){
itemNames.forEach((itemName) => {
const name = itemName.trim().toLowerCase().replace(/\s+/g, '-');
cy.get(`button[id='remove-${name}']`).click();
});
};

goToCart(){
// Click on cart button
this.elements.cartBtn().click();
}
};
module.exports = new Cart();
2 changes: 1 addition & 1 deletion cypress/pages/inventoryPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class Inventory{
}

}
module.exports = new Inventory();
module.exports = new Inventory();

0 comments on commit c13cd80

Please sign in to comment.