How to use capybara_context method of CapybaraContextHolder Package

Best Howitzer_ruby code snippet using CapybaraContextHolder.capybara_context

element_dsl.rb

Source:element_dsl.rb Github

copy

Full Screen

1require 'howitzer/web/capybara_context_holder'2module Howitzer3 module Web4 # This module combines element dsl methods5 module ElementDsl6 # This module holds element helper methods7 module Helpers8 private9 def lambda_args(*args, **keyword_args)10 {11 lambda_args: {12 args: args,13 keyword_args: keyword_args14 }15 }16 end17 end18 include CapybaraContextHolder19 include Helpers20 def self.included(base) # :nodoc:21 base.extend(ClassMethods)22 end23 def convert_arguments(args, options, block_args, block_options)24 conv_args = args.map { |el| el.is_a?(Proc) ? proc_to_selector(el, block_args, block_options) : el }25 args_options = pop_options_from_array(conv_args)26 block_args_options = pop_options_from_array(block_args)27 conv_options = [args_options, options, block_args_options, block_options].map do |el|28 el.transform_keys(&:to_sym)29 end.reduce(&:merge).except(:lambda_args)30 [conv_args, conv_options]31 end32 def proc_to_selector(proc, block_args, block_options)33 lambda_args = extract_lambda_args(block_args, block_options)34 if lambda_args35 if lambda_args[:keyword_args].present?36 proc.call(*lambda_args[:args], **lambda_args[:keyword_args])37 else38 proc.call(*lambda_args[:args])39 end40 else41 puts "WARNING! Passing lambda arguments with element options is deprecated.\n" \42 "Please use 'lambda_args' method, for example: foo_element(lambda_args(title: 'Example'), wait: 10)"43 proc.call(*block_args.shift(proc.arity))44 end45 end46 def extract_lambda_args(block_args, block_options)47 (block_args.first.is_a?(Hash) && block_args.first[:lambda_args]) || block_options[:lambda_args]48 end49 def pop_options_from_array(value)50 if value.last.is_a?(Hash) && !value.last.key?(:lambda_args)51 value.pop52 else53 {}54 end55 end56 # This module holds element dsl methods57 module ClassMethods58 include Helpers59 protected60 # Creates a group of methods to interact with described HTML element(s) on page61 # @note This method generates following dynamic methods:62 #63 # <b><em>element_name</em>_element</b> - equals capybara #find(...) method64 #65 # <b><em>element_name</em>_elements</b> - equals capybara #all(...) method66 #67 # <b><em>element_name</em>_elements.first</b> - equals capybara #first(...) method68 #69 # <b>wait_for_<em>element_name</em>_element</b> - equals capybara #find(...) method but returns nil70 #71 # <b>within_<em>element_name</em>_element</b> - equals capybara #within(...) method72 #73 # <b>has_<em>element_name</em>_element?</b> - equals capybara #has_selector(...) method74 #75 # <b>has_no_<em>element_name</em>_element?</b> - equals capybara #has_no_selector(...) method76 # @param name [Symbol, String] an unique element name77 # @param args [Array] original Capybara arguments. For details, see `Capybara::Node::Finders#all`.78 # @param options [Hash] original Capybara options. For details, see `Capybara::Node::Finders#all`.79 # @example Using in a page class80 # class HomePage < Howitzer::Web::Page81 # element :top_panel, '.top'82 # element :bottom_panel, '.bottom'83 # element :new_button, :xpath, ".//*[@name='New']"84 #85 # def press_top_new_button86 # within_top_panel_element do87 # new_button_element.click88 # end89 # end90 #91 # def press_bottom_new_button92 # within_bottom_panel_element do93 # new_button_element.click94 # end95 # end96 # end97 #98 # HomePage.on do99 # is_expected.to have_top_panel_element100 # press_top_new_element101 # is_expected.to have_no_new_button_element(match: :first)102 # end103 # @example Using in a section class104 # class MenuSection < Howitzer::Web::Section105 # me '.main-menu'106 # element :menu_item, '.item'107 #108 # def menu_items109 # menu_item_elements.map(&:text)110 # end111 # end112 # @raise [BadElementParamsError] if wrong element arguments113 # @!visibility public114 def element(name, *args, **options)115 validate_arguments!(args)116 define_element(name, args, options)117 define_elements(name, args, options)118 define_wait_for_element(name, args, options)119 define_within_element(name, args, options)120 define_has_element(name, args, options)121 define_has_no_element(name, args, options)122 end123 private124 def validate_arguments!(args)125 return unless args.map(&:class).count(Proc) > 1126 raise Howitzer::BadElementParamsError, 'Using more than 1 proc in arguments is forbidden'127 end128 def define_element(name, args, options)129 define_method("#{name}_element") do |*block_args, **block_options|130 conv_args, conv_options = convert_arguments(args, options, block_args, block_options)131 if conv_options.present?132 capybara_context.find(*conv_args, **conv_options)133 else134 capybara_context.find(*conv_args)135 end136 end137 private "#{name}_element"138 end139 def define_elements(name, args, options)140 define_method("#{name}_elements") do |*block_args, **block_options|141 conv_args, conv_options = convert_arguments(args, options, block_args, block_options)142 if conv_options.present?143 capybara_context.all(*conv_args, **conv_options)144 else145 capybara_context.all(*conv_args)146 end147 end148 private "#{name}_elements"149 end150 def define_wait_for_element(name, args, options)151 define_method("wait_for_#{name}_element") do |*block_args, **block_options|152 conv_args, conv_options = convert_arguments(args, options, block_args, block_options)153 if conv_options.present?154 capybara_context.find(*conv_args, **conv_options)155 else156 capybara_context.find(*conv_args)157 end158 return nil159 end160 private "wait_for_#{name}_element"161 end162 def define_within_element(name, args, options)163 define_method("within_#{name}_element") do |*block_args, **block_options, &block|164 conv_args, conv_options = convert_arguments(args, options, block_args, block_options)165 new_scope = if conv_options.present?166 capybara_context.find(*conv_args, **conv_options)167 else168 capybara_context.find(*conv_args)169 end170 begin171 capybara_scopes.push(new_scope)172 block.call173 ensure174 capybara_scopes.pop175 end176 end177 end178 def define_has_element(name, args, options)179 define_method("has_#{name}_element?") do |*block_args, **block_options|180 conv_args, conv_options = convert_arguments(args, options, block_args, block_options)181 if conv_options.present?182 capybara_context.has_selector?(*conv_args, **conv_options)183 else184 capybara_context.has_selector?(*conv_args)185 end186 end187 end188 def define_has_no_element(name, args, options)189 define_method("has_no_#{name}_element?") do |*block_args, **block_options|190 conv_args, conv_options = convert_arguments(args, options, block_args, block_options)191 if conv_options.present?192 capybara_context.has_no_selector?(*conv_args, **conv_options)193 else194 capybara_context.has_no_selector?(*conv_args)195 end196 end197 end198 end199 end200 end201end...

Full Screen

Full Screen

iframe_dsl.rb

Source:iframe_dsl.rb Github

copy

Full Screen

1require 'howitzer/web/capybara_context_holder'2module Howitzer3 module Web4 # This module combines iframe dsl methods5 module IframeDsl6 include CapybaraContextHolder7 def self.included(base) # :nodoc:8 base.extend(ClassMethods)9 end10 private11 def iframe_element_selector(args, params)12 args = convert_iframe_arguments(args, params)13 case args[0]14 when String, Hash15 [:frame, *args]16 when Integer17 idx = args.shift18 ["iframe:nth-of-type(#{idx + 1})", *args]19 else20 args21 end22 end23 def convert_iframe_arguments(args, params)24 new_args = args.deep_dup25 hash = new_args.pop.transform_keys(&:to_sym).merge(params.transform_keys(&:to_sym)) if new_args.last.is_a?(Hash)26 new_args << hash if hash.present?27 new_args28 end29 # This module holds frame dsl class methods30 module ClassMethods31 protected32 # Creates a group of methods to interact with described HTML frame on page33 # @note This method generates following dynamic methods:34 #35 # <b><em>frame_name</em>_iframe</b> - equals capybara #within_frame(...) method36 #37 # <b>has_<em>frame_name</em>_iframe?</b> - equals capybara #has_selector(...) method38 #39 # <b>has_no_<em>frame_name</em>_iframe?</b> - equals capybara #has_no_selector(...) method40 # @param name [Symbol, String] an unique iframe name41 # @param args [Array] original Capybara arguments. For details, see `Capybara::Session#within_frame`.42 # @raise [NameError] if page class can not be found43 # @example Using in a page class44 # class FbPage < Howitzer::Web::Page45 # element :like, :xpath, ".//*[text()='Like']"46 # def like47 # like_element.click48 # end49 #50 # def liked?51 # #some code here52 # end53 # end54 #55 # module Utils56 # class GroupFbPage < Howitzer::Web::Page57 # end58 # end59 #60 # class HomePage < Howitzer::Web::Page61 # iframe :fb, 162 #63 # # frame with explicit class declaration64 # # iframe :fb, FbPage, 165 #66 # # frame with namespace67 # iframe :utils_group_fb68 # end69 #70 # HomePage.on do71 # fb_iframe do |frame|72 # frame.like73 # expect(frame).to be_liked74 # end75 # end76 # HomePage.on { is_expected.to have_fb_iframe }77 # @!visibility public78 def iframe(name, *args)79 raise ArgumentError, 'iframe selector arguments must be specified' if args.blank?80 klass = args.first.is_a?(Class) ? args.shift : find_matching_class(name)81 raise NameError, "class can not be found for #{name} iframe" if klass.blank?82 define_iframe(klass, name, args)83 define_has_iframe(name, args)84 define_has_no_iframe(name, args)85 end86 private87 def define_iframe(klass, name, args)88 define_method "#{name}_iframe" do |**params, &block|89 capybara_context.within_frame(*convert_iframe_arguments(args, params)) do90 klass.displayed?91 block.call klass.instance92 end93 end94 end95 def define_has_iframe(name, args)96 define_method("has_#{name}_iframe?") do |**params|97 capybara_context.has_selector?(*iframe_element_selector(args, params))98 end99 end100 def define_has_no_iframe(name, args)101 define_method("has_no_#{name}_iframe?") do |**params|102 capybara_context.has_no_selector?(*iframe_element_selector(args, params))103 end104 end105 def find_matching_class(name)106 Howitzer::Web::Page.descendants.select { |el| el.name.underscore.tr('/', '_') == "#{name}_page" }107 .max_by { |el| el.name.count('::') }108 end109 end110 end111 end112end...

Full Screen

Full Screen

capybara_context_holder.rb

Source:capybara_context_holder.rb Github

copy

Full Screen

2 module Web3 # This module mixin capybara context methods4 module CapybaraContextHolder5 # Returns capybara context. For example, capybara session, parent element, etc.6 def capybara_context7 capybara_scopes.last8 end9 private10 def capybara_scopes11 return super if defined?(super)12 raise NotImplementedError, "Please define 'capybara_scopes' method for class holder"13 end14 end15 end16end...

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1CapybaraContextHolder.capybara_context(driver: :selenium) do2CapybaraContextHolder.capybara_context(driver: :poltergeist) do3CapybaraContextHolder.capybara_context(driver: :selenium) do4CapybaraContextHolder.capybara_context(driver: :poltergeist) do5CapybaraContextHolder.capybara_context(browser:

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1CapybaraContextHolder.register_capybara_context(:default)2capybara_context = CapybaraContextHolder.capybara_context(:default)3capybara_context.visit('http://www.google.com')4capybara_context.fill_in('q', with: 'capybara')5capybara_context.click_button('Google Search')6capybara_context.click_link('Capybara')7capybara_context.save_screenshot('capybara.png')8capybara_context.save_page('capybara.html')9capybara_context.find_field('q')10capybara_context.find_button('Google Search')11capybara_context.find_link('Capybara')12capybara_context.find_fieldset('q')13capybara_context.find_legend('q')14capybara_context.find_table('q')15capybara_context.find_row('q')16capybara_context.find_cell('q')17capybara_context.find_checkbox('q')18capybara_context.find_radio_button('q')

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1When(/^I use capybara_context method to get the current Capybara context$/) do2 capybara_context.visit('http://google.com')3 capybara_context.fill_in('q', :with => 'Capybara')4 capybara_context.click_button('Google Search')5When(/^I use CapybaraContextHolder class to get the current Capybara context$/) do6 capybara_context.visit('http://google.com')7 capybara_context.fill_in('q', :with => 'Capybara')8 capybara_context.click_button('Google Search')

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1 Capybara::Selenium::Driver.new(app, :browser => :firefox)2 Capybara::Session.new(:selenium)3 Capybara::Selenium::Driver.new(app, :browser => :firefox)4 Capybara::Session.new(:selenium)

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1capybara_context.visit_page('http://www.google.com')2capybara_context.find_link('Images')3capybara_context.click_link('Images')4capybara_context.visit_page('http://www.google.com')5capybara_context.find_link('Images')

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1 def initialize(driver)2 def set_driver(driver)3 def set_session(session)4 @session = Capybara::Session.new(@driver)5 def set_context(context)6capybara_context = CapybaraContext.new(:selenium)7capybara_context.set_driver(:selenium)8capybara_context_holder.set_context(capybara_context)

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1CapybaraContextHolder.capybara_context(driver: :selenium) do2CapybaraContextHolder.capybara_context(driver: :poltergeist) do3CapybaraContextHolder.capybara_context(driver: :selenium) do4CapybaraContextHolder.capybara_context(driver: :poltergeist) do5CapybaraContextHolder.capybara_context(browser:

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1CapybaraContextHolder.register_capybara_context(:default)2capybara_context = CapybaraContextHolder.capybara_context(:default)3capybara_context.visit('http://www.google.com')4capybara_context.fill_in('q', with: 'capybara')5capybara_context.click_button('Google Search')6capybara_context.click_link('Capybara')7capybara_context.save_screenshot('capybara.png')8capybara_context.save_page('capybara.html')9capybara_context.find_field('q')10capybara_context.find_button('Google Search')11capybara_context.find_link('Capybara')12capybara_context.find_fieldset('q')13capybara_context.find_legend('q')14capybara_context.find_table('q')15capybara_context.find_row('q')16capybara_context.find_cell('q')17capybara_context.find_checkbox('q')18capybara_context.find_radio_button('q')

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1 Capybara::Selenium::Driver.new(app, :browser => :firefox)2 Capybara::Session.new(:selenium)3 Capybara::Selenium::Driver.new(app, :browser => :firefox)4 Capybara::Session.new(:selenium)

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1CapybaraContextHolder.register_capybara_context(:default)2capybara_context = CapybaraContextHolder.capybara_context(:default)3capybara_context.visit('http://www.google.com')4capybara_context.fill_in('q', with: 'capybara')5capybara_context.click_button('Google Search')6capybara_context.click_link('Capybara')7capybara_context.save_screenshot('capybara.png')8capybara_context.save_page('capybara.html')9capybara_context.find_field('q')10capybara_context.find_button('Google Search')11capybara_context.find_link('Capybara')12capybara_context.find_fieldset('q')13capybara_context.find_legend('q')14capybara_context.find_table('q')15capybara_context.find_row('q')16capybara_context.find_cell('q')17capybara_context.find_checkbox('q')18capybara_context.find_radio_button('q')

Full Screen

Full Screen

capybara_context

Using AI Code Generation

copy

Full Screen

1 Capybara::Selenium::Driver.new(app, :browser => :firefox)2 Capybara::Session.new(:selenium)3 Capybara::Selenium::Driver.new(app, :browser => :firefox)4 Capybara::Session.new(:selenium)

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 Howitzer_ruby 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