Skip to content

Commit 74e2843

Browse files
committed
Automated checkout functionality scenarios
Signed-off-by: Ganesh Hubale <ganeshhubale03@gmail.com>
1 parent f32feb4 commit 74e2843

File tree

8 files changed

+235
-22
lines changed

8 files changed

+235
-22
lines changed

.github/workflows/code-format.yaml

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
# name: Code formatting
2-
# on: [push, pull_request]
3-
4-
# jobs:
5-
# build:
6-
# name: Prettier
7-
# runs-on: ubuntu-latest
8-
# steps:
9-
# - uses: actions/checkout@v1
10-
# - name: Install NPM
11-
# run: npm install .
12-
# - name: Check code formatting
13-
# run: npm run check
1+
name: Code Formatting
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
build:
9+
name: Prettier
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v3
14+
15+
- name: Install dependencies
16+
run: npm install
17+
18+
- name: Check code formatting
19+
run: npm run check

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ This project automates test scenarios for the website - [https://www.saucedemo.c
9797
- Leave required fields blank (e.g., First Name, Last Name, Zip Code).
9898
- Click "Continue."
9999
- Verify validation messages appear for required fields.
100-
- **Verify Removing an Item from Checkout Page**
101-
- Add multiple items to the cart.
102-
- Proceed to checkout.
103-
- Remove an item from the checkout overview page.
104-
- Verify the total price updates accordingly.
105100
- **Verify Total Price Calculation**
106101
- Add multiple items to the cart.
107102
- Proceed to the checkout overview page.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const inventory = require("../../pages/inventoryPage");
2+
const cart = require("../../pages/cartPage");
3+
const items = require("../../fixtures/items.json");
4+
const users = require("../../fixtures/users.json");
5+
const checkout = require("../../pages/checkoutPage");
6+
7+
describe("Checkout Functionality", () => {
8+
beforeEach("login", ()=> {
9+
cy.login();
10+
});
11+
12+
afterEach("cleanUp", ()=> {
13+
cy.clearCookies();
14+
cy.clearLocalStorage();
15+
});
16+
17+
it("Proceed to Checkout", ()=>{
18+
// List of items to be added to cart
19+
const itemNames = Object.values(items).map(item => item.name);
20+
21+
// Add items to cart
22+
inventory.addMultipleItems(itemNames);
23+
24+
// Go to cart
25+
inventory.goToCart();
26+
27+
// Checkout cart
28+
cart.checkoutCart();
29+
30+
// Enter information
31+
checkout.fillUserInfo(users.user.firstName, users.user.lastName, users.user.zipCode);
32+
33+
// Continue checkout
34+
checkout.continue();
35+
36+
// Verify checkout overiview page
37+
cy.get("span[data-test=title]").should("contain", "Checkout: Overview");
38+
39+
// Verify product items selected in cart
40+
cy.get("div[data-test=inventory-item-name]").then((items) => {
41+
const itemNamesCheckout = [...items].map((item) => item.textContent.trim())
42+
43+
expect(itemNamesCheckout).to.deep.equal(itemNames);
44+
});
45+
});
46+
47+
it("Complete a Purchase", ()=>{
48+
// List of items to be added to cart
49+
const itemNames = Object.values(items).map(item => item.name);
50+
51+
// Add items to cart
52+
inventory.addMultipleItems(itemNames);
53+
54+
// Go to cart
55+
inventory.goToCart();
56+
57+
// Checkout cart
58+
cart.checkoutCart();
59+
60+
// Enter information
61+
checkout.fillUserInfo(users.user.firstName, users.user.lastName, users.user.zipCode);
62+
63+
// Continue checkout
64+
checkout.continue();
65+
66+
// Finish the checkout
67+
checkout.finish();
68+
69+
// Verify checkout complete message
70+
cy.get("h2[data-test=complete-header]").should("contain", "Thank you for your order!");
71+
const completeMsg = "Your order has been dispatched, and will arrive just as fast as the pony can get there!";
72+
cy.get("div[data-test=complete-text]").should("contain", completeMsg);
73+
74+
// Verify cart is empty now
75+
inventory.goToCart();
76+
cy.get("div[data-test=inventory-item-name]").should("not.exist");
77+
});
78+
79+
it("Verify Checkout Validation Messages", ()=>{
80+
// List of items to be added to cart
81+
const itemNames = Object.values(items).map(item => item.name);
82+
83+
// Add items to cart
84+
inventory.addMultipleItems(itemNames);
85+
86+
// Go to cart
87+
inventory.goToCart();
88+
89+
// Checkout cart
90+
cart.checkoutCart();
91+
92+
93+
// Leave user information fields empty and Continue checkout
94+
checkout.continue();
95+
96+
// Verify error message
97+
cy.get("h3[data-test=error]").should("contain", "Error: First Name is required");
98+
99+
// Enter first name
100+
checkout.elements.firstNameInput().clear().type(users.user.firstName)
101+
checkout.continue();
102+
// Verify error message
103+
cy.get("h3[data-test=error]").should("contain", "Error: Last Name is required");
104+
105+
// Enter first name and last name
106+
checkout.elements.lastNameInput().clear().type(users.user.lastName)
107+
checkout.continue();
108+
// Verify error message
109+
cy.get("h3[data-test=error]").should("contain", "Error: Postal Code is required");
110+
});
111+
112+
it("Verify Total Price Calculation", ()=>{
113+
// List of items to be added to cart
114+
const itemNames = Object.values(items).map(item => item.name);
115+
116+
// Add items to cart
117+
inventory.addMultipleItems(itemNames);
118+
119+
// Go to cart
120+
inventory.goToCart();
121+
122+
// Checkout cart
123+
cart.checkoutCart();
124+
125+
// Enter information
126+
checkout.fillUserInfo(users.user.firstName, users.user.lastName, users.user.zipCode);
127+
128+
// Continue checkout and go to overivew page
129+
checkout.continue();
130+
131+
// Verify the total price matches the sum of item prices plus tax.
132+
cy.get("div[data-test=inventory-item-price]").then((prices) => {
133+
const itemPrices = [...prices].map((price) => parseFloat(price.textContent.replace("$", "").trim()));
134+
let totalBeforeTax = 0
135+
itemPrices.forEach((price) => {
136+
totalBeforeTax = totalBeforeTax + price
137+
});
138+
let totalAfterTax = (totalBeforeTax*0.08 + totalBeforeTax).toFixed(2)
139+
cy.get("div[data-test=total-label]").then(($total) => {
140+
let displayedTotal = parseFloat($total.text().replace("Total: $", "").trim());
141+
expect(parseFloat(totalAfterTax)).to.deep.equal(displayedTotal);
142+
});
143+
});
144+
});
145+
});

cypress/fixtures/users.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"user": {
3+
"firstName": "Standard",
4+
"lastName": "Tester",
5+
"zipCode": 445322
6+
}
7+
}

cypress/pages/cartPage.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class Cart{
22

33
elements = {
4-
cartBtn: () => cy.get("div[id=shopping_cart_container]")
4+
cartBtn: () => cy.get("div[id=shopping_cart_container]"),
5+
checkoutbtn: () => cy.get("button[id=checkout]")
56
}
67

78
removeFromCart(itemNames){
@@ -10,5 +11,9 @@ class Cart{
1011
cy.get(`button[id='remove-${name}']`).click();
1112
});
1213
};
14+
15+
checkoutCart(){
16+
this.elements.checkoutbtn().click();
17+
}
1318
};
1419
module.exports = new Cart();

cypress/pages/checkoutPage.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class checkout{
2+
3+
elements = {
4+
firstNameInput: () => cy.get("input[id=first-name]"),
5+
lastNameInput: () => cy.get("input[id=last-name]"),
6+
zipCodeInput: () => cy.get("input[id=postal-code]"),
7+
cancelBtn: () => cy.get("button[id=cancel]"),
8+
continueBtn: () => cy.get("input[id=continue]"),
9+
finishBtn: () => cy.get("button[id=finish]"),
10+
}
11+
12+
fillUserInfo(firstName, lastName, zipCode){
13+
this.elements.firstNameInput().clear().type(firstName);
14+
this.elements.lastNameInput().clear().type(lastName);
15+
this.elements.zipCodeInput().clear().type(zipCode);
16+
};
17+
18+
cancelCheckout(){
19+
this.elements.cancelBtn().click();
20+
}
21+
22+
continue(){
23+
this.elements.continueBtn().click();
24+
}
25+
26+
finish(){
27+
this.elements.finishBtn().click();
28+
}
29+
30+
};
31+
module.exports = new checkout();

package-lock.json

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"cypress:open": "CYPRESS_CRASH_REPORTS=false cypress open",
9-
"cypress:run": "CYPRESS_CRASH_REPORTS=false cypress run"
9+
"cypress:run": "CYPRESS_CRASH_REPORTS=false cypress run",
10+
"check": "prettier --check ."
1011
},
1112
"repository": {
1213
"type": "git",
@@ -16,6 +17,7 @@
1617
"author": "",
1718
"license": "ISC",
1819
"devDependencies": {
19-
"cypress": "^13.17.0"
20+
"cypress": "^13.17.0",
21+
"prettier": "^3.4.2"
2022
}
2123
}

0 commit comments

Comments
 (0)