How to use perform_click_action method of Capybara.Node Package

Best Capybara code snippet using Capybara.Node.perform_click_action

element.rb

Source:element.rb Github

copy

Full Screen

...149 # @option options [Integer] y Y coordinate to offset the click location. If {Capybara.configure w3c_click_offset} is `true` the150 # offset will be from the element center, otherwise it will be from the top left corner of the element151 # @return [Capybara::Node::Element] The element152 def click(*keys, **options)153 perform_click_action(keys, options) do |k, opts|154 base.click(k, opts)155 end156 end157 ##158 #159 # Right Click the Element.160 #161 # @macro action_waiting_behavior162 # @macro click_modifiers163 # @return [Capybara::Node::Element] The element164 def right_click(*keys, **options)165 perform_click_action(keys, options) do |k, opts|166 base.right_click(k, opts)167 end168 end169 ##170 #171 # Double Click the Element.172 #173 # @macro action_waiting_behavior174 # @macro click_modifiers175 # @return [Capybara::Node::Element] The element176 def double_click(*keys, **options)177 perform_click_action(keys, options) do |k, opts|178 base.double_click(k, opts)179 end180 end181 ##182 #183 # Send Keystrokes to the Element.184 #185 # @overload send_keys(keys, ...)186 # @param keys [String, Symbol, Array<String,Symbol>]187 #188 # Examples:189 #190 # element.send_keys "foo" #=> value: 'foo'191 # element.send_keys "tet", :left, "s" #=> value: 'test'192 # element.send_keys [:control, 'a'], :space #=> value: ' ' - assuming ctrl-a selects all contents193 #194 # Symbols supported for keys:195 # * :cancel196 # * :help197 # * :backspace198 # * :tab199 # * :clear200 # * :return201 # * :enter202 # * :shift203 # * :control204 # * :alt205 # * :pause206 # * :escape207 # * :space208 # * :page_up209 # * :page_down210 # * :end211 # * :home212 # * :left213 # * :up214 # * :right215 # * :down216 # * :insert217 # * :delete218 # * :semicolon219 # * :equals220 # * :numpad0221 # * :numpad1222 # * :numpad2223 # * :numpad3224 # * :numpad4225 # * :numpad5226 # * :numpad6227 # * :numpad7228 # * :numpad8229 # * :numpad9230 # * :multiply - numeric keypad *231 # * :add - numeric keypad +232 # * :separator - numeric keypad 'separator' key ??233 # * :subtract - numeric keypad -234 # * :decimal - numeric keypad .235 # * :divide - numeric keypad /236 # * :f1237 # * :f2238 # * :f3239 # * :f4240 # * :f5241 # * :f6242 # * :f7243 # * :f8244 # * :f9245 # * :f10246 # * :f11247 # * :f12248 # * :meta249 # * :command - alias of :meta250 #251 # @return [Capybara::Node::Element] The element252 def send_keys(*args)253 synchronize { base.send_keys(*args) }254 self255 end256 ##257 #258 # Hover on the Element.259 #260 # @return [Capybara::Node::Element] The element261 def hover262 synchronize { base.hover }263 self264 end265 ##266 #267 # @return [String] The tag name of the element268 #269 def tag_name270 # Element type is immutable so cache it271 @tag_name ||= initial_cache[:tag_name] || synchronize { base.tag_name }272 end273 ##274 #275 # Whether or not the element is visible. Not all drivers support CSS, so276 # the result may be inaccurate.277 #278 # @return [Boolean] Whether the element is visible279 #280 def visible?281 synchronize { base.visible? }282 end283 ##284 #285 # Whether or not the element is currently in the viewport and it (or descendants)286 # would be considered clickable at the elements center point.287 #288 # @return [Boolean] Whether the elements center is obscured.289 #290 def obscured?291 synchronize { base.obscured? }292 end293 ##294 #295 # Whether or not the element is checked.296 #297 # @return [Boolean] Whether the element is checked298 #299 def checked?300 synchronize { base.checked? }301 end302 ##303 #304 # Whether or not the element is selected.305 #306 # @return [Boolean] Whether the element is selected307 #308 def selected?309 synchronize { base.selected? }310 end311 ##312 #313 # Whether or not the element is disabled.314 #315 # @return [Boolean] Whether the element is disabled316 #317 def disabled?318 synchronize { base.disabled? }319 end320 ##321 #322 # Whether or not the element is readonly.323 #324 # @return [Boolean] Whether the element is readonly325 #326 def readonly?327 synchronize { base.readonly? }328 end329 ##330 #331 # Whether or not the element supports multiple results.332 #333 # @return [Boolean] Whether the element supports multiple results.334 #335 def multiple?336 synchronize { base.multiple? }337 end338 ##339 #340 # An XPath expression describing where on the page the element can be found.341 #342 # @return [String] An XPath expression343 #344 def path345 synchronize { base.path }346 end347 def rect348 synchronize { base.rect }349 end350 ##351 #352 # Trigger any event on the current element, for example mouseover or focus353 # events. Not supported with the Selenium driver, and SHOULDN'T BE USED IN TESTING unless you354 # fully understand why you're using it, that it can allow actions a user could never355 # perform, and that it may completely invalidate your test.356 #357 # @param [String] event The name of the event to trigger358 #359 # @return [Capybara::Node::Element] The element360 def trigger(event)361 synchronize { base.trigger(event) }362 self363 end364 ##365 #366 # Drag the element to the given other element.367 #368 # source = page.find('#foo')369 # target = page.find('#bar')370 # source.drag_to(target)371 #372 # @param [Capybara::Node::Element] node The element to drag to373 # @param [Hash] options Driver specific options for dragging. May not be supported by all drivers.374 # @option options [Numeric] :delay (0.05) When using Chrome/Firefox with Selenium and HTML5 dragging this is the number375 # of seconds between each stage of the drag.376 # @option options [Boolean] :html5 When using Chrome/Firefox with Selenium enables to force the use of HTML5377 # (true) or legacy (false) dragging. If not specified the driver will attempt to378 # detect the correct method to use.379 #380 # @return [Capybara::Node::Element] The dragged element381 def drag_to(node, **options)382 synchronize { base.drag_to(node.base, **options) }383 self384 end385 ##386 #387 # Drop items on the current element.388 #389 # target = page.find('#foo')390 # target.drop('/some/path/file.csv')391 #392 # @overload drop(path, ...)393 # @param [String, #to_path] path Location of the file to drop on the element394 #395 # @overload drop(strings, ...)396 # @param [Hash] strings A hash of type to data to be dropped - `{ "text/url" => "https://www.google.com" }`397 #398 # @return [Capybara::Node::Element] The element399 def drop(*args)400 options = args.map do |arg|401 return arg.to_path if arg.respond_to?(:to_path)402 arg403 end404 synchronize { base.drop(*options) }405 self406 end407 ##408 #409 # Scroll the page or element.410 #411 # @overload scroll_to(position, offset: [0,0])412 # Scroll the page or element to its top, bottom or middle.413 # @param [:top, :bottom, :center, :current] position414 # @param [[Integer, Integer]] offset415 #416 # @overload scroll_to(element, align: :top)417 # Scroll the page or current element until the given element is aligned at the top, bottom, or center of it.418 # @param [Capybara::Node::Element] element The element to be scrolled into view419 # @param [:top, :bottom, :center] align Where to align the element being scrolled into view with relation to the current page/element if possible420 #421 # @overload scroll_to(x,y)422 # @param [Integer] x Horizontal scroll offset423 # @param [Integer] y Vertical scroll offset424 #425 # @return [Capybara::Node::Element] The element426 def scroll_to(pos_or_el_or_x, y = nil, align: :top, offset: nil)427 case pos_or_el_or_x428 when Symbol429 synchronize { base.scroll_to(nil, pos_or_el_or_x) } unless pos_or_el_or_x == :current430 when Capybara::Node::Element431 synchronize { base.scroll_to(pos_or_el_or_x.base, align) }432 else433 synchronize { base.scroll_to(nil, nil, [pos_or_el_or_x, y]) }434 end435 synchronize { base.scroll_by(*offset) } unless offset.nil?436 self437 end438 ##439 #440 # Execute the given JS in the context of the element not returning a result. This is useful for scripts that return441 # complex objects, such as jQuery statements. {#execute_script} should be used over442 # {#evaluate_script} whenever a result is not expected or needed. `this` in the script will refer to the element this is called on.443 #444 # @param [String] script A string of JavaScript to execute445 # @param args Optional arguments that will be passed to the script. Driver support for this is optional and types of objects supported may differ between drivers446 #447 def execute_script(script, *args)448 session.execute_script(<<~JS, self, *args)449 (function (){450 #{script}451 }).apply(arguments[0], Array.prototype.slice.call(arguments,1));452 JS453 end454 ##455 #456 # Evaluate the given JS in the context of the element and return the result. Be careful when using this with457 # scripts that return complex objects, such as jQuery statements. {#execute_script} might458 # be a better alternative. `this` in the script will refer to the element this is called on.459 #460 # @param [String] script A string of JavaScript to evaluate461 # @return [Object] The result of the evaluated JavaScript (may be driver specific)462 #463 def evaluate_script(script, *args)464 session.evaluate_script(<<~JS, self, *args)465 (function(){466 return #{script.strip}467 }).apply(arguments[0], Array.prototype.slice.call(arguments,1));468 JS469 end470 ##471 #472 # Evaluate the given JavaScript in the context of the element and obtain the result from a473 # callback function which will be passed as the last argument to the script. `this` in the474 # script will refer to the element this is called on.475 #476 # @param [String] script A string of JavaScript to evaluate477 # @return [Object] The result of the evaluated JavaScript (may be driver specific)478 #479 def evaluate_async_script(script, *args)480 session.evaluate_async_script(<<~JS, self, *args)481 (function (){482 #{script}483 }).apply(arguments[0], Array.prototype.slice.call(arguments,1));484 JS485 end486 ##487 #488 # Toggle the elements background color between white and black for a period of time.489 #490 # @return [Capybara::Node::Element] The element491 def flash492 execute_script(<<~JS, 100)493 async function flash(el, delay){494 var old_bg = el.style.backgroundColor;495 var colors = ["black", "white"];496 for(var i=0; i<20; i++){497 el.style.backgroundColor = colors[i % colors.length];498 await new Promise(resolve => setTimeout(resolve, delay));499 }500 el.style.backgroundColor = old_bg;501 }502 flash(this, arguments[0]);503 JS504 self505 end506 # @api private507 def reload508 if @allow_reload509 begin510 reloaded = @query.resolve_for(query_scope.reload)&.first511 @base = reloaded.base if reloaded512 rescue StandardError => e513 raise e unless catch_error?(e)514 end515 end516 self517 end518 ##519 #520 # A human-readable representation of the element.521 #522 # @return [String] A string representation523 def inspect524 %(#<Capybara::Node::Element tag="#{base.tag_name}" path="#{base.path}">)525 rescue NotSupportedByDriverError526 %(#<Capybara::Node::Element tag="#{base.tag_name}">)527 rescue *session.driver.invalid_element_errors528 %(Obsolete #<Capybara::Node::Element>)529 end530 # @api private531 def initial_cache532 base.respond_to?(:initial_cache) ? base.initial_cache : {}533 end534 STYLE_SCRIPT = <<~JS535 (function(){536 var s = window.getComputedStyle(this);537 var result = {};538 for (var i = arguments.length; i--; ) {539 var property_name = arguments[i];540 result[property_name] = s.getPropertyValue(property_name);541 }542 return result;543 }).apply(this, arguments)544 JS545 private546 def perform_click_action(keys, wait: nil, **options)547 raise ArgumentError, 'You must specify both x: and y: for a click offset' if nil ^ options[:x] ^ options[:y]548 options[:offset] ||= :center if session_options.w3c_click_offset549 synchronize(wait) { yield keys, options }550 self551 end552 end553 end554end...

Full Screen

Full Screen

perform_click_action

Using AI Code Generation

copy

Full Screen

1 def click_button(name)2 Capybara::Node::Element.new(page, page.find_button(name)).perform_click_action3 def click_button(name)4 Capybara::Node::Element.new(page, page.find_button(name)).perform_click_action5I am sending you the codedump of How to override click() method of Capybara::Node::Element class in Ruby that you can see here: https://codedump.io/share/3bT0DnTcFVWJ/1

Full Screen

Full Screen

perform_click_action

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', :with => 'Hello World')3click_button('Google Search')

Full Screen

Full Screen

perform_click_action

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', :with => 'Hello World')3click_button('Google Search')

Full Screen

Full Screen

perform_click_action

Using AI Code Generation

copy

Full Screen

1visit('/')2find(:css, 'input[name="q"]').set('hello')3perform_click_action(find(:css, 'input[name="btnK"]'))

Full Screen

Full Screen

perform_click_action

Using AI Code Generation

copy

Full Screen

1 visit('/')2 def search_for(search_term)3 fill_in('q', :with => search_term)4GoogleHomePage.new.search_for('Ruby')5 visit('/')6 def search_for(search_term)7 fill_in('q', :with => search_term)8GoogleHomePage.new.search_for('Ruby')9 visit('/')10 def search_for(search_term)11 fill_in('q', :with => search_term)12GoogleHomePage.new.search_for('Ruby')

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