this is my first question of stackoverflow community, I am currently working with Cypress and have created test case that randomly clicks an element from a list. I am trying to also get the text from that same function for my case, i am trying to do;
context('Select feature of site', () => {
it('Randomly select element of elements section', () => {
cy.visit('website url')
cy.wait(600)
cy.get('.element_container')
.children()
.eq(Math.floor(Math.random() * 44))
.click({force: true})
.each(($containerSubNewsListWrapper) => {
cy.get($containerSubNewsListWrapper).click()
// cy.title().should('eq','') i stucked here
})
}) })
I click randomly but I'm stuck on syncing the title,i would need to retrieve a title for an item selected randomly and store it somewhere, verify afterwards. I appreciate any ideas
To save the title in a variable using an alias you can do something like this:
cy.title().as('title1')
Then, When you are inside a different page and want to validate that the title matches the previous title you can do something like:
cy.get("@title1").then((title1) => {
cy.title().then((title2) => {. //get the title of the current page
expect(title1).to.equal(title2)
})
})