Best Capybara code snippet using Capybara.find_by_attr
selector.rb
Source:selector.rb
...86 XPath.descendant(:img)[XPath.attr(:alt).is(locator)]]87 matchers << XPath.attr(:'aria-label').is(locator) if enable_aria_label88 xpath = xpath[matchers.reduce(:|)]89 end90 xpath = xpath[find_by_attr(:title, title)]91 xpath = xpath[XPath.descendant(:img)[XPath.attr(:alt) == alt]] if alt92 xpath93 end94 node_filter(:href) do |node, href|95 # If not a Regexp it's been handled in the main XPath96 href.is_a?(Regexp) ? node[:href].match(href) : true97 end98 describe do |**options|99 desc = +""100 desc << " with href #{options[:href].inspect}" if options[:href]101 desc << " with no href attribute" if options.fetch(:href, true).nil?102 end103end104Capybara.add_selector(:button) do105 xpath(:value, :title, :type) do |locator, enable_aria_label: false, **options|106 input_btn_xpath = XPath.descendant(:input)[XPath.attr(:type).one_of('submit', 'reset', 'image', 'button')]107 btn_xpath = XPath.descendant(:button)108 image_btn_xpath = XPath.descendant(:input)[XPath.attr(:type) == 'image']109 unless locator.nil?110 locator = locator.to_s111 locator_matches = XPath.attr(:id).equals(locator) | XPath.attr(:value).is(locator) | XPath.attr(:title).is(locator)112 locator_matches |= XPath.attr(:'aria-label').is(locator) if enable_aria_label113 input_btn_xpath = input_btn_xpath[locator_matches]114 btn_xpath = btn_xpath[locator_matches | XPath.string.n.is(locator) | XPath.descendant(:img)[XPath.attr(:alt).is(locator)]]115 alt_matches = XPath.attr(:alt).is(locator)116 alt_matches |= XPath.attr(:'aria-label').is(locator) if enable_aria_label117 image_btn_xpath = image_btn_xpath[alt_matches]118 end119 res_xpath = input_btn_xpath.union(btn_xpath).union(image_btn_xpath)120 res_xpath = expression_filters.keys.inject(res_xpath) { |memo, ef| memo[find_by_attr(ef, options[ef])] }121 res_xpath122 end123 node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }124 describe do |disabled: nil, **options|125 desc = +""126 desc << " that is disabled" if disabled == true127 desc << describe_all_expression_filters(options)128 desc129 end130end131Capybara.add_selector(:link_or_button) do132 label "link or button"133 xpath do |locator, **options|134 self.class.all.values_at(:link, :button).map { |selector| selector.xpath.call(locator, options) }.reduce(:union)135 end136 node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| node.tag_name == "a" || !(value ^ node.disabled?) }137 describe { |disabled: nil, **_options| " that is disabled" if disabled == true }138end139Capybara.add_selector(:fillable_field) do140 label "field"141 xpath do |locator, **options|142 xpath = XPath.descendant(:input, :textarea)[!XPath.attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')]143 locate_field(xpath, locator, options)144 end145 expression_filter(:type) do |expr, type|146 type = type.to_s147 if ['textarea'].include?(type)148 expr.self(type.to_sym)149 else150 expr[XPath.attr(:type) == type]151 end152 end153 filter_set(:_field, %i[disabled multiple name placeholder])154 node_filter(:with) do |node, with|155 with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s156 end157 describe do |options|158 desc = +""159 desc << describe_all_expression_filters(options)160 desc << " with value #{options[:with].to_s.inspect}" if options.key?(:with)161 desc162 end163end164Capybara.add_selector(:radio_button) do165 label "radio button"166 xpath do |locator, **options|167 xpath = XPath.descendant(:input)[XPath.attr(:type) == 'radio']168 locate_field(xpath, locator, options)169 end170 filter_set(:_field, %i[checked unchecked disabled name])171 node_filter(:option) { |node, value| node.value == value.to_s }172 describe do |option: nil, **options|173 desc = +""174 desc << " with value #{option.inspect}" if option175 desc << describe_all_expression_filters(options)176 desc177 end178end179Capybara.add_selector(:checkbox) do180 xpath do |locator, **options|181 xpath = XPath.descendant(:input)[XPath.attr(:type) == 'checkbox']182 locate_field(xpath, locator, options)183 end184 filter_set(:_field, %i[checked unchecked disabled name])185 node_filter(:option) { |node, value| node.value == value.to_s }186 describe do |option: nil, **options|187 desc = +""188 desc << " with value #{option.inspect}" if option189 desc << describe_all_expression_filters(options)190 desc191 end192end193Capybara.add_selector(:select) do194 label "select box"195 xpath do |locator, **options|196 xpath = XPath.descendant(:select)197 locate_field(xpath, locator, options)198 end199 filter_set(:_field, %i[disabled multiple name placeholder])200 node_filter(:options) do |node, options|201 actual = if node.visible?202 node.all(:xpath, './/option', wait: false).map(&:text)203 else204 node.all(:xpath, './/option', visible: false, wait: false).map { |option| option.text(:all) }205 end206 options.sort == actual.sort207 end208 expression_filter(:with_options) do |expr, options|209 options.inject(expr) do |xpath, option|210 xpath[Capybara::Selector.all[:option].call(option)]211 end212 end213 node_filter(:selected) do |node, selected|214 actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }215 Array(selected).sort == actual.sort216 end217 node_filter(:with_selected) do |node, selected|218 actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }219 (Array(selected) - actual).empty?220 end221 describe do |options: nil, with_options: nil, selected: nil, with_selected: nil, **opts|222 desc = +""223 desc << " with options #{options.inspect}" if options224 desc << " with at least options #{with_options.inspect}" if with_options225 desc << " with #{selected.inspect} selected" if selected226 desc << " with at least #{with_selected.inspect} selected" if with_selected227 desc << describe_all_expression_filters(opts)228 desc229 end230end231Capybara.add_selector(:datalist_input) do232 label "input box with datalist completion"233 xpath do |locator, **options|234 xpath = XPath.descendant(:input)[XPath.attr(:list)]235 locate_field(xpath, locator, options)236 end237 filter_set(:_field, %i[disabled name placeholder])238 node_filter(:options) do |node, options|239 actual = node.find("//datalist[@id=#{node[:list]}]", visible: :all).all(:datalist_option, wait: false).map(&:value)240 options.sort == actual.sort241 end242 expression_filter(:with_options) do |expr, options|243 options.inject(expr) do |xpath, option|244 xpath[XPath.attr(:list) == XPath.anywhere(:datalist)[Capybara::Selector.all[:datalist_option].call(option)].attr(:id)]245 end246 end247 describe do |options: nil, with_options: nil, **opts|248 desc = +""249 desc << " with options #{options.inspect}" if options250 desc << " with at least options #{with_options.inspect}" if with_options251 desc << describe_all_expression_filters(opts)252 desc253 end254end255Capybara.add_selector(:option) do256 xpath do |locator|257 xpath = XPath.descendant(:option)258 xpath = xpath[XPath.string.n.is(locator.to_s)] unless locator.nil?259 xpath260 end261 node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }262 node_filter(:selected, :boolean) { |node, value| !(value ^ node.selected?) }263 describe do |**options|264 desc = +""265 desc << " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)266 desc << " that is#{' not' unless options[:selected]} selected" if options.key?(:selected)267 desc268 end269end270Capybara.add_selector(:datalist_option) do271 label "datalist option"272 visible(:all)273 xpath do |locator|274 xpath = XPath.descendant(:option)275 xpath = xpath[XPath.string.n.is(locator.to_s) | (XPath.attr(:value) == locator.to_s)] unless locator.nil?276 xpath277 end278 node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }279 describe do |**options|280 desc = +""281 desc << " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)282 desc283 end284end285Capybara.add_selector(:file_field) do286 label "file field"287 xpath do |locator, options|288 xpath = XPath.descendant(:input)[XPath.attr(:type) == 'file']289 locate_field(xpath, locator, options)290 end291 filter_set(:_field, %i[disabled multiple name])292 describe do |**options|293 desc = +""294 desc << describe_all_expression_filters(options)295 desc296 end297end298Capybara.add_selector(:label) do299 label "label"300 xpath(:for) do |locator, options|301 xpath = XPath.descendant(:label)302 xpath = xpath[XPath.string.n.is(locator.to_s) | (XPath.attr(:id) == locator.to_s)] unless locator.nil?303 if options.key?(:for) && !options[:for].is_a?(Capybara::Node::Element)304 with_attr = XPath.attr(:for) == options[:for].to_s305 labelable_elements = %i[button input keygen meter output progress select textarea]306 wrapped = !XPath.attr(:for) &307 XPath.descendant(*labelable_elements)[XPath.attr(:id) == options[:for].to_s]308 xpath = xpath[with_attr | wrapped]309 end310 xpath311 end312 node_filter(:for) do |node, field_or_value|313 if field_or_value.is_a? Capybara::Node::Element314 if node[:for]315 field_or_value[:id] == node[:for]316 else317 field_or_value.find_xpath('./ancestor::label[1]').include? node.base318 end319 else320 true # Non element values were handled through the expression filter321 end322 end323 describe do |**options|324 desc = +""325 desc << " for #{options[:for]}" if options[:for]326 desc327 end328end329Capybara.add_selector(:table) do330 xpath(:caption) do |locator, caption: nil, **_options|331 xpath = XPath.descendant(:table)332 xpath = xpath[(XPath.attr(:id) == locator.to_s) | XPath.descendant(:caption).is(locator.to_s)] unless locator.nil?333 xpath = xpath[XPath.descendant(:caption) == caption] if caption334 xpath335 end336 describe do |caption: nil, **_options|337 desc = +""338 desc << " with caption #{caption}" if caption339 desc340 end341end342Capybara.add_selector(:frame) do343 xpath(:name) do |locator, **options|344 xpath = XPath.descendant(:iframe).union(XPath.descendant(:frame))345 xpath = xpath[(XPath.attr(:id) == locator.to_s) | (XPath.attr(:name) == locator.to_s)] unless locator.nil?346 xpath = expression_filters.keys.inject(xpath) { |memo, ef| memo[find_by_attr(ef, options[ef])] }347 xpath348 end349 describe do |name: nil, **_options|350 desc = +""351 desc << " with name #{name}" if name352 desc353 end354end355Capybara.add_selector(:element) do356 xpath do |locator, **_options|357 XPath.descendant((locator || '@').to_sym)358 end359 expression_filter(:attributes, matcher: /.+/) do |xpath, name, val|360 case val...
frame.rb
Source:frame.rb
...6 locator_matchers = (XPath.attr(:id) == locator.to_s) | (XPath.attr(:name) == locator.to_s)7 locator_matchers |= XPath.attr(test_id) == locator if test_id8 xpath = xpath[locator_matchers]9 end10 xpath[find_by_attr(:name, name)]11 end12 describe_expression_filters do |name: nil, **|13 " with name #{name}" if name14 end15end...
find_by_attr
Using AI Code Generation
1 Capybara::Selenium::Driver.new(app, :browser => :firefox)2 def find_by_attr(attr, value)3Capybara.visit('/')4Capybara.find_by_attr('id', 'lst-ib').set('Hello World!')5Capybara.find_by_attr('name', 'btnK').click6Capybara.find_by_attr('id', 'resultStats').text7"About 5,590,000,000 results (0.62 seconds)"
find_by_attr
Using AI Code Generation
1 def find_by_attr(attr, value)2find_by_attr('name', 'btnG').click3page.should have_content('capybara')
find_by_attr
Using AI Code Generation
1 def find_by_attr(attr, value)2 visit('/')3 find_by_attr('name', 'q').set('Hello World')
find_by_attr
Using AI Code Generation
1 def find_by_attr(attr, value)2 def find_by_attr(attr, value)3Capybara::Session.new.visit('/')4Capybara::Session.new.find_by_attr('name', 'q').set('Hello World')5Capybara::Session.new.find_by_attr('name', 'q').native.send_keys(:return)6Capybara::Session.new.find_by_attr('name', 'q').value.should eq('Hello World')7Capybara::Session.new.find_by_attr('name', 'q').native.send_keys(:backspace)8Capybara::Session.new.find_by_attr('name', 'q').value.should eq('Hello Worl')9Capybara::Session.new.find_by_attr('name', 'q').native.send_keys(:backspace)10Capybara::Session.new.find_by_attr('name', 'q').value.should eq('Hello Wor')11Capybara::Session.new.find_by_attr('name', 'q').native.send_keys(:backspace)12Capybara::Session.new.find_by_attr('name', 'q').value.should eq('Hello Wo')13Capybara::Session.new.find_by_attr('name', 'q').native.send_keys(:backspace)14Capybara::Session.new.find_by_attr('name', 'q').value.should eq('Hello W')15Capybara::Session.new.find_by_attr('name', 'q').native.send_keys(:backspace)16Capybara::Session.new.find_by_attr('name', 'q').value.should eq('Hello ')17Capybara::Session.new.find_by_attr('name', 'q').native.send_keys(:backspace)18Capybara::Session.new.find_by_attr('name', 'q').value.should eq('Hello')19Capybara::Session.new.find_by_attr('name', 'q').native.send_keys(:backspace)20Capybara::Session.new.find_by_attr('name', 'q').value.should eq
find_by_attr
Using AI Code Generation
1 def self.find_by_attr(attr, value)2Capybara.find_by_attr('id', 'lst-ib').set('Capybara')3Capybara.find_by_attr('name', 'btnK').click4Given(/^I am on Google's homepage$/) do5When(/^I enter "([^"]*)" in the search box$/) do |text|6 find_by_attr('id', 'lst-ib').set(text)7When(/^I click the search button$/) do8 find_by_attr('name', 'btnK').click9Then(/^I should see "([^"]*)" in the page title$/) do |text|10 expect(page.title).to include(text
find_by_attr
Using AI Code Generation
1puts Capybara.find_by_attr('name', 'peter')2puts Capybara.find_by_attr('name', 'peter')3puts Capybara.find_by_attr('name', 'peter')4puts Capybara.find_by_attr('name', 'peter')5puts Capybara.find_by_attr('name', 'peter')6puts Capybara.find_by_attr('name', 'peter')7puts Capybara.find_by_attr('name', 'peter')8puts Capybara.find_by_attr('name', 'peter')9puts Capybara.find_by_attr('name', 'peter')
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!!