Cypress - if then functions












0















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>









share|improve this question

























  • Cypress docs - Conditional Testing 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 9:49
















0















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>









share|improve this question

























  • Cypress docs - Conditional Testing 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 9:49














0












0








0








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>









share|improve this question
















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>






if-statement testing automation cypress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 8:23







Dominik Skála

















asked Nov 23 '18 at 13:54









Dominik SkálaDominik Skála

227




227













  • Cypress docs - Conditional Testing 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 9:49



















  • Cypress docs - Conditional Testing 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 9:49

















Cypress docs - Conditional Testing contains a huge number of examples.

– Richard Matsen
Nov 23 '18 at 18:56





Cypress docs - Conditional Testing 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





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 9:49





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 9:49












2 Answers
2






active

oldest

votes


















0














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
}





share|improve this answer
























  • 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.






share|improve this answer








New contributor




Davidz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448012%2fcypress-if-then-functions%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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
    }





    share|improve this answer
























    • 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














    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
    }





    share|improve this answer
























    • 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












    0








    0







    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
    }





    share|improve this answer













    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
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 17:00









    BrendanBrendan

    889616




    889616













    • 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





















    • 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



















    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





    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





    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





    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







    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.






    share|improve this answer








    New contributor




    Davidz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      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.






      share|improve this answer








      New contributor




      Davidz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        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.






        share|improve this answer








        New contributor




        Davidz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        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.







        share|improve this answer








        New contributor




        Davidz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Davidz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Feb 1 at 17:17









        DavidzDavidz

        363




        363




        New contributor




        Davidz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Davidz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Davidz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448012%2fcypress-if-then-functions%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'