9

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>
4
  • Cypress docs - Conditional Testing contains a huge number of examples. Commented Nov 23, 2018 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? Commented Nov 25, 2018 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. Commented Nov 26, 2018 at 9:49
  • Does this answer your question? How to check if element exists using Cypress.io Commented Oct 9, 2020 at 12:57

2 Answers 2

15

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

That doesn't work. I'm clicking on checkbox if element exists and this code allways found nothing.
Post your code, HTML, and cypress command log and people will be able to help you better
I post it in description.
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()
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.