Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set CSS properties like `display` using Cypress commands?

I would like to hide an element by setting the display property.

I've tried using invoke to set the attribute value, but it may only work for HTML attributes?

cy.get('#hubspot-messages-iframe-container > iframe')
        .invoke('attr', 'display', 'none!important')
        .should('have.attr', 'display', 'none!important')

This seems to be setting the attribute values, but the element is still visible, which leads me to believe it's probably not recognizing my attribute or i'm using the wrong command.

Tried using this as suggested:

enter image description here

The .css() function seems to not be setting the value the way it should:

enter image description here

Here's me validating my own sanity that the selector is correct and the display is none lol: enter image description here

like image 756
jameseg Avatar asked Mar 29 '19 17:03

jameseg


People also ask

How do you make elements visible in Cypress?

For handling the hidden elements, Cypress takes the help of the jQuery method show. It has to be invoked with the help of the Cypress command (invoke['show']).


2 Answers

This seems to work folks. I believe initial !important is a reference of some sort and not an actual value and maybe that's why .css() wouldn't work.

Cypress.Commands.add('hideHubSpot', () => {
    cy.get('#hubspot-messages-iframe-container')
        .invoke('attr', 'style', 'display: none')
        .should('have.attr', 'style', 'display: none')
like image 137
jameseg Avatar answered Sep 27 '22 22:09

jameseg


You can invoke the css function from jQuery using invoke to change the CSS. Note that your !important won't work, but you don't need it, since setting CSS via Javascript will override any other styles on the item.

Then you can just use the have.css assertion from the Assertion List to check, if you need to.

Here's what that would look like:

cy.get('#hubspot-messages-iframe-container > iframe')
  // use the .css function from jquery, since Cypress yields jquery elements
  .invoke('css', 'display', 'none')
  .should('have.css', 'display', 'none')
like image 34
Zach Bloomquist Avatar answered Sep 27 '22 21:09

Zach Bloomquist