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+ } ) ;
0 commit comments