Seeking assistance with my journey in learning javascript and cypress. I am having trouble locating an item on a page and adding it to the shopping cart.
After using .find
, I am not receiving any response. There are no errors in cypress, but the item is not being added to the cart, and cy.log
isn't displaying anything either.
/// <reference types="Cypress"/>
it ('CountElements', () => {
cy.visit ('https://www.saucedemo.com/v1/inventory.html')
cy.get('.inventory_list >')
cy.get('.inventory_list >').as ('PopularProducts')
cy.get('@PopularProducts').should('have.length', 6)
})
it ('Adding "top" element to the shopping cart from the main page', function (){
cy.visit ('https://www.saucedemo.com/v1/inventory.html')
cy.get('.inventory_list >').as ('PopularProducts')
cy.get('@PopularProducts')
.find ('.inventory_item_name')
.each(($el,index,$list) => {
if($el.attr('.inventory_item_name') == 'Sauce Labs Onesie'){
cy.log ('Item found as intended')
cy.get ('@PopularProducts').eq(index).contains('ADD TO CART').click()
}
})
})
})
I have been able to successfully add the item to the cart by explicitly specifying its position in the list, but I need the functionality to work without knowing the exact location.
it ('Adding "top" element to the shopping cart from the main page', function (){
cy.visit ('https://www.saucedemo.com/v1/inventory.html')
cy.get('.inventory_list >').as ('PopularProducts')
cy.get('@PopularProducts')
.find ('.inventory_item_name')
.each(($el,index,$list) => {
if($el.attr('.inventory_item_name') == 'Sauce Labs Onesie'){
cy.log ('Item found as intended')
}
})
cy.get ('@PopularProducts').eq(5).contains('ADD TO CART').click()
})