How to test if a page has a date picker?












3















I'm spec-ing my form that will have some date pickers.



expect(page).to have_select('Start date') and expect(page).to have_select('deal[start_date]') both return no matches. The latter makes sense given that the html for date pickers is a little funky (name="deal[start_date(2i)])



expect(page).to have_date_select('deal[start_date]') says that Capybara doesn't recognize has_date_select.



Is there a way to do this?










share|improve this question



























    3















    I'm spec-ing my form that will have some date pickers.



    expect(page).to have_select('Start date') and expect(page).to have_select('deal[start_date]') both return no matches. The latter makes sense given that the html for date pickers is a little funky (name="deal[start_date(2i)])



    expect(page).to have_date_select('deal[start_date]') says that Capybara doesn't recognize has_date_select.



    Is there a way to do this?










    share|improve this question

























      3












      3








      3








      I'm spec-ing my form that will have some date pickers.



      expect(page).to have_select('Start date') and expect(page).to have_select('deal[start_date]') both return no matches. The latter makes sense given that the html for date pickers is a little funky (name="deal[start_date(2i)])



      expect(page).to have_date_select('deal[start_date]') says that Capybara doesn't recognize has_date_select.



      Is there a way to do this?










      share|improve this question














      I'm spec-ing my form that will have some date pickers.



      expect(page).to have_select('Start date') and expect(page).to have_select('deal[start_date]') both return no matches. The latter makes sense given that the html for date pickers is a little funky (name="deal[start_date(2i)])



      expect(page).to have_date_select('deal[start_date]') says that Capybara doesn't recognize has_date_select.



      Is there a way to do this?







      ruby-on-rails capybara






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 20:13









      Jonathan TuzmanJonathan Tuzman

      358415




      358415
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Capybaras has_select? predicate and have_select matcher look for HTML <select> elements. Depending on what you mean by a date picker you'll have to use a matcher that would match the correct type of element, or have_selector if there isn't a specific matcher. If what you are actually checking for is an <input type="date"> element with an associated label containing the text 'Start date' then you can use the have_field matcher:



          expect(page).to have_field 'Start date', type: 'date'


          If you're using the Rails date_select view helper it produces HTML like



          <div class="field">
          <label for="post_start_date">Start date</label>
          <select id="post_start_date_1i" name="post[start_date(1i)]">
          <option value="2014">2014</option>
          <option value="2015">2015</option>
          ...
          </select>
          <select id="post_start_date_2i" name="post[start_date(2i)]">
          <option value="1" selected="selected">January</option>
          <option value="2">February</option>
          ...
          </select>
          <select id="post_start_date_3i" name="post[start_date(3i)]">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          ...
          </select>
          </div>


          You can see from looking at that HTML that the <label> element isn't actually associated with any of the form fields (the for attribute doesn't match an id) which means you won't be able to match the <select> elements using the label text. Instead you'd need to use either the id or name and match them individually



          expect(page).to have_select('post_start_date_1i')
          expect(page).to have_select('post[start_date(2i)]')
          ...


          or you could use the id filter with a regex and a count to check that there are 3 elements matching like



          expect(page).to have_select(id: /^post_start_date_[123]i$/, count: 3)


          Note, that's not technically as 'correct' as doing them individually since you could actually have 3 elements with id of post_start_date_1i and it would still pass, but in that case your HTML would also be invalid.



          If that's not the type of HTML element you're checking for then you'll need to provide the HTML to get an exact answer.






          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%2f54260830%2fhow-to-test-if-a-page-has-a-date-picker%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














            Capybaras has_select? predicate and have_select matcher look for HTML <select> elements. Depending on what you mean by a date picker you'll have to use a matcher that would match the correct type of element, or have_selector if there isn't a specific matcher. If what you are actually checking for is an <input type="date"> element with an associated label containing the text 'Start date' then you can use the have_field matcher:



            expect(page).to have_field 'Start date', type: 'date'


            If you're using the Rails date_select view helper it produces HTML like



            <div class="field">
            <label for="post_start_date">Start date</label>
            <select id="post_start_date_1i" name="post[start_date(1i)]">
            <option value="2014">2014</option>
            <option value="2015">2015</option>
            ...
            </select>
            <select id="post_start_date_2i" name="post[start_date(2i)]">
            <option value="1" selected="selected">January</option>
            <option value="2">February</option>
            ...
            </select>
            <select id="post_start_date_3i" name="post[start_date(3i)]">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            ...
            </select>
            </div>


            You can see from looking at that HTML that the <label> element isn't actually associated with any of the form fields (the for attribute doesn't match an id) which means you won't be able to match the <select> elements using the label text. Instead you'd need to use either the id or name and match them individually



            expect(page).to have_select('post_start_date_1i')
            expect(page).to have_select('post[start_date(2i)]')
            ...


            or you could use the id filter with a regex and a count to check that there are 3 elements matching like



            expect(page).to have_select(id: /^post_start_date_[123]i$/, count: 3)


            Note, that's not technically as 'correct' as doing them individually since you could actually have 3 elements with id of post_start_date_1i and it would still pass, but in that case your HTML would also be invalid.



            If that's not the type of HTML element you're checking for then you'll need to provide the HTML to get an exact answer.






            share|improve this answer






























              0














              Capybaras has_select? predicate and have_select matcher look for HTML <select> elements. Depending on what you mean by a date picker you'll have to use a matcher that would match the correct type of element, or have_selector if there isn't a specific matcher. If what you are actually checking for is an <input type="date"> element with an associated label containing the text 'Start date' then you can use the have_field matcher:



              expect(page).to have_field 'Start date', type: 'date'


              If you're using the Rails date_select view helper it produces HTML like



              <div class="field">
              <label for="post_start_date">Start date</label>
              <select id="post_start_date_1i" name="post[start_date(1i)]">
              <option value="2014">2014</option>
              <option value="2015">2015</option>
              ...
              </select>
              <select id="post_start_date_2i" name="post[start_date(2i)]">
              <option value="1" selected="selected">January</option>
              <option value="2">February</option>
              ...
              </select>
              <select id="post_start_date_3i" name="post[start_date(3i)]">
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              ...
              </select>
              </div>


              You can see from looking at that HTML that the <label> element isn't actually associated with any of the form fields (the for attribute doesn't match an id) which means you won't be able to match the <select> elements using the label text. Instead you'd need to use either the id or name and match them individually



              expect(page).to have_select('post_start_date_1i')
              expect(page).to have_select('post[start_date(2i)]')
              ...


              or you could use the id filter with a regex and a count to check that there are 3 elements matching like



              expect(page).to have_select(id: /^post_start_date_[123]i$/, count: 3)


              Note, that's not technically as 'correct' as doing them individually since you could actually have 3 elements with id of post_start_date_1i and it would still pass, but in that case your HTML would also be invalid.



              If that's not the type of HTML element you're checking for then you'll need to provide the HTML to get an exact answer.






              share|improve this answer




























                0












                0








                0







                Capybaras has_select? predicate and have_select matcher look for HTML <select> elements. Depending on what you mean by a date picker you'll have to use a matcher that would match the correct type of element, or have_selector if there isn't a specific matcher. If what you are actually checking for is an <input type="date"> element with an associated label containing the text 'Start date' then you can use the have_field matcher:



                expect(page).to have_field 'Start date', type: 'date'


                If you're using the Rails date_select view helper it produces HTML like



                <div class="field">
                <label for="post_start_date">Start date</label>
                <select id="post_start_date_1i" name="post[start_date(1i)]">
                <option value="2014">2014</option>
                <option value="2015">2015</option>
                ...
                </select>
                <select id="post_start_date_2i" name="post[start_date(2i)]">
                <option value="1" selected="selected">January</option>
                <option value="2">February</option>
                ...
                </select>
                <select id="post_start_date_3i" name="post[start_date(3i)]">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                ...
                </select>
                </div>


                You can see from looking at that HTML that the <label> element isn't actually associated with any of the form fields (the for attribute doesn't match an id) which means you won't be able to match the <select> elements using the label text. Instead you'd need to use either the id or name and match them individually



                expect(page).to have_select('post_start_date_1i')
                expect(page).to have_select('post[start_date(2i)]')
                ...


                or you could use the id filter with a regex and a count to check that there are 3 elements matching like



                expect(page).to have_select(id: /^post_start_date_[123]i$/, count: 3)


                Note, that's not technically as 'correct' as doing them individually since you could actually have 3 elements with id of post_start_date_1i and it would still pass, but in that case your HTML would also be invalid.



                If that's not the type of HTML element you're checking for then you'll need to provide the HTML to get an exact answer.






                share|improve this answer















                Capybaras has_select? predicate and have_select matcher look for HTML <select> elements. Depending on what you mean by a date picker you'll have to use a matcher that would match the correct type of element, or have_selector if there isn't a specific matcher. If what you are actually checking for is an <input type="date"> element with an associated label containing the text 'Start date' then you can use the have_field matcher:



                expect(page).to have_field 'Start date', type: 'date'


                If you're using the Rails date_select view helper it produces HTML like



                <div class="field">
                <label for="post_start_date">Start date</label>
                <select id="post_start_date_1i" name="post[start_date(1i)]">
                <option value="2014">2014</option>
                <option value="2015">2015</option>
                ...
                </select>
                <select id="post_start_date_2i" name="post[start_date(2i)]">
                <option value="1" selected="selected">January</option>
                <option value="2">February</option>
                ...
                </select>
                <select id="post_start_date_3i" name="post[start_date(3i)]">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                ...
                </select>
                </div>


                You can see from looking at that HTML that the <label> element isn't actually associated with any of the form fields (the for attribute doesn't match an id) which means you won't be able to match the <select> elements using the label text. Instead you'd need to use either the id or name and match them individually



                expect(page).to have_select('post_start_date_1i')
                expect(page).to have_select('post[start_date(2i)]')
                ...


                or you could use the id filter with a regex and a count to check that there are 3 elements matching like



                expect(page).to have_select(id: /^post_start_date_[123]i$/, count: 3)


                Note, that's not technically as 'correct' as doing them individually since you could actually have 3 elements with id of post_start_date_1i and it would still pass, but in that case your HTML would also be invalid.



                If that's not the type of HTML element you're checking for then you'll need to provide the HTML to get an exact answer.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 21 at 10:38

























                answered Jan 18 at 21:08









                Thomas WalpoleThomas Walpole

                30.6k32747




                30.6k32747






























                    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%2f54260830%2fhow-to-test-if-a-page-has-a-date-picker%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

                    Liquibase includeAll doesn't find base path

                    How to use setInterval in EJS file?

                    Petrus Granier-Deferre