How to use click method of Capybara.Node Package

Best Capybara code snippet using Capybara.Node.click

actions.rb

Source:actions.rb Github

copy

Full Screen

...3 module Node4 module Actions5 ##6 #7 # Finds a button or link and clicks it. See {Capybara::Node::Actions#click_button} and8 # {Capybara::Node::Actions#click_link} for what locator will match against for each type of element9 # @!macro waiting_behavior10 # If the driver is capable of executing JavaScript, +$0+ will wait for a set amount of time11 # and continuously retry finding the element until either the element is found or the time12 # expires. The length of time +find+ will wait is controlled through {Capybara.default_max_wait_time}13 #14 # @option options [false, Numeric] wait (Capybara.default_max_wait_time) Maximum time to wait for matching element to appear.15 #16 # @overload click_link_or_button([locator], options)17 #18 # @param [String] locator See {Capybara::Node::Actions#click_button} and {Capybara::Node::Actions#click_link}19 #20 # @return [Capybara::Node::Element] The element clicked21 #22 def click_link_or_button(locator = nil, **options)23 find(:link_or_button, locator, options).click24 end25 alias_method :click_on, :click_link_or_button26 ##27 #28 # Finds a link by id, Capybara.test_id attribute, text or title and clicks it. Also looks at image29 # alt text inside the link.30 #31 # @macro waiting_behavior32 #33 # @overload click_link([locator], options)34 # @param [String] locator text, id, Capybara.test_id attribute, title or nested image's alt attribute35 # @param options See {Capybara::Node::Finders#find_link}36 #37 # @return [Capybara::Node::Element] The element clicked38 def click_link(locator = nil, **options)39 find(:link, locator, options).click40 end41 ##42 #43 # Finds a button on the page and clicks it.44 # This can be any \<input> element of type submit, reset, image, button or it can be a45 # \<button> element. All buttons can be found by their id, Capybara.test_id attribute, value, or title. \<button> elements can also be found46 # by their text content, and image \<input> elements by their alt attribute47 #48 # @macro waiting_behavior49 #50 # @overload click_button([locator], **options)51 # @param [String] locator Which button to find52 # @param options See {Capybara::Node::Finders#find_button}53 # @return [Capybara::Node::Element] The element clicked54 def click_button(locator = nil, **options)55 find(:button, locator, options).click56 end57 ##58 #59 # Locate a text field or text area and fill it in with the given text60 # The field can be found via its name, id, Capybara.test_id attribute, or label text.61 #62 # page.fill_in 'Name', with: 'Bob'63 #64 #65 # @overload fill_in([locator], with:, **options)66 # @param [String] locator Which field to fill in67 # @param [Hash] options68 # @param with: [String] The value to fill_in69 # @macro waiting_behavior70 # @option options [String] currently_with The current value property of the field to fill in71 # @option options [Boolean] multiple Match fields that can have multiple values?72 # @option options [String] id Match fields that match the id attribute73 # @option options [String] name Match fields that match the name attribute74 # @option options [String] placeholder Match fields that match the placeholder attribute75 # @option options [String, Array<String>] class Match fields that match the class(es) provided76 # @option options [Hash] fill_options Driver specific options regarding how to fill fields (Defaults come from Capybara.default_set_options)77 #78 # @return [Capybara::Node::Element] The element filled_in79 def fill_in(locator = nil, with:, currently_with: nil, fill_options: {}, **find_options)80 find_options[:with] = currently_with if currently_with81 find(:fillable_field, locator, find_options).set(with, fill_options)82 end83 # @!macro label_click84 # @option options [Boolean] allow_label_click (Capybara.automatic_label_click) Attempt to click the label to toggle state if element is non-visible.85 ##86 #87 # Find a radio button and mark it as checked. The radio button can be found88 # via name, id or label text.89 #90 # page.choose('Male')91 #92 # @overload choose([locator], **options)93 # @param [String] locator Which radio button to choose94 #95 # @option options [String] option Value of the radio_button to choose96 # @option options [String] id Match fields that match the id attribute97 # @option options [String] name Match fields that match the name attribute98 # @option options [String, Array<String>] class Match fields that match the class(es) provided99 # @macro waiting_behavior100 # @macro label_click101 #102 # @return [Capybara::Node::Element] The element chosen or the label clicked103 def choose(locator = nil, **options)104 _check_with_label(:radio_button, true, locator, options)105 end106 ##107 #108 # Find a check box and mark it as checked. The check box can be found109 # via name, id or label text.110 #111 # page.check('German')112 #113 #114 # @overload check([locator], **options)115 # @param [String] locator Which check box to check116 #117 # @option options [String] option Value of the checkbox to select118 # @option options [String] id Match fields that match the id attribute119 # @option options [String] name Match fields that match the name attribute120 # @option options [String, Array<String>] class Match fields that match the class(es) provided121 # @macro label_click122 # @macro waiting_behavior123 #124 # @return [Capybara::Node::Element] The element checked or the label clicked125 def check(locator = nil, **options)126 _check_with_label(:checkbox, true, locator, options)127 end128 ##129 #130 # Find a check box and mark uncheck it. The check box can be found131 # via name, id or label text.132 #133 # page.uncheck('German')134 #135 #136 # @overload uncheck([locator], **options)137 # @param [String] locator Which check box to uncheck138 #139 # @option options [String] option Value of the checkbox to deselect140 # @option options [String] id Match fields that match the id attribute141 # @option options [String] name Match fields that match the name attribute142 # @option options [String, Array<String>] class Match fields that match the class(es) provided143 # @macro label_click144 # @macro waiting_behavior145 #146 # @return [Capybara::Node::Element] The element unchecked or the label clicked147 def uncheck(locator = nil, **options)148 _check_with_label(:checkbox, false, locator, options)149 end150 ##151 #152 # If `:from` option is present, `select` finds a select box, or text input with associated datalist,153 # on the page and selects a particular option from it.154 # Otherwise it finds an option inside current scope and selects it.155 # If the select box is a multiple select, +select+ can be called multiple times to select more than156 # one option.157 # The select box can be found via its name, id or label text. The option can be found by its text.158 #159 # page.select 'March', from: 'Month'160 #161 # @macro waiting_behavior162 #163 # @param value [String] Which option to select164 # @param from: [String] The id, Capybara.test_id atrtribute, name or label of the select box165 #166 # @return [Capybara::Node::Element] The option element selected167 def select(value = nil, from: nil, **options)168 el = from ? find_select_or_datalist_input(from, options) : self169 if el.respond_to?(:tag_name) && (el.tag_name == 'input')170 select_datalist_option(el, value)171 else172 el.find(:option, value, options).select_option173 end174 end175 ##176 #177 # Find a select box on the page and unselect a particular option from it. If the select178 # box is a multiple select, +unselect+ can be called multiple times to unselect more than179 # one option. The select box can be found via its name, id or label text.180 #181 # page.unselect 'March', from: 'Month'182 #183 # @macro waiting_behavior184 #185 # @param value [String] Which option to unselect186 # @param from: [String] The id, Capybara.test_id attribute, name or label of the select box187 #188 # @return [Capybara::Node::Element] The option element unselected189 def unselect(value = nil, from: nil, **options)190 scope = from ? find(:select, from, options) : self191 scope.find(:option, value, options).unselect_option192 end193 ##194 #195 # Find a file field on the page and attach a file given its path. The file field can196 # be found via its name, id or label text. In the case of the file field being hidden for197 # styling reasons the `make_visible` option can be used to temporarily change the CSS of198 # the file field, attach the file, and then revert the CSS back to original.199 #200 # page.attach_file(locator, '/path/to/file.png')201 #202 # @overload attach_file([locator], path, **options)203 # @macro waiting_behavior204 #205 # @param [String] locator Which field to attach the file to206 # @param [String] path The path of the file that will be attached, or an array of paths207 #208 # @option options [Symbol] match (Capybara.match) The matching strategy to use (:one, :first, :prefer_exact, :smart).209 # @option options [Boolean] exact (Capybara.exact) Match the exact label name/contents or accept a partial match.210 # @option options [Boolean] multiple Match field which allows multiple file selection211 # @option options [String] id Match fields that match the id attribute212 # @option options [String] name Match fields that match the name attribute213 # @option options [String, Array<String>] class Match fields that match the class(es) provided214 # @option options [true, Hash] make_visible 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)215 #216 # @return [Capybara::Node::Element] The file field element217 def attach_file(locator = nil, path, make_visible: nil, **options) # rubocop:disable Style/OptionalArguments218 Array(path).each do |p|219 raise Capybara::FileNotFound, "cannot attach file, #{p} does not exist" unless File.exist?(p.to_s)220 end221 # Allow user to update the CSS style of the file input since they are so often hidden on a page222 if make_visible223 ff = find(:file_field, locator, options.merge(visible: :all))224 while_visible(ff, make_visible) { |el| el.set(path) }225 else226 find(:file_field, locator, options).set(path)227 end228 end229 private230 def find_select_or_datalist_input(from, options)231 synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do232 begin233 find(:select, from, options)234 rescue Capybara::ElementNotFound => select_error235 raise if %i[selected with_selected multiple].any? { |option| options.key?(option) }236 begin237 find(:datalist_input, from, options)238 rescue Capybara::ElementNotFound => dlinput_error239 raise Capybara::ElementNotFound, "#{select_error.message} and #{dlinput_error.message}"240 end241 end242 end243 end244 def select_datalist_option(input, value)245 datalist_options = input.evaluate_script(DATALIST_OPTIONS_SCRIPT)246 option = datalist_options.find { |o| o.values_at('value', 'label').include?(value) }247 raise ::Capybara::ElementNotFound, %(Unable to find datalist option "#{value}") unless option248 input.set(option['value'])249 rescue ::Capybara::NotSupportedByDriverError250 # Implement for drivers that don't support JS251 datalist = find(:xpath, XPath.descendant(:datalist)[XPath.attr(:id) == input[:list]], visible: false)252 option = datalist.find(:datalist_option, value, disabled: false)253 input.set(option.value)254 end255 def while_visible(element, visible_css)256 visible_css = { opacity: 1, display: 'block', visibility: 'visible' } if visible_css == true257 _update_style(element, visible_css)258 raise ExpectationNotMet, 'The style changes in :make_visible did not make the file input visible' unless element.visible?259 begin260 yield element261 ensure262 _reset_style(element)263 end264 end265 def _update_style(element, style)266 element.execute_script(UPDATE_STYLE_SCRIPT, style)267 rescue Capybara::NotSupportedByDriverError268 warn 'The :make_visible option is not supported by the current driver - ignoring'269 end270 def _reset_style(element)271 element.execute_script(RESET_STYLE_SCRIPT)272 rescue StandardError # rubocop:disable Lint/HandleExceptions swallow extra errors273 end274 def _check_with_label(selector, checked, locator, allow_label_click: session_options.automatic_label_click, **options)275 synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do276 begin277 el = find(selector, locator, options)278 el.set(checked)279 rescue StandardError => e280 raise unless allow_label_click && catch_error?(e)281 begin282 el ||= find(selector, locator, options.merge(visible: :all))283 find(:label, for: el, visible: true).click unless el.checked? == checked284 rescue StandardError # swallow extra errors - raise original285 raise e286 end287 end288 end289 end290 UPDATE_STYLE_SCRIPT = <<~'JS'291 this.capybara_style_cache = this.style.cssText;292 var css = arguments[0];293 for (var prop in css){294 if (css.hasOwnProperty(prop)) {295 this.style[prop] = css[prop]296 }297 }...

Full Screen

Full Screen

selenium_spec_safari.rb

Source:selenium_spec_safari.rb Github

copy

Full Screen

...31skipped_tests = %i[response_headers status_code trigger windows drag]32Capybara::SpecHelper.log_selenium_driver_version(Selenium::WebDriver::Safari) if ENV['CI']33Capybara::SpecHelper.run_specs TestSessions::Safari, SAFARI_DRIVER.to_s, capybara_skip: skipped_tests do |example|34 case example.metadata[:full_description]35 when /click_link can download a file/36 skip "safaridriver doesn't provide a way to set the download directory"37 when /Capybara::Session selenium_safari Capybara::Window#maximize/38 pending "Safari headless doesn't support maximize" if ENV['HEADLESS']39 when /Capybara::Session selenium_safari #visit without a server/,40 /Capybara::Session selenium_safari #visit with Capybara.app_host set should override server/,41 /Capybara::Session selenium_safari #reset_session! When reuse_server == false raises any standard errors caught inside the server during a second session/42 skip "Safari webdriver doesn't support multiple sessions"43 when /Capybara::Session selenium_safari #click_link with alternative text given to a contained image/,44 'Capybara::Session selenium_safari #click_link_or_button with enable_aria_label should click on link'45 pending 'safaridriver thinks these links are non-interactable for some unknown reason'46 when /Capybara::Session selenium_safari #attach_file with a block can upload by clicking the file input/47 skip "safaridriver doesn't allow clicking on file inputs"48 when /Capybara::Session selenium_safari #within_frame works if the frame is closed/,49 /Capybara::Session selenium_safari #switch_to_frame works if the frame is closed/50 skip 'Safari has a race condition when clicking an element that causes the frame to close. It will sometimes raise a NoSuchFrameError'51 when /Capybara::Session selenium_safari #reset_session! removes ALL cookies/52 skip 'Safari webdriver can only remove cookies for the current domain'53 when /Capybara::Session selenium_safari #refresh it reposts/54 skip "Safari opens an alert that can't be closed"55 when 'Capybara::Session selenium_safari node #double_click should allow to adjust the offset',56 'Capybara::Session selenium_safari node #double_click should double click an element'57 pending "safardriver doesn't generate a double click event"58 when 'Capybara::Session selenium_safari node #click should allow multiple modifiers',59 /Capybara::Session selenium_safari node #(click|right_click|double_click) should allow modifiers/60 pending "safaridriver doesn't take key state into account when clicking"61 when 'Capybara::Session selenium_safari #fill_in on a pre-populated textfield with a reformatting onchange should trigger change when clearing field'62 pending "safardriver clear doesn't generate change event"63 when 'Capybara::Session selenium_safari #go_back should fetch a response from the driver from the previous page',64 'Capybara::Session selenium_safari #go_forward should fetch a response from the driver from the previous page'65 skip 'safaridriver loses the ability to find elements in the document after `go_back`'66 when /drag_to.*HTML5/67 pending "Safari doesn't support"68 end69end70RSpec.describe 'Capybara::Session with safari' do71 include Capybara::SpecHelper72 ['Capybara::Session', 'Capybara::Node', Capybara::RSpecMatchers].each do |examples|73 include_examples examples, TestSessions::Safari, SAFARI_DRIVER74 end75 context 'storage' do76 describe '#reset!' do77 it 'clears storage by default' do78 session = TestSessions::Safari79 session.visit('/with_js')80 session.find(:css, '#set-storage').click81 session.reset!82 session.visit('/with_js')83 expect(session.evaluate_script('Object.keys(localStorage)')).to be_empty84 expect(session.evaluate_script('Object.keys(sessionStorage)')).to be_empty85 end86 it 'does not clear storage when false' do87 skip "Safari webdriver doesn't support multiple sessions"88 session = Capybara::Session.new(:selenium_safari_not_clear_storage, TestApp)89 session.visit('/with_js')90 session.find(:css, '#set-storage').click91 session.reset!92 session.visit('/with_js')93 expect(session.evaluate_script('Object.keys(localStorage)')).not_to be_empty94 expect(session.evaluate_script('Object.keys(sessionStorage)')).not_to be_empty95 end96 end97 end98 context 'timeout' do99 it 'sets the http client read timeout' do100 expect(TestSessions::Safari.driver.browser.send(:bridge).http.read_timeout).to eq 30101 end102 end103 describe 'filling in Safari-specific date and time fields with keystrokes' do104 let(:datetime) { Time.new(1983, 6, 19, 6, 30) }105 let(:session) { TestSessions::Safari }106 before do107 session.visit('/form')108 end109 it 'should fill in a date input with a String' do110 pending "Safari doesn't support date inputs"111 session.fill_in('form_date', with: '06/19/1983')112 session.click_button('awesome')113 expect(Date.parse(extract_results(session)['date'])).to eq datetime.to_date114 end115 it 'should fill in a time input with a String' do116 # Safari doesn't support time inputs - so this is just a text input117 session.fill_in('form_time', with: '06:30A')118 session.click_button('awesome')119 results = extract_results(session)['time']120 expect(Time.parse(results).strftime('%r')).to eq datetime.strftime('%r')121 end122 it 'should fill in a datetime input with a String' do123 pending "Safari doesn't support datetime inputs"124 session.fill_in('form_datetime', with: "06/19/1983\t06:30A")125 session.click_button('awesome')126 expect(Time.parse(extract_results(session)['datetime'])).to eq datetime127 end128 end129end...

Full Screen

Full Screen

click

Using AI Code Generation

copy

Full Screen

1 def search_for(term)2google.search_for("Selenium")3find_button("Google Search").click

Full Screen

Full Screen

click

Using AI Code Generation

copy

Full Screen

1 driver.click(self.native)2 config.allow_url("http://google.com")3 config.allow_url("http://google.com")4 config.block_url("http://google.com")5 config.allow_url("http://google.com")6 config.block_url("http://google.com")7 config.allow_url("http://google.com")8 config.block_url("http://google.com")9 config.allow_url("http://google.com")10 config.allow_url("http://google.com")11 config.allow_url("http://google.com")12 config.block_url("http://google.com")13 config.allow_url("http://google.com")14 config.allow_url("http://google.com")15 config.block_url("http://google.com")16 config.block_url("http://google.com")17 config.allow_url("http://google.com")18 config.allow_url("http://google.com")19 config.block_url("http://google.com")20 config.block_url("http://google.com")21 config.allow_url("http://google.com")22 config.allow_url("http://google.com")23 config.allow_url("http://google.com")24 config.block_url("http://google.com")25 config.allow_url("http://google.com")26 config.block_url("

Full Screen

Full Screen

click

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', :with => 'Selenium')3click_button('btnK')4click_link('Selenium - Web Browser Automation')5visit('/')6fill_in('q', :with => 'Selenium')7click_button('btnK')8click_link('Selenium - Web Browser Automation')9visit('/')10fill_in('q', :with => 'Selenium')11click_button('btnK')12click_link('Selenium - Web Browser Automation')13visit('/')14fill_in('q', :with => 'Selenium')15click_button('btnK')16click_link('Selenium - Web Browser Automation')

Full Screen

Full Screen

click

Using AI Code Generation

copy

Full Screen

1node = page.find('a', :text => 'some link')2page.click_link('some link')3page.click_link('some link')4page.click_link('some link')5page.click_link('some link')6page.click_link('some link')7page.click_link('some link')8page.click_link('some link')9page.click_link('some link')10page.click_link('some link')11page.click_link('some link')12page.click_link('some link')13page.click_link('some link')14page.click_link('some link')

Full Screen

Full Screen

click

Using AI Code Generation

copy

Full Screen

1visit('/')2visit('/')3page.click_link('Sign in')4visit('/')5page.click_button('I agree')6visit('/')7page.click_link_or_button('I agree')

Full Screen

Full Screen

click

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', :with => 'Selenium')3click_button('btnK')4click_link('Selenium - Web Browser Automation')5visit('/')6fill_in('q', :with => 'Selenium')7click_button('btnK')8click_link('Selenium - Web Browser Automation')9visit('/')10fill_in('q', :with => 'Selenium')11click_button('btnK')12click_link('Selenium - Web Browser Automation')13visit('/')14fill_in('q', :with => 'Selenium')15click_button('btnK')16click_link('Selenium - Web Browser Automation')

Full Screen

Full Screen

click

Using AI Code Generation

copy

Full Screen

1node = page.find('a', :text => 'some link')2page.click_link('some link')3page.click_link('some link')4page.click_link('some link')5page.click_link('some link')6page.click_link('some link')7page.click_link('some link')8page.click_link('some link')9page.click_link('some link')10page.click_link('some link')11page.click_link('some link')12page.click_link('some link')13page.click_link('some link')14page.click_link('some link')

Full Screen

Full Screen

click

Using AI Code Generation

copy

Full Screen

1visit('/')2visit('/')3page.click_link('Sign in')4visit('/')5page.click_button('I agree')6visit('/')7page.click_link_or_button('I agree')

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful