Best Capybara code snippet using Capybara.escape
selector_query.rb
Source:selector_query.rb
...45 regexp = if options[:text].is_a?(Regexp)46 options[:text]47 else48 if exact_text == true49 /\A#{Regexp.escape(options[:text].to_s)}\z/50 else51 Regexp.escape(options[:text].to_s)52 end53 end54 text_visible = visible55 text_visible = :all if text_visible == :hidden56 return false if not node.text(text_visible).match(regexp)57 end58 if exact_text.is_a?(String)59 regexp = /\A#{Regexp.escape(options[:exact_text])}\z/60 text_visible = visible61 text_visible = :all if text_visible == :hidden62 return false if not node.text(text_visible).match(regexp)63 end64 case visible65 when :visible then return false unless node.visible?66 when :hidden then return false if node.visible?67 end68 res = query_filters.all? do |name, filter|69 if options.has_key?(name)70 filter.matches?(node, options[name])71 elsif filter.default?72 filter.matches?(node, filter.default)73 else74 true75 end76 end77 res &&= Capybara.using_wait_time(0){ @filter_block.call(node)} unless @filter_block.nil?78 res79 end80 def visible81 case (vis = options.fetch(:visible){ @selector.default_visibility })82 when true then :visible83 when false then :all84 else vis85 end86 end87 def exact?88 return false if !supports_exact?89 options.fetch(:exact, Capybara.exact)90 end91 def match92 options.fetch(:match, Capybara.match)93 end94 def xpath(exact=nil)95 exact = self.exact? if exact.nil?96 expr = if @expression.respond_to?(:to_xpath) and exact97 @expression.to_xpath(:exact)98 else99 @expression.to_s100 end101 filtered_xpath(expr)102 end103 def css104 filtered_css(@expression)105 end106 # @api private107 def resolve_for(node, exact = nil)108 node.synchronize do109 children = if selector.format == :css110 node.find_css(self.css)111 else112 node.find_xpath(self.xpath(exact))113 end.map do |child|114 if node.is_a?(Capybara::Node::Base)115 Capybara::Node::Element.new(node.session, child, node, self)116 else117 Capybara::Node::Simple.new(child)118 end119 end120 Capybara::Result.new(children, self)121 end122 end123 # @api private124 def supports_exact?125 @expression.respond_to? :to_xpath126 end127 private128 def valid_keys129 VALID_KEYS + custom_keys130 end131 def query_filters132 if options.has_key?(:filter_set)133 Capybara::Selector::FilterSet.all[options[:filter_set]].filters134 else135 @selector.custom_filters136 end137 end138 def custom_keys139 @custom_keys ||= query_filters.keys + @selector.expression_filters140 end141 def assert_valid_keys142 super143 unless VALID_MATCH.include?(match)144 raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(", ")}"145 end146 end147 def filtered_xpath(expr)148 if options.has_key?(:id) || options.has_key?(:class)149 expr = "(#{expr})"150 expr = "#{expr}[#{XPath.attr(:id) == options[:id]}]" if options.has_key?(:id) && !custom_keys.include?(:id)151 if options.has_key?(:class) && !custom_keys.include?(:class)152 class_xpath = Array(options[:class]).map do |klass|153 "contains(concat(' ',normalize-space(@class),' '),' #{klass} ')"154 end.join(" and ")155 expr = "#{expr}[#{class_xpath}]"156 end157 end158 expr159 end160 def filtered_css(expr)161 if options.has_key?(:id) || options.has_key?(:class)162 css_selectors = expr.split(',').map(&:rstrip)163 expr = css_selectors.map do |sel|164 sel += "##{Capybara::Selector::CSS.escape(options[:id])}" if options.has_key?(:id) && !custom_keys.include?(:id)165 sel += Array(options[:class]).map { |k| ".#{Capybara::Selector::CSS.escape(k)}"}.join if options.has_key?(:class) && !custom_keys.include?(:class)166 sel167 end.join(", ")168 end169 expr170 end171 def warn_exact_usage172 if options.has_key?(:exact) && !supports_exact?173 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."174 end175 end176 def exact_text177 options.fetch(:exact_text, Capybara.exact_text)178 end179 end...
olm_vn.rb
Source:olm_vn.rb
...18 visit question["question_url"]19 begin20 question_html = page.find(:xpath, "//div[@id='question']")["outerHTML"]21 string = "INSERT INTO `questions` (`group`, `category`, `title`, `question_html`, `source_url`)22 VALUES ('#{con.escape(question["group"])}','#{con.escape(question["category"])}','#{con.escape(question["question_title"])}','#{con.escape(question_html)}','#{con.escape(question["question_url"])}')"23 con.query(string)24 rescue25 end26 end27end...
escape
Using AI Code Generation
1When(/^I search for the text "(.*?)"$/) do |arg1|2 visit('/')3 fill_in('q', :with => arg1)4 click_button('Google Search')5Then(/^I should see the text "(.*?)"$/) do |arg1|6 page.has_content?(arg1)7When(/^I search for the text "(.*?)"$/) do |arg1|8 visit('/')9 fill_in('q', :with => arg1)10 click_button('Google Search')11Then(/^I should see the text "(.*?)"$/) do |arg1|12 page.has_content?(arg1)13When(/^I search for the text "(.*?)"$/) do |arg1|14 visit('/')15 fill_in('q', :with => arg1)16 click_button('Google Search')17Then(/^I should see the text "(.*?)"$/) do |arg1|18 page.has_content?(arg1)
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!!