Search code examples
seleniumwebautomationcypressassertion

Cypress scrollIntoView() giving error while assertion


while I using should('be.visible') assertion then if the element is not on the screen assertion is giving an error. So that's why I using scrollIntoView() but this time element comes on screen so scrollIntoView() cannot work as respected an giving error too.

I don't want to use 'exist' i need to use 'be.visible' assertion.

Last error thrown code, I leaving here as an example.

Then('click work order from the work order list {int}', (i) => {
    cy.xpath(`//div[contains(@class, 'workorder')] //div[contains(text(), '` + getWorkOrderWithNoAttachment(i) + ` /` + `')]`)
        .scrollIntoView()
        .should('be.visible', { setTimeout: 10000 })
        .click()
})

Solution

  • Cypress docs state that

    It is unsafe to chain further commands that rely on the subject after .scrollIntoView().

    Considering this I assume the following test should fix your current error:

    Then('click work order from the work order list {int}', (i) => {
        cy.xpath(`//div[contains(@class, 'workorder')] //div[contains(text(), '` + getWorkOrderWithNoAttachment(i) + ` /` + `')]`)
            .as('workOrder')
        cy.get('@workOrder').scrollIntoView();
        cy.get('@workOrder').should('be.visible', { setTimeout: 10000 })
        cy.get('@workOrder').click()
    })
    

    Furthermore docs state that click() already performs

    • Scroll the element into view.
    • Ensure the element is not hidden.

    So you can safely use the following test to achieve the same result:

    Then('click work order from the work order list {int}', (i) => {
        cy.xpath(`//div[contains(@class, 'workorder')] //div[contains(text(), '` + getWorkOrderWithNoAttachment(i) + ` /` + `')]`)
        .click()
    })