RubyDoc.info:

Class: Capybara::Selector

– Documentation for teamcapybara/capybara (master)

– RubyDoc.info (original) (raw)

All Selectors below support the listed selector specific filters in addition to the following system-wide filters

Built-in Selectors

  page.html # => '<input>'

  page.find :xpath, './/input'
  page.html # => '<input>'

  page.find :css, 'input'
  page.html # => '<input id="field">'

  page.find :id, 'field'
  page.html # => '<label for="article_title">Title</label>
            #     <input id="article_title" name="article[title]" value="Hello world">'

  page.find :field, 'article_title'
  page.find :field, 'article[title]'
  page.find :field, 'Title'
  page.find :field, 'Title', type: 'text', with: 'Hello world'
  page.html # => '<fieldset disabled>
            #       <legend>Fields (disabled)</legend>
            #     </fieldset>'

  page.find :fieldset, 'Fields (disabled)', disabled: true
  page.html # => '<a href="/">Home</a>'

  page.find :link, 'Home', href: '/'

  page.html # => '<a href="/"><img src="/logo.png" alt="The logo"></a>'

  page.find :link, 'The logo', href: '/'
  page.find :link, alt: 'The logo', href: '/'
  page.html # => '<button>Submit</button>'

  page.find :button, 'Submit'

  page.html # => '<button name="article[state]" value="draft">Save as draft</button>'

  page.find :button, 'Save as draft', name: 'article[state]', value: 'draft'
  page.html # => '<a href="/">Home</a>'

  page.find :link_or_button, 'Home'

  page.html # => '<button>Submit</button>'

  page.find :link_or_button, 'Submit'
  page.html # => '<label for="article_body">Body</label>
            #     <textarea id="article_body" name="article[body]"></textarea>'

  page.find :fillable_field, 'article_body'
  page.find :fillable_field, 'article[body]'
  page.find :fillable_field, 'Body'
  page.find :field, 'Body', type: 'textarea'
  page.html # => '<input type="radio" id="article_state_published" name="article[state]" value="published" checked>
            #     <label for="article_state_published">Published</label>
            #     <input type="radio" id="article_state_draft" name="article[state]" value="draft">
            #     <label for="article_state_draft">Draft</label>'

  page.find :radio_button, 'article_state_published'
  page.find :radio_button, 'article[state]', option: 'published'
  page.find :radio_button, 'Published', checked: true
  page.find :radio_button, 'Draft', unchecked: true
  page.html # => '<input type="checkbox" id="registration_terms" name="registration[terms]" value="true">
            #     <label for="registration_terms">I agree to terms and conditions</label>'

  page.find :checkbox, 'registration_terms'
  page.find :checkbox, 'registration[terms]'
  page.find :checkbox, 'I agree to terms and conditions', unchecked: true
  page.html # => '<label for="article_category">Category</label>
            #     <select id="article_category" name="article[category]">
            #       <option value="General" checked></option>
            #       <option value="Other"></option>
            #     </select>'

  page.find :select, 'article_category'
  page.find :select, 'article[category]'
  page.find :select, 'Category'
  page.find :select, 'Category', selected: 'General'
  page.find :select, with_options: ['General']
  page.find :select, with_options: ['Other']
  page.find :select, options: ['General', 'Other']
  page.find :select, options: ['General'] # => raises Capybara::ElementNotFound
  page.html # => '<option value="General" checked></option>
            #     <option value="Disabled" disabled></option>
            #     <option value="Other"></option>'

  page.find :option, 'General'
  page.find :option, 'General', selected: true
  page.find :option, 'Disabled', disabled: true
  page.find :option, 'Other', selected: false
  page.html # => '<label for="ice_cream_flavor">Flavor</label>
            #     <input list="ice_cream_flavors" id="ice_cream_flavor" name="ice_cream[flavor]">
            #     <datalist id="ice_cream_flavors">
            #       <option value="Chocolate"></option>
            #       <option value="Strawberry"></option>
            #       <option value="Vanilla"></option>
            #     </datalist>'

  page.find :datalist_input, 'ice_cream_flavor'
  page.find :datalist_input, 'ice_cream[flavor]'
  page.find :datalist_input, 'Flavor'
  page.find :datalist_input, with_options: ['Chocolate', 'Strawberry']
  page.find :datalist_input, options: ['Chocolate', 'Strawberry', 'Vanilla']
  page.find :datalist_input, options: ['Chocolate'] # => raises Capybara::ElementNotFound
  page.html # => '<datalist>
            #       <option value="Chocolate"></option>
            #       <option value="Strawberry"></option>
            #       <option value="Vanilla"></option>
            #       <option value="Forbidden" disabled></option>
            #     </datalist>'

  page.find :datalist_option, 'Chocolate'
  page.find :datalist_option, 'Strawberry'
  page.find :datalist_option, 'Vanilla'
  page.find :datalist_option, 'Forbidden', disabled: true
  page.html # => '<label for="article_banner_image">Banner Image</label>
            #     <input type="file" id="article_banner_image" name="article[banner_image]">'

  page.find :file_field, 'article_banner_image'
  page.find :file_field, 'article[banner_image]'
  page.find :file_field, 'Banner Image'
  page.find :file_field, 'Banner Image', name: 'article[banner_image]'
  page.find :field, 'Banner Image', type: 'file'
  page.html # => '<label for="article_title">Title</label>
            #     <input id="article_title" name="article[title]">'

  page.find :label, 'Title'
  page.find :label, 'Title', for: 'article_title'
  page.find :label, 'Title', for: page.find('article[title]')

:table - Find table elements

  page.html # => '<table>
            #       <caption>A table</caption>
            #       <tr>
            #         <th>A</th>
            #         <th>B</th>
            #       </tr>
            #       <tr>
            #         <td>1</td>
            #         <td>2</td>
            #       </tr>
            #       <tr>
            #         <td>3</td>
            #         <td>4</td>
            #       </tr>
            #     </table>'

  page.find :table, 'A table'
  page.find :table, with_rows: [
    { 'A' => '1', 'B' => '2' },
    { 'A' => '3', 'B' => '4' },
  ]
  page.find :table, with_rows: [
    ['1', '2'],
    ['3', '4'],
  ]
  page.find :table, rows: [
    { 'A' => '1', 'B' => '2' },
    { 'A' => '3', 'B' => '4' },
  ]
  page.find :table, rows: [
    ['1', '2'],
    ['3', '4'],
  ]
  page.find :table, rows: [ ['1', '2'] ] # => raises Capybara::ElementNotFound
  page.html # => '<table>
            #       <tr>
            #         <th>A</th>
            #         <th>B</th>
            #       </tr>
            #       <tr>
            #         <td>1</td>
            #         <td>2</td>
            #       </tr>
            #       <tr>
            #         <td>3</td>
            #         <td>4</td>
            #       </tr>
            #     </table>'

  page.find :table_row, 'A' => '1', 'B' => '2'
  page.find :table_row, 'A' => '3', 'B' => '4'
  page.html # => '<iframe id="embed_frame" name="embed" src="https://example.com/embed"></iframe>'

  page.find :frame, 'embed_frame'
  page.find :frame, 'embed'
  page.find :frame, name: 'embed'
  page.html # => '<button type="button" role="menuitemcheckbox" aria-checked="true">Check me</button>

  page.find :element, 'button'
  page.find :element, type: 'button', text: 'Check me'
  page.find :element, role: 'menuitemcheckbox'
  page.find :element, role: /checkbox/, 'aria-checked': 'true'