How to use merge_args method of ClassMethods Package

Best Site_prism code snippet using ClassMethods.merge_args

dsl.rb

Source:dsl.rb Github

copy

Full Screen

...62 # meta-programmed method. This ensures that the Capybara query is correct.63 #64 # Accepts any combination of arguments sent at DSL definition or runtime65 # and combines them in such a way that Capybara can operate with them.66 def merge_args(find_args, runtime_args, visibility_args = {})67 find_args = find_args.dup68 runtime_args = runtime_args.dup69 options = visibility_args.dup70 SitePrism.logger.debug("Initial args: #{find_args}, #{runtime_args}.")71 recombine_args(find_args, runtime_args, options)72 return [*find_args, *runtime_args, {}] if options.empty?73 [*find_args, *runtime_args, options]74 end75 # Options re-combiner. This takes the original inputs and combines76 # them such that there is only one hash passed as a final argument77 # to Capybara.78 #79 # If the hash is empty, then the hash is omitted from the payload sent80 # to Capybara, and the find / runtime arguments are sent alone.81 #82 # NB: If the +wait+ key is present in the options hash, even as false or 0, It will83 # be set as the user-supplied value (So user error can be the cause for issues).84 def recombine_args(find_args, runtime_args, options)85 options.merge!(find_args.pop) if find_args.last.is_a? Hash86 options.merge!(runtime_args.pop) if runtime_args.last.is_a? Hash87 options[:wait] = Capybara.default_max_wait_time unless options.key?(:wait)88 end89 # SitePrism::DSL::ClassMethods90 # This exposes all of the DSL definitions users will use when generating91 # their POM classes.92 #93 # Many of these methods will be used in-line to allow users to generate a multitude of94 # methods and locators for finding elements / sections on a page or section of a page95 module ClassMethods96 attr_reader :expected_items97 # Creates an instance of a SitePrism Element - This will create several methods designed to98 # Locate the element -> @return [Capybara::Node::Element]99 # Check the elements presence or non-presence -> @return [Boolean]100 # Wait for the elements to be present or not -> @return [TrueClass, SitePrism::Error]101 # Validate certain properties about the element102 def element(name, *find_args)103 SitePrism::Deprecator.deprecate('Passing a block to :element') if block_given?104 build(:element, name, *find_args) do105 define_method(name) do |*runtime_args, &element_block|106 warn_if_dsl_collision(self, name)107 raise_if_block(self, name, !element_block.nil?, :element)108 _find(*merge_args(find_args, runtime_args))109 end110 end111 end112 # Creates a enumerable instance of a SitePrism Element - This will create several methods designed to113 # Locate the enumerable element -> @return [Capybara::Result]114 # Check the elements presence or non-presence -> @return [Boolean]115 # Wait for the elements to be present or not -> @return [TrueClass, SitePrism::Error]116 # Validate certain properties about the elements117 def elements(name, *find_args)118 SitePrism::Deprecator.deprecate('Passing a block to :elements') if block_given?119 build(:elements, name, *find_args) do120 define_method(name) do |*runtime_args, &element_block|121 warn_if_dsl_collision(self, name)122 raise_if_block(self, name, !element_block.nil?, :elements)123 _all(*merge_args(find_args, runtime_args))124 end125 end126 end127 # Sets the `expected_items` iVar on a class. This property is used in conjunction with128 # `all_there?` to provide a way of granularising the check made to only interrogate a sub-set129 # of DSL defined items130 def expected_elements(*elements)131 @expected_items = elements132 end133 # Creates an instance of a SitePrism Section - This will create several methods designed to134 # Locate the section -> @return [SitePrism::Section]135 # Check the section presence or non-presence -> @return [Boolean]136 # Wait for the section to be present or not -> @return [TrueClass, SitePrism::Error]137 # Validate certain properties about the section138 def section(name, *args, &block)139 section_class, find_args = extract_section_options(args, &block)140 build(:section, name, *find_args) do141 define_method(name) do |*runtime_args, &runtime_block|142 warn_if_dsl_collision(self, name)143 section_element = _find(*merge_args(find_args, runtime_args))144 section_class.new(self, section_element, &runtime_block)145 end146 end147 end148 # Creates an enumerable instance of a SitePrism Section - This will create several methods designed to149 # Locate the sections -> @return [Array]150 # Check the sections presence or non-presence -> @return [Boolean]151 # Wait for the sections to be present or not -> @return [TrueClass, SitePrism::Error]152 # Validate certain properties about the section153 def sections(name, *args, &block)154 section_class, find_args = extract_section_options(args, &block)155 build(:sections, name, *find_args) do156 define_method(name) do |*runtime_args, &element_block|157 raise_if_block(self, name, !element_block.nil?, :sections)158 _all(*merge_args(find_args, runtime_args)).map do |element|159 section_class.new(self, element)160 end161 end162 end163 end164 def iframe(name, klass, *args)165 SitePrism.logger.debug('Block passed into iFrame construct at build time') if block_given?166 element_find_args = deduce_iframe_element_find_args(args)167 scope_find_args = deduce_iframe_scope_find_args(args)168 build(:iframe, name, *element_find_args) do169 define_method(name) do |&block|170 raise MissingBlockError unless block171 within_frame(*scope_find_args) { block.call(klass.new) }172 end173 end174 end175 def mapped_items(legacy: true)176 return old_mapped_items if legacy177 new_mapped_items178 end179 private180 def old_mapped_items181 SitePrism::Deprecator.soft_deprecate(182 '.mapped_items on a class',183 'To allow easier recursion through the items in conjunction with #all_there?',184 '.mapped_items(legacy: false)'185 )186 @old_mapped_items ||= []187 end188 def new_mapped_items189 @new_mapped_items ||= { element: [], elements: [], section: [], sections: [], iframe: [] }190 end191 def build(type, name, *find_args)192 if find_args.empty?193 create_error_method(name)194 else195 map_item(type, name)196 yield197 end198 add_helper_methods(name, *find_args)199 end200 def map_item(type, name)201 old_mapped_items << { type => name }202 new_mapped_items[type] << name.to_sym203 end204 def add_helper_methods(name, *find_args)205 create_existence_checker(name, *find_args)206 create_nonexistence_checker(name, *find_args)207 SitePrism::RspecMatchers.new(name)._create_rspec_existence_matchers if defined?(RSpec)208 create_visibility_waiter(name, *find_args)209 create_invisibility_waiter(name, *find_args)210 end211 def create_helper_method(proposed_method_name, *find_args)212 return create_error_method(proposed_method_name) if find_args.empty?213 yield214 end215 def create_existence_checker(element_name, *find_args)216 method_name = "has_#{element_name}?"217 create_helper_method(method_name, *find_args) do218 define_method(method_name) do |*runtime_args|219 args = merge_args(find_args, runtime_args)220 element_exists?(*args)221 end222 end223 end224 def create_nonexistence_checker(element_name, *find_args)225 method_name = "has_no_#{element_name}?"226 create_helper_method(method_name, *find_args) do227 define_method(method_name) do |*runtime_args|228 args = merge_args(find_args, runtime_args)229 element_does_not_exist?(*args)230 end231 end232 end233 def create_visibility_waiter(element_name, *find_args)234 method_name = "wait_until_#{element_name}_visible"235 create_helper_method(method_name, *find_args) do236 define_method(method_name) do |*runtime_args|237 args = merge_args(find_args, runtime_args, visible: true)238 return true if element_exists?(*args)239 raise SitePrism::ElementVisibilityTimeoutError240 end241 end242 end243 def create_invisibility_waiter(element_name, *find_args)244 method_name = "wait_until_#{element_name}_invisible"245 create_helper_method(method_name, *find_args) do246 define_method(method_name) do |*runtime_args|247 args = merge_args(find_args, runtime_args, visible: true)248 return true if element_does_not_exist?(*args)249 raise SitePrism::ElementInvisibilityTimeoutError250 end251 end252 end253 def create_error_method(name)254 SitePrism.logger.error("#{name} has come from an item with no locators.")255 SitePrism::Deprecator.soft_deprecate(256 'DSL definition with no find_args',257 'All DSL elements should have find_args'258 )259 define_method(name) { raise SitePrism::InvalidElementError }260 end261 def deduce_iframe_scope_find_args(args)...

Full Screen

Full Screen

element_container.rb

Source:element_container.rb Github

copy

Full Screen

...14 # Options re-combiner. This takes the original inputs and combines15 # them such that there is only one hash passed as a final argument16 # to Appium.17 #18 def merge_args(find_args, runtime_args = {})19 find_args = find_args.dup.flatten20 runtime_args = runtime_args.dup21 [*find_args, *runtime_args]22 end23 module ClassMethods24 attr_reader :mapped_items25 ##26 #27 # Declare an element with name and args to find it28 #29 # element :email, :accessibility_id, 'email_text_field'30 #31 # @param name Element name32 # @param *find_args An array contain information to find the element. It contains locator strategy and search target33 # http://appium.io/docs/en/commands/element/find-element/34 #35 # Element doesn't support block so that will raise if pass a block when declare36 #37 def element(name, *find_args)38 build_element(name, *find_args) do39 define_method(name) do |*runtime_args, &block|40 raise_if_block(self, name, !block.nil?, :element)41 _find(*merge_args(find_args, runtime_args))42 end43 create_get_element_params(name, find_args)44 end45 end46 ##47 #48 # Declare an elements with name and args to find it49 #50 # elements :contact_cell, :accessibility_id, 'contact_cell'51 #52 # @param name Element name53 # @param *find_args An array contain information to find the elements. It contains locator strategy and search target54 # http://appium.io/docs/en/commands/element/find-element/55 #56 # Elements doesn't support block so that will raise if pass a block when declare57 #58 def elements(name, *find_args)59 build_elements(name, *find_args) do60 define_method(name) do |*runtime_args, &block|61 raise_if_block(self, name, !block.nil?, :elements)62 _all(*merge_args(find_args, runtime_args))63 end64 create_get_element_params(name, find_args)65 end66 end67 def section(name, *args, &block)68 section_class, find_args = extract_section_options(args, &block)69 build_element(name, *find_args) do70 define_method(name) do |*runtime_args|71 section_element = _find(*merge_args(find_args, runtime_args))72 section_class.new(self, section_element)73 end74 create_get_element_params(name, find_args)75 end76 end77 def sections(name, *args, &block)78 section_class, find_args = extract_section_options(args, &block)79 build_sections(section_class, name, *find_args) do80 define_method(name) do |*runtime_args, &block|81 raise_if_block(self, name, !block.nil?, :sections)82 _all(*merge_args(find_args, runtime_args)).map do |element|83 section_class.new(self, element)84 end85 end86 create_get_element_params(name, find_args)87 end88 end89 ##90 # Add item to @mapped_items array91 #92 # @param item Item need to add93 #94 def add_to_mapped_items(item)95 @mapped_items ||= []96 @mapped_items << item97 end98 private99 # Add item to @mapped_items or define method for element and section100 def build_element(name, *find_args)101 if find_args.empty?102 create_error_method(name)103 else104 add_to_mapped_items(name)105 yield106 end107 create_existence_checker(name, *find_args)108 create_nonexistence_checker(name, *find_args)109 create_enable_checker(name, *find_args)110 create_disable_checker(name, *find_args)111 end112 # Add item to @mapped_items or define method for elements113 def build_elements(name, *find_args)114 if find_args.empty?115 create_error_method(name)116 else117 add_to_mapped_items(name)118 yield119 end120 create_existence_checker(name, *find_args)121 create_nonexistence_checker(name, *find_args)122 create_get_all_elements(name, *find_args)123 end124 # Add item to @mapped_items or define method for elements125 def build_sections(section_class, name, *find_args)126 if find_args.empty?127 create_error_method(name)128 else129 add_to_mapped_items(name)130 yield131 end132 create_existence_checker(name, *find_args)133 create_nonexistence_checker(name, *find_args)134 create_get_all_sections(section_class, name, *find_args)135 end136 # Define method to notify that we can't find item without args137 def create_error_method(name)138 define_method(name) do139 raise Appom::InvalidElementError140 end141 end142 def create_helper_method(proposed_method_name, *find_args)143 if find_args.empty?144 create_error_method(proposed_method_name)145 else146 yield147 end148 end149 ##150 # Try to get all elements until not get empty array151 #152 def create_get_all_elements(element_name, *find_args)153 method_name = "get_all_#{element_name}"154 create_helper_method(method_name, *find_args) do155 define_method(method_name) do |*runtime_args|156 args = merge_args(find_args, runtime_args)157 wait_until_get_not_empty(*args)158 end159 end160 end161 ##162 # Try to get all sections until not get empty array163 #164 def create_get_all_sections(section_class, element_name, *find_args)165 method_name = "get_all_#{element_name}"166 create_helper_method(method_name, *find_args) do167 define_method(method_name) do |*runtime_args|168 args = merge_args(find_args, runtime_args)169 wait_until_get_not_empty(*args).map do |element|170 section_class.new(self, element)171 end172 end173 end174 end175 ##176 # Check element exist177 # We will try to find all elements with *find_args178 # Condition is pass when response is not empty179 #180 def create_existence_checker(element_name, *find_args)181 method_name = "has_#{element_name}"182 create_helper_method(method_name, *find_args) do183 define_method(method_name) do |*runtime_args|184 args = merge_args(find_args, runtime_args)185 wait_until('at least one element exists', *args)186 end187 end188 end189 ##190 # Check element non-existent191 # We will try to find all elements with *find_args192 # Condition is pass when response is empty193 #194 def create_nonexistence_checker(element_name, *find_args)195 method_name = "has_no_#{element_name}"196 create_helper_method(method_name, *find_args) do197 define_method(method_name) do |*runtime_args|198 args = merge_args(find_args, runtime_args)199 wait_until('no element exists', *args)200 end201 end202 end203 ##204 # Wait until element will be enable205 #206 def create_enable_checker(element_name, *find_args)207 method_name = "#{element_name}_enable"208 create_helper_method(method_name, *find_args) do209 define_method(method_name) do |*runtime_args|210 args = merge_args(find_args, runtime_args)211 wait_until('element enable', *args)212 end213 end214 end215 ##216 # Wait until an element will be disable217 #218 def create_disable_checker(element_name, *find_args)219 method_name = "#{element_name}_disable"220 create_helper_method(method_name, *find_args) do221 define_method(method_name) do |*runtime_args|222 args = merge_args(find_args, runtime_args)223 wait_until('element disable', *args)224 end225 end226 end227 ##228 # Get parameter is passed when declared element229 #230 def create_get_element_params(element_name, *find_args)231 method_name = "#{element_name}_params"232 create_helper_method(method_name, *find_args) do233 define_method(method_name) do234 merge_args(find_args)235 end236 end237 end238 ##239 # Extract section options240 # @return section class name and the remaining parameters241 #242 def extract_section_options(args, &block)243 if args.first.is_a?(Class)244 klass = args.shift245 section_class = klass if klass.ancestors.include?(Appom::Section)246 end247 section_class = deduce_section_class(section_class, &block)248 arguments = deduce_search_arguments(section_class, args)...

Full Screen

Full Screen

merge_args

Using AI Code Generation

copy

Full Screen

1 def initialize(args = {})2 merge_args(args)3 def merge_args(args)4b = B.new(name: 'John', age: 25)

Full Screen

Full Screen

merge_args

Using AI Code Generation

copy

Full Screen

1 def initialize(args = {})2 merge_args(args)3 def merge_args(args)4b = B.new(name: 'John', age: 25)5 def self.merge_args(*args)6puts Test.merge_args([1, 2, 3], [4, 5, 6])7putsTest.erg_as([1, 2, 3], [4, 5, 6], [7, 8, 9])

Full Screen

Full Screen

merge_args

Using AI Code Generation

copy

Full Screen

1 def self.included(base)2 def merge_args(arg1, arg2)3 {arg1 => arg2}.merge(arg1 => arg2)4puts MyClass.merge_args("a", "b")5puts MyClass2.merge_args("a", "b")

Full Screen

Full Screen

merge_args

Using AI Code Generation

copy

Full Screen

1 def self.included(base)2 base.extend(ClassMethods)3 def merge_args(*args)4 options = args.last.is_a?(Hash) ? args.pop : {}5 def initialize(*args)6 options = merge_args(*args)7 def self.included(base)8 base.extend(ClassMethods)9 def merge_args(*args)10 options = args.last.is_a?(Hash) ? args.pop : {}11 def initialize(*args)12 options = merge_args(*args)13 def self.included(base)14 base.extend(ClassMethods)15 def merge_args(*args)16 options = args.last.is_a?(Hash) ? args.pop : {}17 def initialize(*args)18 options = merge_args(*args)

Full Screen

Full Screen

merge_args

Using AI Code Generation

copy

Full Screen

1 def initialize(*args)2 @args = merge_args(args)3a = A.new(1,2,3)4 def initialize(*args)5 @args = merge_args(args)6a = A.new(1,2,3)7 def initialize(*args)8 @args = merge_args(args)9a = A.new(1,2,3)10 def initialize(*args)11 @args = merge_args(args)12a = A.new(1,2,3)13 def self.included(base)14 def merge_args(arg1, arg2)15 {arg1 => arg2}.merge(arg1.to_sym => arg2)16puts MyClass.merge_args("a", "b")17puts MyClass2.merge_args("a", "b")18 def self.included(base)19 def merge_args(arg1, arg2)20 {arg1.to_sym => arg2}.merge(arg1 => arg2)21puts MyClass.merge_args("a", "b")22puts MyClass2.merge_args("a", "b")

Full Screen

Full Screen

merge_args

Using AI Code Generation

copy

Full Screen

1 def self.included(base)2 base.extend(ClassMethods)3 def merge_args(*args)4 options = args.last.is_a?(Hash) ? args.pop : {}5 def initialize(*args)6 options = merge_args(*args)7 def self.included(base)8 base.extend(ClassMethods)9 def merge_args(*args)10 options = args.last.is_a?(Hash) ? args.pop : {}11 def initialize(*args)12 options = merge_args(*args)13 def self.included(base)14 base.extend(ClassMethods)15 def merge_args(*args)16 options = args.last.is_a?(Hash) ? args.pop : {}17 def initialize(*args)18 options = merge_args(*args)

Full Screen

Full Screen

merge_args

Using AI Code Generation

copy

Full Screen

1 def self.included(base)2 def merge_args(arg1, arg2)3 {arg1 => arg2}.merge(arg1 => arg2)4puts MyClass.merge_args("a", "b")5puts MyClass2.merge_args("a", "b")6 def self.included(base)7 def merge_args(arg1, arg2)8 {arg1 => arg2}.merge(arg1.to_sym => arg2)9puts MyClass.merge_args("a", "b")10puts MyClass2.merge_args("a", "b")11 def self.included(base)12 def merge_args(arg1, arg2)13 {arg1.to_sym => arg2}.merge(arg1 => arg2)14puts MyClass.merge_args("a", "b")15puts MyClass2.merge_args("a", "b")

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