Geb wait for element to disappear












1















When I perform an action on my page, a spinner is displayed which disappears once the action is completed. I want to wait for the spinner to disappear so as to execute the assert statements.



I read the documentation which tells me how to wait for an element to appear but does not give info on how to wait for the element to disappear
I don't know how to implement this in Cucumber, Geb, Groovy project.










share|improve this question





























    1















    When I perform an action on my page, a spinner is displayed which disappears once the action is completed. I want to wait for the spinner to disappear so as to execute the assert statements.



    I read the documentation which tells me how to wait for an element to appear but does not give info on how to wait for the element to disappear
    I don't know how to implement this in Cucumber, Geb, Groovy project.










    share|improve this question



























      1












      1








      1








      When I perform an action on my page, a spinner is displayed which disappears once the action is completed. I want to wait for the spinner to disappear so as to execute the assert statements.



      I read the documentation which tells me how to wait for an element to appear but does not give info on how to wait for the element to disappear
      I don't know how to implement this in Cucumber, Geb, Groovy project.










      share|improve this question
















      When I perform an action on my page, a spinner is displayed which disappears once the action is completed. I want to wait for the spinner to disappear so as to execute the assert statements.



      I read the documentation which tells me how to wait for an element to appear but does not give info on how to wait for the element to disappear
      I don't know how to implement this in Cucumber, Geb, Groovy project.







      groovy cucumber geb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 3:50









      M.Ricciuti

      3,5502419




      3,5502419










      asked Nov 14 '18 at 20:10









      Varun JainVarun Jain

      63




      63
























          2 Answers
          2






          active

          oldest

          votes


















          2














          I'll edit/explain this in a bit, when i have more time:



          In your page object:



          static content = {
          loadingSpinner(wait:3, required:false) { $("mat-spinner") }
          //this wait:3 is redundant (i think) if we also give the waitFor() a timeout
          //required:false allows our wait !displayed to pass even if the element isnt there
          }

          def "Handle the loader"() {
          try {
          waitFor(2) { loadingSpinner.isDisplayed() }
          } catch (WaitTimeoutException wte) {
          //do nothing, if spinner doesnt load then thats ok
          //most likely the spinner has come and gone before we finished page load
          //if this is not the case, up our waitFor timeout
          return true;
          }
          waitFor(10) { !loadingSpinner.isDisplayed() }
          }





          share|improve this answer

































            0














            As described in the documentation, the waitFor block uses Groovy Truth to know when it waited long enough. When you put a Navigator in it and the element is currently not present, it will wait for it to appear or until the maximum waiting time it elapsed.



            So if want to wait for an element to disappear, you can simply put it in a waitFor like this:



            // go to the page
            waitFor(2) { $(".loadingspinner").displayed }
            waitFor(10) { !$(".loadingspinner").displayed }
            // do your assertions


            In case the loading spinner already disappeared, waitFor will return immediately. In case it never disappears, it will throw a WaitTimeoutException after 10 seconds, which will make your test fail.






            share|improve this answer


























            • does this address a 1second gap between page load and spinner display?

              – Doug Clark
              Jan 2 at 18:51











            • Now it does. You might want to play a bit with the timeouts...

              – Michael
              Jan 2 at 18:55











            • but now what if it never displays? im looking to improve on my solution

              – Doug Clark
              Jan 2 at 18:59













            • In that case the first waitFor will throw a WaitTimeoutException. You are in trouble if it disappeared within the 2 seconds timeout. It might be worth considering finding something that appears.

              – Michael
              Jan 2 at 19:02











            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%2f53308043%2fgeb-wait-for-element-to-disappear%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









            2














            I'll edit/explain this in a bit, when i have more time:



            In your page object:



            static content = {
            loadingSpinner(wait:3, required:false) { $("mat-spinner") }
            //this wait:3 is redundant (i think) if we also give the waitFor() a timeout
            //required:false allows our wait !displayed to pass even if the element isnt there
            }

            def "Handle the loader"() {
            try {
            waitFor(2) { loadingSpinner.isDisplayed() }
            } catch (WaitTimeoutException wte) {
            //do nothing, if spinner doesnt load then thats ok
            //most likely the spinner has come and gone before we finished page load
            //if this is not the case, up our waitFor timeout
            return true;
            }
            waitFor(10) { !loadingSpinner.isDisplayed() }
            }





            share|improve this answer






























              2














              I'll edit/explain this in a bit, when i have more time:



              In your page object:



              static content = {
              loadingSpinner(wait:3, required:false) { $("mat-spinner") }
              //this wait:3 is redundant (i think) if we also give the waitFor() a timeout
              //required:false allows our wait !displayed to pass even if the element isnt there
              }

              def "Handle the loader"() {
              try {
              waitFor(2) { loadingSpinner.isDisplayed() }
              } catch (WaitTimeoutException wte) {
              //do nothing, if spinner doesnt load then thats ok
              //most likely the spinner has come and gone before we finished page load
              //if this is not the case, up our waitFor timeout
              return true;
              }
              waitFor(10) { !loadingSpinner.isDisplayed() }
              }





              share|improve this answer




























                2












                2








                2







                I'll edit/explain this in a bit, when i have more time:



                In your page object:



                static content = {
                loadingSpinner(wait:3, required:false) { $("mat-spinner") }
                //this wait:3 is redundant (i think) if we also give the waitFor() a timeout
                //required:false allows our wait !displayed to pass even if the element isnt there
                }

                def "Handle the loader"() {
                try {
                waitFor(2) { loadingSpinner.isDisplayed() }
                } catch (WaitTimeoutException wte) {
                //do nothing, if spinner doesnt load then thats ok
                //most likely the spinner has come and gone before we finished page load
                //if this is not the case, up our waitFor timeout
                return true;
                }
                waitFor(10) { !loadingSpinner.isDisplayed() }
                }





                share|improve this answer















                I'll edit/explain this in a bit, when i have more time:



                In your page object:



                static content = {
                loadingSpinner(wait:3, required:false) { $("mat-spinner") }
                //this wait:3 is redundant (i think) if we also give the waitFor() a timeout
                //required:false allows our wait !displayed to pass even if the element isnt there
                }

                def "Handle the loader"() {
                try {
                waitFor(2) { loadingSpinner.isDisplayed() }
                } catch (WaitTimeoutException wte) {
                //do nothing, if spinner doesnt load then thats ok
                //most likely the spinner has come and gone before we finished page load
                //if this is not the case, up our waitFor timeout
                return true;
                }
                waitFor(10) { !loadingSpinner.isDisplayed() }
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 2 at 18:58

























                answered Nov 16 '18 at 16:54









                Doug ClarkDoug Clark

                335212




                335212

























                    0














                    As described in the documentation, the waitFor block uses Groovy Truth to know when it waited long enough. When you put a Navigator in it and the element is currently not present, it will wait for it to appear or until the maximum waiting time it elapsed.



                    So if want to wait for an element to disappear, you can simply put it in a waitFor like this:



                    // go to the page
                    waitFor(2) { $(".loadingspinner").displayed }
                    waitFor(10) { !$(".loadingspinner").displayed }
                    // do your assertions


                    In case the loading spinner already disappeared, waitFor will return immediately. In case it never disappears, it will throw a WaitTimeoutException after 10 seconds, which will make your test fail.






                    share|improve this answer


























                    • does this address a 1second gap between page load and spinner display?

                      – Doug Clark
                      Jan 2 at 18:51











                    • Now it does. You might want to play a bit with the timeouts...

                      – Michael
                      Jan 2 at 18:55











                    • but now what if it never displays? im looking to improve on my solution

                      – Doug Clark
                      Jan 2 at 18:59













                    • In that case the first waitFor will throw a WaitTimeoutException. You are in trouble if it disappeared within the 2 seconds timeout. It might be worth considering finding something that appears.

                      – Michael
                      Jan 2 at 19:02
















                    0














                    As described in the documentation, the waitFor block uses Groovy Truth to know when it waited long enough. When you put a Navigator in it and the element is currently not present, it will wait for it to appear or until the maximum waiting time it elapsed.



                    So if want to wait for an element to disappear, you can simply put it in a waitFor like this:



                    // go to the page
                    waitFor(2) { $(".loadingspinner").displayed }
                    waitFor(10) { !$(".loadingspinner").displayed }
                    // do your assertions


                    In case the loading spinner already disappeared, waitFor will return immediately. In case it never disappears, it will throw a WaitTimeoutException after 10 seconds, which will make your test fail.






                    share|improve this answer


























                    • does this address a 1second gap between page load and spinner display?

                      – Doug Clark
                      Jan 2 at 18:51











                    • Now it does. You might want to play a bit with the timeouts...

                      – Michael
                      Jan 2 at 18:55











                    • but now what if it never displays? im looking to improve on my solution

                      – Doug Clark
                      Jan 2 at 18:59













                    • In that case the first waitFor will throw a WaitTimeoutException. You are in trouble if it disappeared within the 2 seconds timeout. It might be worth considering finding something that appears.

                      – Michael
                      Jan 2 at 19:02














                    0












                    0








                    0







                    As described in the documentation, the waitFor block uses Groovy Truth to know when it waited long enough. When you put a Navigator in it and the element is currently not present, it will wait for it to appear or until the maximum waiting time it elapsed.



                    So if want to wait for an element to disappear, you can simply put it in a waitFor like this:



                    // go to the page
                    waitFor(2) { $(".loadingspinner").displayed }
                    waitFor(10) { !$(".loadingspinner").displayed }
                    // do your assertions


                    In case the loading spinner already disappeared, waitFor will return immediately. In case it never disappears, it will throw a WaitTimeoutException after 10 seconds, which will make your test fail.






                    share|improve this answer















                    As described in the documentation, the waitFor block uses Groovy Truth to know when it waited long enough. When you put a Navigator in it and the element is currently not present, it will wait for it to appear or until the maximum waiting time it elapsed.



                    So if want to wait for an element to disappear, you can simply put it in a waitFor like this:



                    // go to the page
                    waitFor(2) { $(".loadingspinner").displayed }
                    waitFor(10) { !$(".loadingspinner").displayed }
                    // do your assertions


                    In case the loading spinner already disappeared, waitFor will return immediately. In case it never disappears, it will throw a WaitTimeoutException after 10 seconds, which will make your test fail.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 2 at 18:54

























                    answered Jan 1 at 10:39









                    MichaelMichael

                    1,2261912




                    1,2261912













                    • does this address a 1second gap between page load and spinner display?

                      – Doug Clark
                      Jan 2 at 18:51











                    • Now it does. You might want to play a bit with the timeouts...

                      – Michael
                      Jan 2 at 18:55











                    • but now what if it never displays? im looking to improve on my solution

                      – Doug Clark
                      Jan 2 at 18:59













                    • In that case the first waitFor will throw a WaitTimeoutException. You are in trouble if it disappeared within the 2 seconds timeout. It might be worth considering finding something that appears.

                      – Michael
                      Jan 2 at 19:02



















                    • does this address a 1second gap between page load and spinner display?

                      – Doug Clark
                      Jan 2 at 18:51











                    • Now it does. You might want to play a bit with the timeouts...

                      – Michael
                      Jan 2 at 18:55











                    • but now what if it never displays? im looking to improve on my solution

                      – Doug Clark
                      Jan 2 at 18:59













                    • In that case the first waitFor will throw a WaitTimeoutException. You are in trouble if it disappeared within the 2 seconds timeout. It might be worth considering finding something that appears.

                      – Michael
                      Jan 2 at 19:02

















                    does this address a 1second gap between page load and spinner display?

                    – Doug Clark
                    Jan 2 at 18:51





                    does this address a 1second gap between page load and spinner display?

                    – Doug Clark
                    Jan 2 at 18:51













                    Now it does. You might want to play a bit with the timeouts...

                    – Michael
                    Jan 2 at 18:55





                    Now it does. You might want to play a bit with the timeouts...

                    – Michael
                    Jan 2 at 18:55













                    but now what if it never displays? im looking to improve on my solution

                    – Doug Clark
                    Jan 2 at 18:59







                    but now what if it never displays? im looking to improve on my solution

                    – Doug Clark
                    Jan 2 at 18:59















                    In that case the first waitFor will throw a WaitTimeoutException. You are in trouble if it disappeared within the 2 seconds timeout. It might be worth considering finding something that appears.

                    – Michael
                    Jan 2 at 19:02





                    In that case the first waitFor will throw a WaitTimeoutException. You are in trouble if it disappeared within the 2 seconds timeout. It might be worth considering finding something that appears.

                    – Michael
                    Jan 2 at 19:02


















                    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%2f53308043%2fgeb-wait-for-element-to-disappear%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

                    Bressuire

                    Vorschmack

                    Quarantine