Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in cypress if element is visible on screen?

I have a web page which is very long and there is a footer on the bottom. To see the footer I have to scroll down. I want to write a cypress test to make sure that the footer is not visible when a user is on the top of the page. To do that I write such code:

        cy
        .get('#footer')
        .should('not.be.visible')

And it fails although it is in fact not on the screen, but below the visible part of the page.

like image 908
oksana_mys Avatar asked Mar 12 '20 17:03

oksana_mys


People also ask

How do you know if an element is visible or not in Cypress?

We can check if these elements exist on the webpage in the following way: cy. get('body') .

How do you find the element in Cypress?

Get Element By Containing Text in Cypress You can simply use the contains() function along with the tag name and should be able to get the element. Note: The above example we are using contains() for <a> element, but you can use it for any element if you want to find the attribute or perform an action using text.


1 Answers

The assertion be.visible is designed to check if the user is able to interact with an element even if she has to scroll down to it.

But you can add and use a custom assertion isInViewport, have a look at https://github.com/cypress-io/cypress/issues/877#issuecomment-490504922.

like image 81
geoM Avatar answered Oct 05 '22 05:10

geoM