Skip to content

Instantly share code, notes, and snippets.

@SarahElson
Created March 19, 2024 16:24
Show Gist options
  • Save SarahElson/305ad0dcee8148d1af2956feeccf7e77 to your computer and use it in GitHub Desktop.
Save SarahElson/305ad0dcee8148d1af2956feeccf7e77 to your computer and use it in GitHub Desktop.
How To Check If An Element Exists In Cypress
import config from './config.json'
import MainPage from '../../page-objects/components/MainPage'
describe('Elements Exist', () => {
it('Elements exist across the pages', () =>{
cy.visit(`${config.URL3}`)
//Element Exists
cy.get('body')
.then($body => {
if ($body.find('.compare-total').length) {
return '.compare-total'
}
return '.both.text-reset'
})
.then(selector => {
cy.get(selector)
})
//Improve Element Exist Method
MainPage.elExists('.content-products.entry-content.flex-grow-0.order-5.order-md-4 > div > div:nth-of-type(1)')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment