Best Capybara code snippet using Queries.session_options
finders.rb
Source:finders.rb
...28 # @raise [Capybara::ElementNotFound] If the element can't be found before time expires29 #30 def find(*args, &optional_filter_block)31 if args.last.is_a? Hash32 args.last[:session_options] = session_options33 else34 args.push(session_options: session_options)35 end36 synced_resolve Capybara::Queries::SelectorQuery.new(*args, &optional_filter_block)37 end38 ##39 #40 # Find an {Capybara::Node::Element} based on the given arguments that is also an ancestor of the element called on. +ancestor+ will raise an error if the element41 # is not found.42 #43 # +ancestor+ takes the same options as +find+.44 #45 # element.ancestor('#foo').find('.bar')46 # element.ancestor(:xpath, './/div[contains(., "bar")]')47 # element.ancestor('ul', text: 'Quox').click_link('Delete')48 #49 # @param (see Capybara::Node::Finders#find)50 #51 # @!macro waiting_behavior52 #53 # @option options [Boolean] match The matching strategy to use.54 #55 # @return [Capybara::Node::Element] The found element56 # @raise [Capybara::ElementNotFound] If the element can't be found before time expires57 #58 def ancestor(*args, &optional_filter_block)59 if args.last.is_a? Hash60 args.last[:session_options] = session_options61 else62 args.push(session_options: session_options)63 end64 synced_resolve Capybara::Queries::AncestorQuery.new(*args, &optional_filter_block)65 end66 ##67 #68 # Find an {Capybara::Node::Element} based on the given arguments that is also a sibling of the element called on. +sibling+ will raise an error if the element69 # is not found.70 #71 #72 # +sibling+ takes the same options as +find+.73 #74 # element.sibling('#foo').find('.bar')75 # element.sibling(:xpath, './/div[contains(., "bar")]')76 # element.sibling('ul', text: 'Quox').click_link('Delete')77 #78 # @param (see Capybara::Node::Finders#find)79 #80 # @macro waiting_behavior81 #82 # @option options [Boolean] match The matching strategy to use.83 #84 # @return [Capybara::Node::Element] The found element85 # @raise [Capybara::ElementNotFound] If the element can't be found before time expires86 #87 def sibling(*args, &optional_filter_block)88 if args.last.is_a? Hash89 args.last[:session_options] = session_options90 else91 args.push(session_options: session_options)92 end93 synced_resolve Capybara::Queries::SiblingQuery.new(*args, &optional_filter_block)94 end95 ##96 #97 # Find a form field on the page. The field can be found by its name, id or label text.98 #99 # @overload find_field([locator], options={})100 # @param [String] locator name, id, placeholder or text of associated label element101 #102 # @macro waiting_behavior103 #104 #105 # @option options [Boolean] checked Match checked field?106 # @option options [Boolean] unchecked Match unchecked field?107 # @option options [Boolean, Symbol] disabled (false) Match disabled field?108 # * true - only finds a disabled field109 # * false - only finds an enabled field110 # * :all - finds either an enabled or disabled field111 # @option options [Boolean] readonly Match readonly field?112 # @option options [String, Regexp] with Value of field to match on113 # @option options [String] type Type of field to match on114 # @option options [Boolean] multiple Match fields that can have multiple values?115 # @option options [String] id Match fields that match the id attribute116 # @option options [String] name Match fields that match the name attribute117 # @option options [String] placeholder Match fields that match the placeholder attribute118 # @option options [String, Array<String>] Match fields that match the class(es) passed119 # @return [Capybara::Node::Element] The found element120 #121 def find_field(locator=nil, options={}, &optional_filter_block)122 locator, options = nil, locator if locator.is_a? Hash123 find(:field, locator, options, &optional_filter_block)124 end125 alias_method :field_labeled, :find_field126 ##127 #128 # Find a link on the page. The link can be found by its id or text.129 #130 # @overload find_link([locator], options={})131 # @param [String] locator id, title, text, or alt of enclosed img element132 #133 # @macro waiting_behavior134 #135 # @option options [String,Regexp,nil] href Value to match against the links href, if nil finds link placeholders (<a> elements with no href attribute)136 # @option options [String] id Match links with the id provided137 # @option options [String] title Match links with the title provided138 # @option options [String] alt Match links with a contained img element whose alt matches139 # @option options [String, Array<String>] class Match links that match the class(es) provided140 # @return [Capybara::Node::Element] The found element141 #142 def find_link(locator=nil, options={}, &optional_filter_block)143 locator, options = nil, locator if locator.is_a? Hash144 find(:link, locator, options, &optional_filter_block)145 end146 ##147 #148 # Find a button on the page.149 # This can be any \<input> element of type submit, reset, image, button or it can be a150 # \<button> element. All buttons can be found by their id, value, or title. \<button> elements can also be found151 # by their text content, and image \<input> elements by their alt attribute152 #153 # @overload find_button([locator], options={})154 # @param [String] locator id, value, title, text content, alt of image155 #156 # @overload find_button(options={})157 #158 # @macro waiting_behavior159 #160 # @option options [Boolean, Symbol] disabled (false) Match disabled button?161 # * true - only finds a disabled button162 # * false - only finds an enabled button163 # * :all - finds either an enabled or disabled button164 # @option options [String] id Match buttons with the id provided165 # @option options [String] title Match buttons with the title provided166 # @option options [String] value Match buttons with the value provided167 # @option options [String, Array<String>] class Match buttons that match the class(es) provided168 # @return [Capybara::Node::Element] The found element169 #170 def find_button(locator=nil, options={}, &optional_filter_block)171 locator, options = nil, locator if locator.is_a? Hash172 find(:button, locator, options, &optional_filter_block)173 end174 ##175 #176 # Find a element on the page, given its id.177 #178 # @macro waiting_behavior179 #180 # @param [String] id id of element181 #182 # @return [Capybara::Node::Element] The found element183 #184 def find_by_id(id, options={}, &optional_filter_block)185 find(:id, id, options, &optional_filter_block)186 end187 ##188 # @!method all([kind = Capybara.default_selector], locator = nil, options = {})189 #190 # Find all elements on the page matching the given selector191 # and options.192 #193 # Both XPath and CSS expressions are supported, but Capybara194 # does not try to automatically distinguish between them. The195 # following statements are equivalent:196 #197 # page.all(:css, 'a#person_123')198 # page.all(:xpath, './/a[@id="person_123"]')199 #200 #201 # If the type of selector is left out, Capybara uses202 # {Capybara.default_selector}. It's set to :css by default.203 #204 # page.all("a#person_123")205 #206 # Capybara.default_selector = :xpath207 # page.all('.//a[@id="person_123"]')208 #209 # The set of found elements can further be restricted by specifying210 # options. It's possible to select elements by their text or visibility:211 #212 # page.all('a', text: 'Home')213 # page.all('#menu li', visible: true)214 #215 # By default if no elements are found, an empty array is returned;216 # however, expectations can be set on the number of elements to be found which217 # will trigger Capybara's waiting behavior for the expectations to match.The218 # expectations can be set using219 #220 # page.assert_selector('p#foo', count: 4)221 # page.assert_selector('p#foo', maximum: 10)222 # page.assert_selector('p#foo', minimum: 1)223 # page.assert_selector('p#foo', between: 1..10)224 #225 # See {Capybara::Helpers#matches_count?} for additional information about226 # count matching.227 #228 # @param [Symbol] kind Optional selector type (:css, :xpath, :field, etc.) - Defaults to Capybara.default_selector229 # @param [String] locator The selector230 # @option options [String, Regexp] text Only find elements which contain this text or match this regexp231 # @option options [String, Boolean] exact_text (Capybara.exact_text) When String the string the elements contained text must match exactly, when Boolean controls whether the :text option must match exactly232 # @option options [Boolean, Symbol] visible Only find elements with the specified visibility:233 # * true - only finds visible elements.234 # * false - finds invisible _and_ visible elements.235 # * :all - same as false; finds visible and invisible elements.236 # * :hidden - only finds invisible elements.237 # * :visible - same as true; only finds visible elements.238 # @option options [Integer] count Exact number of matches that are expected to be found239 # @option options [Integer] maximum Maximum number of matches that are expected to be found240 # @option options [Integer] minimum Minimum number of matches that are expected to be found241 # @option options [Range] between Number of matches found must be within the given range242 # @option options [Boolean] exact Control whether `is` expressions in the given XPath match exactly or partially243 # @option options [Integer] wait (Capybara.default_max_wait_time) The time to wait for element count expectations to become true244 # @overload all([kind = Capybara.default_selector], locator = nil, options = {})245 # @overload all([kind = Capybara.default_selector], locator = nil, options = {}, &filter_block)246 # @yieldparam element [Capybara::Node::Element] The element being considered for inclusion in the results247 # @yieldreturn [Boolean] Should the element be considered in the results?248 # @return [Capybara::Result] A collection of found elements249 #250 def all(*args, &optional_filter_block)251 if args.last.is_a? Hash252 args.last[:session_options] = session_options253 else254 args.push(session_options: session_options)255 end256 query = Capybara::Queries::SelectorQuery.new(*args, &optional_filter_block)257 synchronize(query.wait) do258 result = query.resolve_for(self)259 raise Capybara::ExpectationNotMet, result.failure_message unless result.matches_count?260 result261 end262 end263 alias_method :find_all, :all264 ##265 #266 # Find the first element on the page matching the given selector267 # and options, or nil if no element matches. By default no waiting268 # behavior occurs, however if {Capybara.wait_on_first_by_default} is set to true269 # it will trigger Capybara's waiting behavior for a minimum of 1 matching element to be found and270 # return the first. Waiting behavior can also be triggered by passing in any of the count271 # expectation options.272 #273 # @overload first([kind], locator, options)274 # @param [:css, :xpath] kind The type of selector275 # @param [String] locator The selector276 # @param [Hash] options Additional options; see {#all}277 # @return [Capybara::Node::Element] The found element or nil278 #279 def first(*args, &optional_filter_block)280 if session_options.wait_on_first_by_default281 options = if args.last.is_a?(Hash) then args.pop.dup else {} end282 args.push({minimum: 1}.merge(options))283 end284 all(*args, &optional_filter_block).first285 rescue Capybara::ExpectationNotMet286 nil287 end288 private289 def synced_resolve(query)290 synchronize(query.wait) do291 if (query.match == :smart or query.match == :prefer_exact)292 result = query.resolve_for(self, true)293 result = query.resolve_for(self, false) if result.empty? && query.supports_exact? && !query.exact?294 else...
selector_query.rb
Source:selector_query.rb
...6 VALID_KEYS = COUNT_KEYS + [:text, :id, :class, :visible, :exact, :exact_text, :match, :wait, :filter_set]7 VALID_MATCH = [:first, :smart, :prefer_exact, :one]8 def initialize(*args, &filter_block)9 @options = if args.last.is_a?(Hash) then args.pop.dup else {} end10 self.session_options = @options.delete(:session_options)11 @filter_block = filter_block12 if args[0].is_a?(Symbol)13 @selector = Selector.all.fetch(args.shift) do |selector_type|14 raise ArgumentError, "Unknown selector type (:#{selector_type})"15 nil16 end17 @locator = args.shift18 else19 @selector = Selector.all.values.find { |s| s.match?(args[0]) }20 @locator = args.shift21 end22 @selector ||= Selector.all[session_options.default_selector]23 warn "Unused parameters passed to #{self.class.name} : #{args.to_s}" unless args.empty?24 # for compatibility with Capybara 2.025 if session_options.exact_options and @selector == Selector.all[:option]26 @options[:exact] = true27 end28 @expression = @selector.call(@locator, @options.merge(enable_aria_label: session_options.enable_aria_label))29 warn_exact_usage30 assert_valid_keys31 end32 def name; selector.name; end33 def label; selector.label or selector.name; end34 def description35 @description = String.new("#{label} #{locator.inspect}")36 @description << " with#{" exact" if exact_text == true} text #{options[:text].inspect}" if options[:text]37 @description << " with exact text #{options[:exact_text]}" if options[:exact_text].is_a?(String)38 @description << " with id #{options[:id]}" if options[:id]39 @description << " with classes #{Array(options[:class]).join(',')}]" if options[:class]40 @description << selector.description(options)41 @description << " that also matches the custom filter block" if @filter_block42 @description43 end44 def matches_filters?(node)45 if options[:text]46 regexp = if options[:text].is_a?(Regexp)47 options[:text]48 else49 if exact_text == true50 /\A#{Regexp.escape(options[:text].to_s)}\z/51 else52 Regexp.escape(options[:text].to_s)53 end54 end55 text_visible = visible56 text_visible = :all if text_visible == :hidden57 return false if not node.text(text_visible).match(regexp)58 end59 if exact_text.is_a?(String)60 regexp = /\A#{Regexp.escape(options[:exact_text])}\z/61 text_visible = visible62 text_visible = :all if text_visible == :hidden63 return false if not node.text(text_visible).match(regexp)64 end65 case visible66 when :visible then return false unless node.visible?67 when :hidden then return false if node.visible?68 end69 res = query_filters.all? do |name, filter|70 if options.has_key?(name)71 filter.matches?(node, options[name])72 elsif filter.default?73 filter.matches?(node, filter.default)74 else75 true76 end77 end78 res &&= node.session.using_wait_time(0){ @filter_block.call(node)} unless @filter_block.nil?79 res80 end81 def visible82 case (vis = options.fetch(:visible){ @selector.default_visibility(session_options.ignore_hidden_elements) })83 when true then :visible84 when false then :all85 else vis86 end87 end88 def exact?89 return false if !supports_exact?90 options.fetch(:exact, session_options.exact)91 end92 def match93 options.fetch(:match, session_options.match)94 end95 def xpath(exact=nil)96 exact = self.exact? if exact.nil?97 expr = if @expression.respond_to?(:to_xpath) and exact98 @expression.to_xpath(:exact)99 else100 @expression.to_s101 end102 filtered_xpath(expr)103 end104 def css105 filtered_css(@expression)106 end107 # @api private108 def resolve_for(node, exact = nil)109 node.synchronize do110 children = if selector.format == :css111 node.find_css(self.css)112 else113 node.find_xpath(self.xpath(exact))114 end.map do |child|115 if node.is_a?(Capybara::Node::Base)116 Capybara::Node::Element.new(node.session, child, node, self)117 else118 Capybara::Node::Simple.new(child)119 end120 end121 Capybara::Result.new(children, self)122 end123 end124 # @api private125 def supports_exact?126 @expression.respond_to? :to_xpath127 end128 private129 def valid_keys130 VALID_KEYS + custom_keys131 end132 def query_filters133 if options.has_key?(:filter_set)134 Capybara::Selector::FilterSet.all[options[:filter_set]].filters135 else136 @selector.custom_filters137 end138 end139 def custom_keys140 @custom_keys ||= query_filters.keys + @selector.expression_filters141 end142 def assert_valid_keys143 super144 unless VALID_MATCH.include?(match)145 raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(", ")}"146 end147 end148 def filtered_xpath(expr)149 if options.has_key?(:id) || options.has_key?(:class)150 expr = "(#{expr})"151 expr = "#{expr}[#{XPath.attr(:id) == options[:id]}]" if options.has_key?(:id) && !custom_keys.include?(:id)152 if options.has_key?(:class) && !custom_keys.include?(:class)153 class_xpath = Array(options[:class]).map do |klass|154 "contains(concat(' ',normalize-space(@class),' '),' #{klass} ')"155 end.join(" and ")156 expr = "#{expr}[#{class_xpath}]"157 end158 end159 expr160 end161 def filtered_css(expr)162 if options.has_key?(:id) || options.has_key?(:class)163 css_selectors = expr.split(',').map(&:rstrip)164 expr = css_selectors.map do |sel|165 sel += "##{Capybara::Selector::CSS.escape(options[:id])}" if options.has_key?(:id) && !custom_keys.include?(:id)166 sel += Array(options[:class]).map { |k| ".#{Capybara::Selector::CSS.escape(k)}"}.join if options.has_key?(:class) && !custom_keys.include?(:class)167 sel168 end.join(", ")169 end170 expr171 end172 def warn_exact_usage173 if options.has_key?(:exact) && !supports_exact?174 warn "The :exact option only has an effect on queries using the XPath#is method. Using it with the query \"#{expression.to_s}\" has no effect."175 end176 end177 def exact_text178 options.fetch(:exact_text, session_options.exact_text)179 end180 end181 end182end...
session_options
Using AI Code Generation
1 @session_options ||= {}2 @session_options ||= {}3 @session_options ||= {}4 @session_options ||= {}5 @session_options ||= {}6 @session_options ||= {}7 @session_options ||= {}8 @session_options ||= {}9 @session_options ||= {}10 @session_options ||= {}11 @session_options ||= {}12 @session_options ||= {}
session_options
Using AI Code Generation
1dbconfig = YAML::load(File.open('database.yml'))2ActiveRecord::Base.establish_connection(dbconfig)3 find(:first, :conditions => "session_id =
session_options
Using AI Code Generation
1 @session_options = { :table_name => "sessions" }2session = ActiveRecord::SessionStore::Session.create(:session_id => "1", :data => "data")3session = ActiveRecord::SessionStore::Session.find_by_session_id("1")4ActiveRecord::SessionStore::Session.session_options = { :table_name => "sessions" }5session = ActiveRecord::SessionStore::Session.create(:session_id => "1", :data => "data")6session = ActiveRecord::SessionStore::Session.find_by_session_id("1")7ActiveRecord::SessionStore::Session.session_options = { :table_name => "sessions" }8session = ActiveRecord::SessionStore::Session.create(:session_id => "1", :data => "data")9session = ActiveRecord::SessionStore::Session.find_by_session_id("1")10ActiveRecord::SessionStore::Session.session_options = { :table_name => "sessions" }11session = ActiveRecord::SessionStore::Session.create(:session_id => "1", :data => "data")12session = ActiveRecord::SessionStore::Session.find_by_session_id("1")
session_options
Using AI Code Generation
1queries.session_options(:cache => false)2queries.session_options(:cache => true)3queries.session_options(:cache => false)4queries.session_options(:cache => true)5queries.session_options(:cache => true)6queries.session_options(:cache => false)7queries.session_options(:cache => false)8queries.session_options(:cache => true)9queries.session_options(:cache => false)10queries.session_options(:cache => true)11queries.session_options(:cache => false)12queries.session_options(:cache => true)13queries.session_options(:cache => false)14queries.session_options(:cache => true)15queries.session_options(:cache => true)16queries.session_options(:cache => false)17queries.session_options(:cache => false)18queries.session_options(:cache => true)19queries.session_options(:cache => true)20queries.session_options(:cache => false)
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!!