10

I have question about Cypress.

I have an element on page which doesn't appear allways. There is no logic when it shows and when not.

Is in Cypress some IF/THEN function or something how do you check if the element is displayed (so fill it up) and when you don't see it than skip that step?

My code:

if (Cypress.$('[data-bind="validationElement: interestInsurable"]').length > 0) {
            cy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()        
        }
else {cy.get('#car-info-serie')}

This is how it looks like in playground: Picture

And there is HTML of that checkbox:

<label class="z-radio z-radio-inline primary" for="p4-conditional-csob-interest-insurable-1" data-bind="popover: { content: VehicleInsuranceTooltips.conditionalDataCsobInterestInsurable1Tooltip }" data-original-title="" title="">
    <input id="p4-conditional-csob-interest-insurable-1" name="p4-conditional-csob-interest-insurable" type="radio" class="custom-radio" data-toggle="radio" data-bind="checkedValue: 1, checked: interestInsurable" value="1">
<span class="icons">
<span class="icon-unchecked"></span>
<span class="icon-checked"></span>
</span>
Patří vozidlo zájemci o pojištění?
</label>
Dominik Skála
  • 793
  • 4
  • 12
  • 30
  • [Cypress docs - Conditional Testing](https://docs.cypress.io/guides/core-concepts/conditional-testing.html) contains a huge number of examples. – Richard Matsen Nov 23 '18 at 18:56
  • How is it possible that there is no logic as to when it shows or not? What makes it show? I guess I am confused as to how an element can just decide to show up in your dom by itself. Can you explain more? – Maccurt Nov 25 '18 at 19:11
  • There are products that you can choose. They're sorted by price and price is changing with every field before. Only two products have that element. – Dominik Skála Nov 26 '18 at 09:49
  • Does this answer your question? [How to check if element exists using Cypress.io](https://stackoverflow.com/questions/56145926/how-to-check-if-element-exists-using-cypress-io) – Michael Freidgeim Oct 09 '20 at 12:57

2 Answers2

16

There is no built in way to do this in cypress. I am using this in my tests:

if (Cypress.$("#yourElement").length > 0) {
  // element exists, do something
} else {
  // element does not exist, do something else
}
Brendan
  • 4,327
  • 1
  • 23
  • 33
  • That doesn't work. I'm clicking on checkbox if element exists and this code allways found nothing. – Dominik Skála Nov 23 '18 at 19:13
  • Post your code, HTML, and cypress command log and people will be able to help you better – Brendan Nov 23 '18 at 19:19
  • I post it in description. – Dominik Skála Nov 24 '18 at 10:02
  • You have the wrong selector, you're telling it to click the label, not the input. Try this: `cy.get('input#p4-conditional-csob-interest-insurable-1').click()` instead of `cy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()` – Brendan Nov 25 '18 at 17:12
0

You have to click on the input element instead of the label:

cy.get('#p4-conditional-csob-interest-insurable-1').click();

Anyways have a look at the Cypress Docs as conditional testing is strongly discouraged.

DavidZ
  • 519
  • 1
  • 5
  • 9