How to use label method of Capybara Package

Best Capybara code snippet using Capybara.label

actions.rb

Source:actions.rb Github

copy

Full Screen

...55 end56 ##57 #58 # Locate a text field or text area and fill it in with the given text.59 # The field can be found via its name, id, {Capybara.configure test_id} attribute, placeholder, or label text.60 # If no locator is provided this will operate on self or a descendant.61 #62 # # will fill in a descendant fillable field with name, id, or label text matching 'Name'63 # page.fill_in 'Name', with: 'Bob'64 #65 # # will fill in `el` if it's a fillable field66 # el.fill_in with: 'Tom'67 #68 #69 # @overload fill_in([locator], with:, **options)70 # @param [String] locator Which field to fill in71 # @param [Hash] options72 # @param with: [String] The value to fill in73 # @macro waiting_behavior74 # @option options [String] currently_with The current value property of the field to fill in75 # @option options [Boolean] multiple Match fields that can have multiple values?76 # @option options [String, Regexp] id Match fields that match the id attribute77 # @option options [String] name Match fields that match the name attribute78 # @option options [String] placeholder Match fields that match the placeholder attribute79 # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided80 # @option options [Hash] fill_options Driver specific options regarding how to fill fields (Defaults come from {Capybara.configure default_set_options})81 #82 # @return [Capybara::Node::Element] The element filled in83 def fill_in(locator = nil, with:, currently_with: nil, fill_options: {}, **find_options)84 find_options[:with] = currently_with if currently_with85 find_options[:allow_self] = true if locator.nil?86 find(:fillable_field, locator, **find_options).set(with, **fill_options)87 end88 # @!macro label_click89 # @option options [Boolean] allow_label_click90 # Attempt to click the label to toggle state if element is non-visible. Defaults to {Capybara.configure automatic_label_click}.91 ##92 #93 # Find a descendant radio button and mark it as checked. The radio button can be found94 # via name, id, {Capybara.configure test_id} attribute or label text. If no locator is95 # provided this will match against self or a descendant.96 #97 # # will choose a descendant radio button with a name, id, or label text matching 'Male'98 # page.choose('Male')99 #100 # # will choose `el` if it's a radio button element101 # el.choose()102 #103 # @overload choose([locator], **options)104 # @param [String] locator Which radio button to choose105 #106 # @option options [String] option Value of the radio_button to choose107 # @option options [String, Regexp] id Match fields that match the id attribute108 # @option options [String] name Match fields that match the name attribute109 # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided110 # @macro waiting_behavior111 # @macro label_click112 #113 # @return [Capybara::Node::Element] The element chosen or the label clicked114 def choose(locator = nil, **options)115 _check_with_label(:radio_button, true, locator, **options)116 end117 ##118 #119 # Find a descendant check box and mark it as checked. The check box can be found120 # via name, id, {Capybara.configure test_id} attribute, or label text. If no locator121 # is provided this will match against self or a descendant.122 #123 # # will check a descendant checkbox with a name, id, or label text matching 'German'124 # page.check('German')125 #126 # # will check `el` if it's a checkbox element127 # el.check()128 #129 #130 # @overload check([locator], **options)131 # @param [String] locator Which check box to check132 #133 # @option options [String] option Value of the checkbox to select134 # @option options [String, Regexp] id Match fields that match the id attribute135 # @option options [String] name Match fields that match the name attribute136 # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided137 # @macro label_click138 # @macro waiting_behavior139 #140 # @return [Capybara::Node::Element] The element checked or the label clicked141 def check(locator = nil, **options)142 _check_with_label(:checkbox, true, locator, **options)143 end144 ##145 #146 # Find a descendant check box and uncheck it. The check box can be found147 # via name, id, {Capybara.configure test_id} attribute, or label text. If148 # no locator is provided this will match against self or a descendant.149 #150 # # will uncheck a descendant checkbox with a name, id, or label text matching 'German'151 # page.uncheck('German')152 #153 # # will uncheck `el` if it's a checkbox element154 # el.uncheck()155 #156 #157 # @overload uncheck([locator], **options)158 # @param [String] locator Which check box to uncheck159 #160 # @option options [String] option Value of the checkbox to deselect161 # @option options [String, Regexp] id Match fields that match the id attribute162 # @option options [String] name Match fields that match the name attribute163 # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided164 # @macro label_click165 # @macro waiting_behavior166 #167 # @return [Capybara::Node::Element] The element unchecked or the label clicked168 def uncheck(locator = nil, **options)169 _check_with_label(:checkbox, false, locator, **options)170 end171 ##172 #173 # If `from` option is present, {#select} finds a select box, or text input with associated datalist,174 # on the page and selects a particular option from it.175 # Otherwise it finds an option inside current scope and selects it.176 # If the select box is a multiple select, {#select} can be called multiple times to select more than177 # one option.178 # The select box can be found via its name, id, {Capybara.configure test_id} attribute, or label text.179 # The option can be found by its text.180 #181 # page.select 'March', from: 'Month'182 #183 # @overload select(value = nil, from: nil, **options)184 # @macro waiting_behavior185 #186 # @param value [String] Which option to select187 # @param from [String] The id, {Capybara.configure test_id} attribute, name or label of the select box188 #189 # @return [Capybara::Node::Element] The option element selected190 def select(value = nil, from: nil, **options)191 raise ArgumentError, 'The :from option does not take an element' if from.is_a? Capybara::Node::Element192 el = from ? find_select_or_datalist_input(from, options) : self193 if el.respond_to?(:tag_name) && (el.tag_name == 'input')194 select_datalist_option(el, value)195 else196 el.find(:option, value, **options).select_option197 end198 end199 ##200 #201 # Find a select box on the page and unselect a particular option from it. If the select202 # box is a multiple select, {#unselect} can be called multiple times to unselect more than203 # one option. The select box can be found via its name, id, {Capybara.configure test_id} attribute,204 # or label text.205 #206 # page.unselect 'March', from: 'Month'207 #208 # @overload unselect(value = nil, from: nil, **options)209 # @macro waiting_behavior210 #211 # @param value [String] Which option to unselect212 # @param from [String] The id, {Capybara.configure test_id} attribute, name or label of the select box213 #214 #215 # @return [Capybara::Node::Element] The option element unselected216 def unselect(value = nil, from: nil, **options)217 raise ArgumentError, 'The :from option does not take an element' if from.is_a? Capybara::Node::Element218 scope = from ? find(:select, from, **options) : self219 scope.find(:option, value, **options).unselect_option220 end221 ##222 #223 # Find a descendant file field on the page and attach a file given its path. There are two ways to use224 # {#attach_file}, in the first method the file field can be found via its name, id,225 # {Capybara.configure test_id} attribute, or label text. In the case of the file field being hidden for226 # styling reasons the `make_visible` option can be used to temporarily change the CSS of227 # the file field, attach the file, and then revert the CSS back to original. If no locator is228 # passed this will match self or a descendant.229 # The second method, which is currently in beta and may be changed/removed, involves passing a block230 # which performs whatever actions would trigger the file chooser to appear.231 #232 # # will attach file to a descendant file input element that has a name, id, or label_text matching 'My File'233 # page.attach_file('My File', '/path/to/file.png')234 #235 # # will attach file to el if it's a file input element236 # el.attach_file('/path/to/file.png')237 #238 # # will attach file to whatever file input is triggered by the block239 # page.attach_file('/path/to/file.png') do240 # page.find('#upload_button').click241 # end242 #243 # @overload attach_file([locator], paths, **options)244 # @macro waiting_behavior245 #246 # @param [String] locator Which field to attach the file to247 # @param [String, Array<String>] paths The path(s) of the file(s) that will be attached248 #249 # @option options [Symbol] match250 # The matching strategy to use (:one, :first, :prefer_exact, :smart). Defaults to {Capybara.configure match}.251 # @option options [Boolean] exact252 # Match the exact label name/contents or accept a partial match. Defaults to {Capybara.configure exact}.253 # @option options [Boolean] multiple Match field which allows multiple file selection254 # @option options [String, Regexp] id Match fields that match the id attribute255 # @option options [String] name Match fields that match the name attribute256 # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided257 # @option options [true, Hash] make_visible258 # A Hash of CSS styles to change before attempting to attach the file, if `true`, `{ opacity: 1, display: 'block', visibility: 'visible' }` is used (may not be supported by all drivers).259 # @overload attach_file(paths, &blk)260 # @param [String, Array<String>] paths The path(s) of the file(s) that will be attached261 # @yield Block whose actions will trigger the system file chooser to be shown262 # @return [Capybara::Node::Element] The file field element263 def attach_file(locator = nil, paths, make_visible: nil, **options) # rubocop:disable Style/OptionalArguments264 if locator && block_given?265 raise ArgumentError, '``#attach_file` does not support passing both a locator and a block'266 end267 Array(paths).each do |path|268 raise Capybara::FileNotFound, "cannot attach file, #{path} does not exist" unless File.exist?(path.to_s)269 end270 options[:allow_self] = true if locator.nil?271 if block_given?272 begin273 execute_script CAPTURE_FILE_ELEMENT_SCRIPT274 yield275 file_field = evaluate_script 'window._capybara_clicked_file_input'276 raise ArgumentError, "Capybara was unable to determine the file input you're attaching to" unless file_field277 rescue ::Capybara::NotSupportedByDriverError278 warn 'Block mode of `#attach_file` is not supported by the current driver - ignoring.'279 end280 end281 # Allow user to update the CSS style of the file input since they are so often hidden on a page282 if make_visible283 ff = file_field || find(:file_field, locator, **options.merge(visible: :all))284 while_visible(ff, make_visible) { |el| el.set(paths) }285 else286 (file_field || find(:file_field, locator, **options)).set(paths)287 end288 end289 private290 def find_select_or_datalist_input(from, options)291 synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do292 begin293 find(:select, from, **options)294 rescue Capybara::ElementNotFound => select_error # rubocop:disable Naming/RescuedExceptionsVariableName295 raise if %i[selected with_selected multiple].any? { |option| options.key?(option) }296 begin297 find(:datalist_input, from, **options)298 rescue Capybara::ElementNotFound => dlinput_error # rubocop:disable Naming/RescuedExceptionsVariableName299 raise Capybara::ElementNotFound, "#{select_error.message} and #{dlinput_error.message}"300 end301 end302 end303 end304 def select_datalist_option(input, value)305 datalist_options = input.evaluate_script(DATALIST_OPTIONS_SCRIPT)306 option = datalist_options.find { |opt| opt.values_at('value', 'label').include?(value) }307 raise ::Capybara::ElementNotFound, %(Unable to find datalist option "#{value}") unless option308 input.set(option['value'])309 rescue ::Capybara::NotSupportedByDriverError310 # Implement for drivers that don't support JS311 datalist = find(:xpath, XPath.descendant(:datalist)[XPath.attr(:id) == input[:list]], visible: false)312 option = datalist.find(:datalist_option, value, disabled: false)313 input.set(option.value)314 end315 def while_visible(element, visible_css)316 if visible_css == true317 visible_css = { opacity: 1, display: 'block', visibility: 'visible', width: 'auto', height: 'auto' }318 end319 _update_style(element, visible_css)320 unless element.visible?321 raise ExpectationNotMet, 'The style changes in :make_visible did not make the file input visible'322 end323 begin324 yield element325 ensure326 _reset_style(element)327 end328 end329 def _update_style(element, style)330 element.execute_script(UPDATE_STYLE_SCRIPT, style)331 rescue Capybara::NotSupportedByDriverError332 warn 'The :make_visible option is not supported by the current driver - ignoring'333 end334 def _reset_style(element)335 element.execute_script(RESET_STYLE_SCRIPT)336 rescue StandardError # rubocop:disable Lint/SuppressedException swallow extra errors337 end338 def _check_with_label(selector, checked, locator,339 allow_label_click: session_options.automatic_label_click, **options)340 options[:allow_self] = true if locator.nil?341 synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do342 begin343 el = find(selector, locator, **options)344 el.set(checked)345 rescue StandardError => e346 raise unless allow_label_click && catch_error?(e)347 begin348 el ||= find(selector, locator, **options.merge(visible: :all))349 el.session.find(:label, for: el, visible: true).click unless el.checked? == checked350 rescue StandardError # swallow extra errors - raise original351 raise e352 end353 end354 end355 end356 UPDATE_STYLE_SCRIPT = <<~'JS'357 this.capybara_style_cache = this.style.cssText;358 var css = arguments[0];359 for (var prop in css){360 if (css.hasOwnProperty(prop)) {361 this.style.setProperty(prop, css[prop], "important");362 }363 }364 JS365 RESET_STYLE_SCRIPT = <<~'JS'366 if (this.hasOwnProperty('capybara_style_cache')) {367 this.style.cssText = this.capybara_style_cache;368 delete this.capybara_style_cache;369 }370 JS371 DATALIST_OPTIONS_SCRIPT = <<~'JS'372 Array.prototype.slice.call((this.list||{}).options || []).373 filter(function(el){ return !el.disabled }).374 map(function(el){ return { "value": el.value, "label": el.label} })375 JS376 CAPTURE_FILE_ELEMENT_SCRIPT = <<~'JS'377 document.addEventListener('click', function file_catcher(e){378 if (e.target.matches("input[type='file']")) {379 window._capybara_clicked_file_input = e.target;380 this.removeEventListener('click', file_catcher);381 e.preventDefault();382 }383 })384 JS385 end386 end387end...

Full Screen

Full Screen

per_session_config_spec.rb

Source:per_session_config_spec.rb Github

copy

Full Screen

...8 session = Capybara::Session.new(:rack_test, TestApp)9 %i[default_host app_host always_include_port run_server10 default_selector default_max_wait_time ignore_hidden_elements11 automatic_reload match exact raise_server_errors visible_text_only12 automatic_label_click enable_aria_label save_path13 asset_host].each do |m|14 expect(session.config.public_send(m)).to eq Capybara.public_send(m)15 end16 end17 it "doesn't change global session when changed" do18 Capybara.threadsafe = true19 host = 'http://my.example.com'20 session = Capybara::Session.new(:rack_test, TestApp) do |config|21 config.default_host = host22 config.automatic_label_click = !config.automatic_label_click23 config.server_errors << ArgumentError24 end25 expect(Capybara.default_host).not_to eq host26 expect(session.config.default_host).to eq host27 expect(Capybara.automatic_label_click).not_to eq session.config.automatic_label_click28 expect(Capybara.server_errors).not_to eq session.config.server_errors29 end30 it "doesn't allow session configuration block when false" do31 Capybara.threadsafe = false32 expect do33 Capybara::Session.new(:rack_test, TestApp) { |config| }34 end.to raise_error 'A configuration block is only accepted when Capybara.threadsafe == true'35 end36 it "doesn't allow session config when false" do37 Capybara.threadsafe = false38 session = Capybara::Session.new(:rack_test, TestApp)39 expect { session.config.default_selector = :title }.to raise_error(/Per session settings are only supported when Capybara.threadsafe == true/)40 expect do41 session.configure do |config|...

Full Screen

Full Screen

label

Using AI Code Generation

copy

Full Screen

1fill_in('q', :with => 'Ruby')2click_button('Google Search')3click_link('Images')4click_link('Gmail')5click_link('Videos')6click_link('News')7click_link('Maps')8click_link('Shopping')9click_link('More')10click_link('Sign in')11click_link('About')12click_link('Advertising')13click_link('Business')14click_link('How Search works')15click_link('Privacy')16click_link('Terms')17click_link('Settings')18click_link('Send feedback')19click_link('Help')20click_link('Report an issue')21click_link('Google.com')22click_link('© 2013 - Privacy - Terms')23click_link('Advertising')24click_link('Business')25click_link('About')26click_link('How Search works')27click_link('Privacy')28click_link('Terms')29click_link('Settings')30click_link('Send feedback')31click_link('Help')32click_link('Report an issue')33click_link('Google.com')34click_link('© 2013 - Privacy - Terms')35click_link('Advertising')36click_link('Business')37click_link('About')38click_link('How Search works')39click_link('Privacy')40click_link('Terms')41click_link('Settings')42click_link('Send feedback')43click_link('Help')44click_link('Report an issue')45click_link('Google.com')46click_link('© 2013 - Privacy - Terms')47click_link('Advertising')48click_link('Business')49click_link('About')50click_link('How Search works')51click_link('Privacy')52click_link('Terms')53click_link('Settings')54click_link('Send feedback')55click_link('Help')56click_link('Report an issue')57click_link('Google.com')58click_link('© 2013 - Privacy - Terms')59click_link('Advertising')60click_link('Business')61click_link('About')62click_link('How Search works')63click_link('Privacy')64click_link('Terms')65click_link('Settings')66click_link('Send feedback')67click_link('Help')68click_link('Report an issue')69click_link('Google.com')

Full Screen

Full Screen

label

Using AI Code Generation

copy

Full Screen

1Capybara.visit('/')2Capybara.fill_in('q', :with => 'Selenium')3Capybara.click_button('Google Search')4Capybara.click_link('Selenium - Web Browser Automation')5Capybara.label('Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.')6 visit('/')7 fill_in('q', :with => 'Selenium')8 click_button('Google Search')9 click_link('Selenium - Web Browser Automation')10 label('Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.')11RSpec::Core::RakeTask.new(:spec)

Full Screen

Full Screen

label

Using AI Code Generation

copy

Full Screen

1 def search_for(search_term)2 fill_in('q', with: search_term)3 click_button('Google Search')4 all('h3.r a').map(&:text)5 let(:google_page) { GooglePage.new }6 google_page.search_for('capybara')7 expect(google_page.get_results).to_not be_empty

Full Screen

Full Screen

label

Using AI Code Generation

copy

Full Screen

1 visit('/')2 visit('/')3 visit('/')

Full Screen

Full Screen

label

Using AI Code Generation

copy

Full Screen

1Capybara.label('User Name')2Capybara.button('Login')3Capybara.link('Forgot Password')4Capybara.field('User Name')5Capybara.select('Country')6Capybara.label('User Name')7Capybara.button('Login')8Capybara.link('Forgot Password')9Capybara.field('User Name')10Capybara.select('Country')11Capybara.label('User Name')12Capybara.button('Login')13Capybara.link('Forgot Password')14Capybara.field('User Name')15Capybara.select('Country')16Capybara.label('User Name')17Capybara.button('Login')18Capybara.link('Forgot Password')19Capybara.field('User Name')20Capybara.select('Country')21Capybara.label('User Name')22Capybara.button('Login')23Capybara.link('Forgot Password')24Capybara.field('User Name')

Full Screen

Full Screen

label

Using AI Code Generation

copy

Full Screen

1Capybara.label('Name')2Capybara.current_session.label('Name')3Capybara.current_session.find('form').label('Name')4Capybara.current_session.find('form').find('label')5Capybara.current_session.find('form').find('label', text: 'Name')6Capybara.current_session.find('form').find(:label, 'Name')7Capybara.current_session.find('form').find(:label, text: 'Name')8Capybara.current_session.find('form').find(:label, 'Name', text: 'Name')9Capybara.current_session.find('form').find(:label, text: 'Name', text: 'Name')10Capybara.current_session.find('form').find(:label, text: 'Name', text: 'Name', exact_text: true)11Capybara.current_session.find('form').find(:label, text: 'Name', text: 'Name', exact_text: false)12Capybara.current_session.find('form').find(:label, text: 'Name', text: 'Name', exact_text: true, exact: true)

Full Screen

Full Screen

label

Using AI Code Generation

copy

Full Screen

1Capybara.label('some_label_text').click2Capybara.field('some_field_text').fill_with('some_value')3Capybara.button('some_button_text').click4Capybara.link('some_link_text').click5Capybara.select('some_select_text').choose('some_option')6Capybara.select('some_select_text').unselect('some_option')7Capybara.checkbox('some_checkbox_text').check8Capybara.checkbox('some_checkbox_text').uncheck9Capybara.radio('some_radio_text').choose10Capybara.file_field('some_file_field_text').attach_file('some_file_path')11Capybara.table('some_table_text').row('some_row_text').cell('some_cell_text')12Capybara.table('some

Full Screen

Full Screen

label

Using AI Code Generation

copy

Full Screen

1 visit('/')2 visit('/')3 visit('/')

Full Screen

Full Screen

label

Using AI Code Generation

copy

Full Screen

1Capybara.label('Name')2Capybara.current_session.label('Name')3Capybara.current_session.find('form').label('Name')4Capybara.current_session.find('form').find('label')5Capybara.current_session.find('form').find('label', text: 'Name')6Capybara.current_session.find('form').find(:label, 'Name')7Capybara.current_session.find('form').find(:label, text: 'Name')8Capybara.current_session.find('form').find(:label, 'Name', text: 'Name')9Capybara.current_session.find('form').find(:label, text: 'Name', text: 'Name')10Capybara.current_session.find('form').find(:label, text: 'Name', text: 'Name', exact_text: true)11Capybara.current_session.find('form').find(:label, text: 'Name', text: 'Name', exact_text: false)12Capybara.current_session.find('form').find(:label, text: 'Name', text: 'Name', exact_text: true, exact: true)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Capybara automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful