Logging and asserting the number of previously-unknown DOM elements












1














I'ts my first tme using Cypress and I almost finalized my first test. But to do so I need to assert against a unknown number. Let me explain:



When the test starts, a random number of elements is generated and I shouldn't have control on such a number (is a requirement). So, I'm trying to get such number in this way:



var previousElems = cy.get('.list-group-item').its('length'); 


I'm not really sure if I'm getting the right data, since I can not log it (the "cypress console" shows me "[Object]" when I print it). But let's say such line returns (5) to exemplify.



During the test, I simulate a user creating extra elements (2) and removing an (1) element. Let's say the user just creates one single extra element.



So, at the end os the test, I need to check if the number of eements with the same class are equals to (5+2-1) = (6) elements. I'm doing it in this way:



cy.get('.list-group-item').its('length').should('eq', (previousTasks + 1));


But I get the following message:




CypressError: Timed out retrying: expected 10 to equal '[object Object]1'




So, how can I log and assert this?
Thanks in advance,



PD: I also tryed:



var previousTasks = (Cypress.$("ul").children)? Cypress.$("ul").children.length : 0;


But it always returns a fixed number (2), even if I put a wait before to make sure all the items are fully loaded.



I also tryed the same with childNodes but it always return 0.










share|improve this question





























    1














    I'ts my first tme using Cypress and I almost finalized my first test. But to do so I need to assert against a unknown number. Let me explain:



    When the test starts, a random number of elements is generated and I shouldn't have control on such a number (is a requirement). So, I'm trying to get such number in this way:



    var previousElems = cy.get('.list-group-item').its('length'); 


    I'm not really sure if I'm getting the right data, since I can not log it (the "cypress console" shows me "[Object]" when I print it). But let's say such line returns (5) to exemplify.



    During the test, I simulate a user creating extra elements (2) and removing an (1) element. Let's say the user just creates one single extra element.



    So, at the end os the test, I need to check if the number of eements with the same class are equals to (5+2-1) = (6) elements. I'm doing it in this way:



    cy.get('.list-group-item').its('length').should('eq', (previousTasks + 1));


    But I get the following message:




    CypressError: Timed out retrying: expected 10 to equal '[object Object]1'




    So, how can I log and assert this?
    Thanks in advance,



    PD: I also tryed:



    var previousTasks = (Cypress.$("ul").children)? Cypress.$("ul").children.length : 0;


    But it always returns a fixed number (2), even if I put a wait before to make sure all the items are fully loaded.



    I also tryed the same with childNodes but it always return 0.










    share|improve this question



























      1












      1








      1







      I'ts my first tme using Cypress and I almost finalized my first test. But to do so I need to assert against a unknown number. Let me explain:



      When the test starts, a random number of elements is generated and I shouldn't have control on such a number (is a requirement). So, I'm trying to get such number in this way:



      var previousElems = cy.get('.list-group-item').its('length'); 


      I'm not really sure if I'm getting the right data, since I can not log it (the "cypress console" shows me "[Object]" when I print it). But let's say such line returns (5) to exemplify.



      During the test, I simulate a user creating extra elements (2) and removing an (1) element. Let's say the user just creates one single extra element.



      So, at the end os the test, I need to check if the number of eements with the same class are equals to (5+2-1) = (6) elements. I'm doing it in this way:



      cy.get('.list-group-item').its('length').should('eq', (previousTasks + 1));


      But I get the following message:




      CypressError: Timed out retrying: expected 10 to equal '[object Object]1'




      So, how can I log and assert this?
      Thanks in advance,



      PD: I also tryed:



      var previousTasks = (Cypress.$("ul").children)? Cypress.$("ul").children.length : 0;


      But it always returns a fixed number (2), even if I put a wait before to make sure all the items are fully loaded.



      I also tryed the same with childNodes but it always return 0.










      share|improve this question















      I'ts my first tme using Cypress and I almost finalized my first test. But to do so I need to assert against a unknown number. Let me explain:



      When the test starts, a random number of elements is generated and I shouldn't have control on such a number (is a requirement). So, I'm trying to get such number in this way:



      var previousElems = cy.get('.list-group-item').its('length'); 


      I'm not really sure if I'm getting the right data, since I can not log it (the "cypress console" shows me "[Object]" when I print it). But let's say such line returns (5) to exemplify.



      During the test, I simulate a user creating extra elements (2) and removing an (1) element. Let's say the user just creates one single extra element.



      So, at the end os the test, I need to check if the number of eements with the same class are equals to (5+2-1) = (6) elements. I'm doing it in this way:



      cy.get('.list-group-item').its('length').should('eq', (previousTasks + 1));


      But I get the following message:




      CypressError: Timed out retrying: expected 10 to equal '[object Object]1'




      So, how can I log and assert this?
      Thanks in advance,



      PD: I also tryed:



      var previousTasks = (Cypress.$("ul").children)? Cypress.$("ul").children.length : 0;


      But it always returns a fixed number (2), even if I put a wait before to make sure all the items are fully loaded.



      I also tryed the same with childNodes but it always return 0.







      cypress






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 18:37

























      asked Nov 12 '18 at 18:10









      gal007

      1,70442340




      1,70442340
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Your problem stems from the fact that Cypress test code is run all at once before the test starts. Commands are queued to be run later, and so storing variables as in your example will not work. This is why you keep getting objects instead of numbers; the object you're getting is called a chainer, and is used to allow you to chain commands off other commands, like so: cy.get('#someSelector').should('...');



          Cypress has a way to get around this though; if you need to operate on some data directly, you can provide a lambda function using .then() that will be run in order with the rest of your commands. Here's a basic example that should work in your scenario:





          cy.get('.list-group-item').its('length').then(previousCount => {
          // Add two elements and remove one...
          cy.get('.list-group-item').its('.length').should('eq', previousCount + 1);
          });


          If you haven't already, I strongly suggest reading the fantastic introduction to Cypress in the docs. This page on variables and aliases should also be useful in this case.






          share|improve this answer





















            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%2f53267800%2flogging-and-asserting-the-number-of-previously-unknown-dom-elements%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Your problem stems from the fact that Cypress test code is run all at once before the test starts. Commands are queued to be run later, and so storing variables as in your example will not work. This is why you keep getting objects instead of numbers; the object you're getting is called a chainer, and is used to allow you to chain commands off other commands, like so: cy.get('#someSelector').should('...');



            Cypress has a way to get around this though; if you need to operate on some data directly, you can provide a lambda function using .then() that will be run in order with the rest of your commands. Here's a basic example that should work in your scenario:





            cy.get('.list-group-item').its('length').then(previousCount => {
            // Add two elements and remove one...
            cy.get('.list-group-item').its('.length').should('eq', previousCount + 1);
            });


            If you haven't already, I strongly suggest reading the fantastic introduction to Cypress in the docs. This page on variables and aliases should also be useful in this case.






            share|improve this answer


























              0














              Your problem stems from the fact that Cypress test code is run all at once before the test starts. Commands are queued to be run later, and so storing variables as in your example will not work. This is why you keep getting objects instead of numbers; the object you're getting is called a chainer, and is used to allow you to chain commands off other commands, like so: cy.get('#someSelector').should('...');



              Cypress has a way to get around this though; if you need to operate on some data directly, you can provide a lambda function using .then() that will be run in order with the rest of your commands. Here's a basic example that should work in your scenario:





              cy.get('.list-group-item').its('length').then(previousCount => {
              // Add two elements and remove one...
              cy.get('.list-group-item').its('.length').should('eq', previousCount + 1);
              });


              If you haven't already, I strongly suggest reading the fantastic introduction to Cypress in the docs. This page on variables and aliases should also be useful in this case.






              share|improve this answer
























                0












                0








                0






                Your problem stems from the fact that Cypress test code is run all at once before the test starts. Commands are queued to be run later, and so storing variables as in your example will not work. This is why you keep getting objects instead of numbers; the object you're getting is called a chainer, and is used to allow you to chain commands off other commands, like so: cy.get('#someSelector').should('...');



                Cypress has a way to get around this though; if you need to operate on some data directly, you can provide a lambda function using .then() that will be run in order with the rest of your commands. Here's a basic example that should work in your scenario:





                cy.get('.list-group-item').its('length').then(previousCount => {
                // Add two elements and remove one...
                cy.get('.list-group-item').its('.length').should('eq', previousCount + 1);
                });


                If you haven't already, I strongly suggest reading the fantastic introduction to Cypress in the docs. This page on variables and aliases should also be useful in this case.






                share|improve this answer












                Your problem stems from the fact that Cypress test code is run all at once before the test starts. Commands are queued to be run later, and so storing variables as in your example will not work. This is why you keep getting objects instead of numbers; the object you're getting is called a chainer, and is used to allow you to chain commands off other commands, like so: cy.get('#someSelector').should('...');



                Cypress has a way to get around this though; if you need to operate on some data directly, you can provide a lambda function using .then() that will be run in order with the rest of your commands. Here's a basic example that should work in your scenario:





                cy.get('.list-group-item').its('length').then(previousCount => {
                // Add two elements and remove one...
                cy.get('.list-group-item').its('.length').should('eq', previousCount + 1);
                });


                If you haven't already, I strongly suggest reading the fantastic introduction to Cypress in the docs. This page on variables and aliases should also be useful in this case.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 16:00









                Joshua Wade

                825316




                825316






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53267800%2flogging-and-asserting-the-number-of-previously-unknown-dom-elements%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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues