Best Site_prism code snippet using SitePrism.create
dsl.rb
Source:dsl.rb
...168 }169 end170 def build(type, name, *find_args)171 if find_args.empty?172 create_error_method(name)173 else174 map_item(type, name)175 yield176 end177 add_helper_methods(name, *find_args)178 end179 def map_item(type, name)180 old_mapped_items << { type => name }181 new_mapped_items[type] << name.to_sym182 end183 def add_helper_methods(name, *find_args)184 create_existence_checker(name, *find_args)185 create_nonexistence_checker(name, *find_args)186 SitePrism::RspecMatchers.new(name)._create_rspec_existence_matchers if defined?(RSpec)187 create_visibility_waiter(name, *find_args)188 create_invisibility_waiter(name, *find_args)189 end190 def create_helper_method(proposed_method_name, *find_args)191 if find_args.empty?192 create_error_method(proposed_method_name)193 else194 yield195 end196 end197 def create_existence_checker(element_name, *find_args)198 method_name = "has_#{element_name}?"199 create_helper_method(method_name, *find_args) do200 define_method(method_name) do |*runtime_args|201 args = merge_args(find_args, runtime_args)202 element_exists?(*args)203 end204 end205 end206 def create_nonexistence_checker(element_name, *find_args)207 method_name = "has_no_#{element_name}?"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 element_does_not_exist?(*args)212 end213 end214 end215 def create_visibility_waiter(element_name, *find_args)216 method_name = "wait_until_#{element_name}_visible"217 create_helper_method(method_name, *find_args) do218 define_method(method_name) do |*runtime_args|219 args = merge_args(find_args, runtime_args, visible: true)220 return true if element_exists?(*args)221 raise SitePrism::ElementVisibilityTimeoutError222 end223 end224 end225 def create_invisibility_waiter(element_name, *find_args)226 method_name = "wait_until_#{element_name}_invisible"227 create_helper_method(method_name, *find_args) do228 define_method(method_name) do |*runtime_args|229 args = merge_args(find_args, runtime_args, visible: true)230 return true if element_does_not_exist?(*args)231 raise SitePrism::ElementInvisibilityTimeoutError232 end233 end234 end235 def create_error_method(name)236 SitePrism.logger.error("#{name} has come from an item with no locators.")237 define_method(name) { raise SitePrism::InvalidElementError }238 end239 def deduce_iframe_scope_find_args(args)240 warn_on_invalid_selector_input(args)241 case args[0]242 when Integer then [args[0]]243 when String then [:css, args[0]]244 else args245 end246 end247 def deduce_iframe_element_find_args(args)248 warn_on_invalid_selector_input(args)249 case args[0]...
element_container.rb
Source:element_container.rb
...40 def iframe(iframe_name, iframe_page_class, selector)41 element_selector = deduce_iframe_element_selector(selector)42 scope_selector = deduce_iframe_scope_selector(selector)43 add_to_mapped_items iframe_name44 create_existence_checker iframe_name, element_selector45 create_nonexistence_checker iframe_name, element_selector46 create_waiter iframe_name, element_selector47 define_method iframe_name do |&block|48 within_frame scope_selector do49 block.call iframe_page_class.new50 end51 end52 end53 def add_to_mapped_items(item)54 @mapped_items ||= []55 @mapped_items << item.to_s56 end57 def raise_if_block(obj, name, has_block)58 return unless has_block59 raise SitePrism::UnsupportedBlock, "#{obj.class}##{name} does not accept blocks, did you mean to define a (i)frame?"60 end61 private62 def build(name, *find_args)63 if find_args.empty?64 create_no_selector name65 else66 add_to_mapped_items name67 yield68 end69 add_helper_methods name, *find_args70 end71 def add_helper_methods(name, *find_args)72 create_existence_checker name, *find_args73 create_nonexistence_checker name, *find_args74 create_waiter name, *find_args75 create_visibility_waiter name, *find_args76 create_invisibility_waiter name, *find_args77 end78 def create_helper_method(proposed_method_name, *find_args)79 if find_args.empty?80 create_no_selector proposed_method_name81 else82 yield83 end84 end85 def create_existence_checker(element_name, *find_args)86 method_name = "has_#{element_name}?"87 create_helper_method method_name, *find_args do88 define_method method_name do |*runtime_args|89 wait_time = SitePrism.use_implicit_waits ? Waiter.default_wait_time : 090 Capybara.using_wait_time wait_time do91 element_exists?(*find_args, *runtime_args)92 end93 end94 end95 end96 def create_nonexistence_checker(element_name, *find_args)97 method_name = "has_no_#{element_name}?"98 create_helper_method method_name, *find_args do99 define_method method_name do |*runtime_args|100 wait_time = SitePrism.use_implicit_waits ? Waiter.default_wait_time : 0101 Capybara.using_wait_time wait_time do102 element_does_not_exist?(*find_args, *runtime_args)103 end104 end105 end106 end107 def create_waiter(element_name, *find_args)108 method_name = "wait_for_#{element_name}"109 create_helper_method method_name, *find_args do110 define_method method_name do |timeout = nil, *runtime_args|111 timeout = timeout.nil? ? Waiter.default_wait_time : timeout112 Capybara.using_wait_time timeout do113 element_exists?(*find_args, *runtime_args)114 end115 end116 end117 end118 def create_visibility_waiter(element_name, *find_args)119 method_name = "wait_until_#{element_name}_visible"120 create_helper_method method_name, *find_args do121 define_method method_name do |timeout = Waiter.default_wait_time, *runtime_args|122 Timeout.timeout timeout, SitePrism::TimeOutWaitingForElementVisibility do123 Capybara.using_wait_time 0 do124 sleep 0.05 until element_exists?(*find_args, *runtime_args, visible: true)125 end126 end127 end128 end129 end130 def create_invisibility_waiter(element_name, *find_args)131 method_name = "wait_until_#{element_name}_invisible"132 create_helper_method method_name, *find_args do133 define_method method_name do |timeout = Waiter.default_wait_time, *runtime_args|134 Timeout.timeout timeout, SitePrism::TimeOutWaitingForElementInvisibility do135 Capybara.using_wait_time 0 do136 sleep 0.05 while element_exists?(*find_args, *runtime_args, visible: true)137 end138 end139 end140 end141 end142 def create_no_selector(method_name)143 define_method method_name do144 raise SitePrism::NoSelectorForElement.new, "#{self.class.name} => :#{method_name} needs a selector"145 end146 end147 def deduce_iframe_scope_selector(selector)148 selector.is_a?(Integer) ? selector : selector.split('#').last149 end150 def deduce_iframe_element_selector(selector)151 selector.is_a?(Integer) ? "iframe:nth-of-type(#{selector + 1})" : selector152 end153 def extract_section_options(args, &block)154 case155 when args.first.is_a?(Class)156 section_class = args.shift...
helpers.rb
Source:helpers.rb
...26 end27 end28 end29 def self.drag_and_drop(page, source, target)30 page.execute_script("function createEvent(typeOfEvent) {\n" +31 "var event =document.createEvent(\"CustomEvent\");\n" +32 "event.initCustomEvent(typeOfEvent,true, true, null);\n" +33 "event.dataTransfer = {\n" +"data: {},\n" +"setData: function (key, value) {\n" +"this.data[key] = value;\n" +"},\n" +34 "getData: function (key) {\n" +"return this.data[key];\n" +"}\n" +"};\n" +"return event;\n" +"}\n" +"\n" +35 "function dispatchEvent(element, event,transferData) {\n" +"if (transferData !== undefined) {\n" +36 "event.dataTransfer = transferData;\n" +"}\n" +"if (element.dispatchEvent) {\n" +37 "element.dispatchEvent(event);\n" +"} else if (element.fireEvent) {\n" +"element.fireEvent(\"on\" + event.type, event);\n" +"}\n" +"}\n" +38 "\n" +"function simulateHTML5DragAndDrop(element, destination) {\n" +"var dragStartEvent =createEvent('dragstart');\n" +39 "dispatchEvent(element, dragStartEvent);\n" +"var dropEvent = createEvent('drop');\n" +"dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n" +40 "var dragEndEvent = createEvent('dragend');\n" +"dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);\n" +"}\n" +"\n" +"var source = arguments[0];\n" +41 "var destination = arguments[1];\n" +"simulateHTML5DragAndDrop(source,destination);",42 source, target)43 rescue Exception => e44 puts "ERROR :" + e.to_s45 end46 def self.random_string(length = 10)47 chars = "ABCDEFGHJKLMNPQRSTUVWXYZABCDEFGHJKLMNPQRSTUVWXYZ"48 result = ""49 length.times { result << chars[rand(chars.size)] }50 result51 end52 def self.getUserLoginId(userType)53 $userConfig[userType.to_sym][:loginId]54 end...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!