Skip to content

Commit

Permalink
bonus53 lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Nov 21, 2023
1 parent b609f50 commit 27e81a5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 100 deletions.
4 changes: 1 addition & 3 deletions cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ module.exports = defineConfig({
'cypress-watch-and-reload': {
watch: ['src/**'],
},
coverage: {
exclude: ['**/src/service*.js'],
},
coverage: false,
},
setupNodeEvents(cypressOn, config) {
// implement node event listeners here
Expand Down
39 changes: 3 additions & 36 deletions cypress/e2e/cart/add-to-cart.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,8 @@ describe('Cart', () => {
cy.location('pathname').should('equal', '/inventory.html')
})

it('adds items to the cart', { viewportHeight: 1200 }, () => {
// confirm the cart badge does not exist at first
InventoryPage.getCartBadge().should('not.exist')

// add the item "Sauce Labs Bike Light" to the cart
// by clicking the button "Add to cart"
InventoryPage.addItemToCart('Sauce Labs Bike Light')
cy.contains('.inventory_item', 'Sauce Labs Bike Light').within(() => {
// the button should switch to "Remove"
cy.contains('button', 'Add to cart').should('not.exist')
cy.contains('button', 'Remove')
})
// the shopping cart link should have number 1
InventoryPage.getCartBadge()
.should('have.text', 1)
.scrollIntoView()
.should('be.visible')
// add the item "Sauce Labs Bolt T-Shirt" to the cart
// by clicking the button "Add to cart"
InventoryPage.addItemToCart('Sauce Labs Bolt T-Shirt')
cy.contains('.inventory_item', 'Sauce Labs Bolt T-Shirt').within(() => {
// the button should switch to "Remove"
cy.contains('button', 'Add to cart').should('not.exist')
cy.contains('button', 'Remove')
})
// the shopping cart link should have number 2
// tip: use https://on.cypress.io/scrollintoview command
// to bring the shopping cart element into the viewport
InventoryPage.getCartBadge()
.should('have.text', 2)
.scrollIntoView()
.should('be.visible')
// there should be two buttons with text "Remove"
// on the inventory page
// https://glebbahmutov.com/cypress-examples/#querying
cy.get('.inventory_item:contains("Remove")').should('have.length', 2)
it('adds the first item to cart', { viewportHeight: 1200 }, () => {
const po = new InventoryPage()
po.addToCart(3)
})
})
88 changes: 27 additions & 61 deletions cypress/support/pages/inventory.page.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,28 @@
// @ts-check
/// <reference types="cypress" />

type SortOrder = 'lohi' | 'hilo' | 'az' | 'za'

export const InventoryPage = {
getCartBadge() {
return cy.get('.shopping_cart_link').find('.shopping_cart_badge')
},

/**
* Add the item to cart by clicking on the "Add to cart" button
* @param name Item name
*/
addItemToCart(name: string) {
cy.contains('.inventory_item', name)
.contains('button', 'Add to cart')
.click()
},

/**
* Sorts inventory by the given sort order
*/
selectSort(sortOrder: SortOrder) {
expect(sortOrder, 'sort order').to.be.oneOf(['lohi', 'hilo', 'az', 'za'])
// find the sort dropdown and select the low to high value
// https://on.cypress.io/select
// Tip: inspect the HTML markup around the sort element
cy.getByTest('product_sort_container').select(sortOrder)
},

/**
* Confirms the inventory items were sorted on the page
* What gets sorted depends on the sort order (text or price)
*/
confirmSorted(sortOrder: SortOrder) {
const assertion =
sortOrder === 'lohi' || sortOrder === 'az'
? 'be.ascending'
: 'be.descending'

if (sortOrder === 'lohi' || sortOrder === 'hilo') {
// find all price elements and map them to numbers
// following the "Lesson 02" solution
// Tip: use cypress-map queries
cy.get('.inventory_item_price')
.map('innerText')
.mapInvoke('slice', 1)
.map(Number)
.print('sorted prices %o')
// confirm the list of numbers is sorted
// https://www.chaijs.com/plugins/chai-sorted/
.should(assertion)
} else {
// items should be sorted by name
cy.get('.inventory_item_name')
.map('innerText')
.print('sorted names %o')
.should(assertion)
}
},
export class InventoryPage {
constructor() {}
addToCart(nthItem: number) {
//reads the name of the item
cy.get('.inventory_item_name')
.eq(nthItem - 1)
.then((product) => {
let prodText = product.text()
cy.log(prodText)
//clicks on the "add to cart" button of that item
cy.get('button.btn_primary.btn_inventory')
.should('have.length', 6)
.eq(nthItem - 1)
.click()
//go to cart
cy.get('a.shopping_cart_link').click()
//take the name of cart item
cy.get('.inventory_item_name').then((product) => {
let cartItemName = product.text()
cy.log(cartItemName)
cy.wrap(prodText === cartItemName).should('be.true')
})
})
}
goToCart() {
cy.get('a.shopping_cart_link').click()
}
}

0 comments on commit 27e81a5

Please sign in to comment.