Best Capybara code snippet using Capybara.Node.Matchers.has_selector
matchers.rb
Source:matchers.rb
...4 ##5 #6 # Checks if a given selector is on the page or current node.7 #8 # page.has_selector?('p#foo')9 # page.has_selector?(:xpath, './/p[@id="foo"]')10 # page.has_selector?(:foo)11 #12 # By default it will check if the expression occurs at least once,13 # but a different number can be specified.14 #15 # page.has_selector?('p.foo', :count => 4)16 #17 # This will check if the expression occurs exactly 4 times.18 #19 # It also accepts all options that {Capybara::Node::Finders#all} accepts,20 # such as :text and :visible.21 #22 # page.has_selector?('li', :text => 'Horse', :visible => true)23 #24 # has_selector? can also accept XPath expressions generated by the25 # XPath gem:26 #27 # page.has_selector?(:xpath, XPath.descendant(:p))28 #29 # @param (see Capybara::Node::Finders#all)30 # @param args31 # @option args [Integer] :count (nil) Number of times the text should occur32 # @option args [Integer] :minimum (nil) Minimum number of times the text should occur33 # @option args [Integer] :maximum (nil) Maximum number of times the text should occur34 # @option args [Range] :between (nil) Range of times that should contain number of times text occurs35 # @return [Boolean] If the expression exists36 #37 def has_selector?(*args)38 assert_selector(*args)39 rescue Capybara::ExpectationNotMet40 return false41 end42 ##43 #44 # Checks if a given selector is not on the page or current node.45 # Usage is identical to Capybara::Node::Matchers#has_selector?46 #47 # @param (see Capybara::Node::Finders#has_selector?)48 # @return [Boolean]49 #50 def has_no_selector?(*args)51 assert_no_selector(*args)52 rescue Capybara::ExpectationNotMet53 return false54 end55 ##56 #57 # Asserts that a given selector is on the page or current node.58 #59 # page.assert_selector('p#foo')60 # page.assert_selector(:xpath, './/p[@id="foo"]')61 # page.assert_selector(:foo)62 #63 # By default it will check if the expression occurs at least once,64 # but a different number can be specified.65 #66 # page.assert_selector('p#foo', :count => 4)67 #68 # This will check if the expression occurs exactly 4 times.69 #70 # It also accepts all options that {Capybara::Node::Finders#all} accepts,71 # such as :text and :visible.72 #73 # page.assert_selector('li', :text => 'Horse', :visible => true)74 #75 # `assert_selector` can also accept XPath expressions generated by the76 # XPath gem:77 #78 # page.assert_selector(:xpath, XPath.descendant(:p))79 #80 # @param (see Capybara::Node::Finders#all)81 # @option options [Integer] :count (nil) Number of times the expression should occur82 # @raise [Capybara::ExpectationNotMet] If the selector does not exist83 #84 def assert_selector(*args)85 query = Capybara::Query.new(*args)86 synchronize(query.wait) do87 result = all(*args)88 result.matches_count? or raise Capybara::ExpectationNotMet, result.failure_message89 end90 return true91 end92 ##93 #94 # Asserts that a given selector is not on the page or current node.95 # Usage is identical to Capybara::Node::Matchers#assert_selector96 #97 # @param (see Capybara::Node::Finders#assert_selector)98 # @raise [Capybara::ExpectationNotMet] If the selector exists99 #100 def assert_no_selector(*args)101 query = Capybara::Query.new(*args)102 synchronize(query.wait) do103 result = all(*args)104 result.matches_count? and raise Capybara::ExpectationNotMet, result.negative_failure_message105 end106 return true107 end108 ##109 #110 # Checks if a given XPath expression is on the page or current node.111 #112 # page.has_xpath?('.//p[@id="foo"]')113 #114 # By default it will check if the expression occurs at least once,115 # but a different number can be specified.116 #117 # page.has_xpath?('.//p[@id="foo"]', :count => 4)118 #119 # This will check if the expression occurs exactly 4 times.120 #121 # It also accepts all options that {Capybara::Node::Finders#all} accepts,122 # such as :text and :visible.123 #124 # page.has_xpath?('.//li', :text => 'Horse', :visible => true)125 #126 # has_xpath? can also accept XPath expressions generate by the127 # XPath gem:128 #129 # xpath = XPath.generate { |x| x.descendant(:p) }130 # page.has_xpath?(xpath)131 #132 # @param [String] path An XPath expression133 # @param options (see Capybara::Node::Finders#all)134 # @option options [Integer] :count (nil) Number of times the expression should occur135 # @return [Boolean] If the expression exists136 #137 def has_xpath?(path, options={})138 has_selector?(:xpath, path, options)139 end140 ##141 #142 # Checks if a given XPath expression is not on the page or current node.143 # Usage is identical to Capybara::Node::Matchers#has_xpath?144 #145 # @param (see Capybara::Node::Finders#has_xpath?)146 # @return [Boolean]147 #148 def has_no_xpath?(path, options={})149 has_no_selector?(:xpath, path, options)150 end151 ##152 #153 # Checks if a given CSS selector is on the page or current node.154 #155 # page.has_css?('p#foo')156 #157 # By default it will check if the selector occurs at least once,158 # but a different number can be specified.159 #160 # page.has_css?('p#foo', :count => 4)161 #162 # This will check if the selector occurs exactly 4 times.163 #164 # It also accepts all options that {Capybara::Node::Finders#all} accepts,165 # such as :text and :visible.166 #167 # page.has_css?('li', :text => 'Horse', :visible => true)168 #169 # @param [String] path A CSS selector170 # @param options (see Capybara::Node::Finders#all)171 # @option options [Integer] :count (nil) Number of times the selector should occur172 # @return [Boolean] If the selector exists173 #174 def has_css?(path, options={})175 has_selector?(:css, path, options)176 end177 ##178 #179 # Checks if a given CSS selector is not on the page or current node.180 # Usage is identical to Capybara::Node::Matchers#has_css?181 #182 # @param (see Capybara::Node::Finders#has_css?)183 # @return [Boolean]184 #185 def has_no_css?(path, options={})186 has_no_selector?(:css, path, options)187 end188 ##189 #190 # Checks if the page or current node has the given text content,191 # ignoring any HTML tags and normalizing whitespace.192 #193 # By default it will check if the text occurs at least once,194 # but a different number can be specified.195 #196 # page.has_text?('lorem ipsum', between: 2..4)197 #198 # This will check if the text occurs from 2 to 4 times.199 #200 # @overload has_text?([type], text, [options])201 # @param [:all, :visible] type Whether to check for only visible or all text202 # @param [String, Regexp] text The text/regexp to check for203 # @param [Hash] options additional options204 # @option options [Integer] :count (nil) Number of times the text should occur205 # @option options [Integer] :minimum (nil) Minimum number of times the text should occur206 # @option options [Integer] :maximum (nil) Maximum number of times the text should occur207 # @option options [Range] :between (nil) Range of times that should contain number of times text occurs208 # @return [Boolean] Whether it exists209 #210 def has_text?(*args)211 query = Capybara::Query.new(*args)212 synchronize(query.wait) do213 raise ExpectationNotMet unless text_found?(*args)214 end215 return true216 rescue Capybara::ExpectationNotMet217 return false218 end219 alias_method :has_content?, :has_text?220 ##221 #222 # Checks if the page or current node does not have the given text223 # content, ignoring any HTML tags and normalizing whitespace.224 #225 # @param (see #has_text?)226 # @return [Boolean] Whether it doesn't exist227 #228 def has_no_text?(*args)229 query = Capybara::Query.new(*args)230 synchronize(query.wait) do231 raise ExpectationNotMet if text_found?(*args)232 end233 return true234 rescue Capybara::ExpectationNotMet235 return false236 end237 alias_method :has_no_content?, :has_no_text?238 ##239 #240 # Checks if the page or current node has a link with the given241 # text or id.242 #243 # @param [String] locator The text or id of a link to check for244 # @param options245 # @option options [String] :href The value the href attribute must be246 # @return [Boolean] Whether it exists247 #248 def has_link?(locator, options={})249 has_selector?(:link, locator, options)250 end251 ##252 #253 # Checks if the page or current node has no link with the given254 # text or id.255 #256 # @param (see Capybara::Node::Finders#has_link?)257 # @return [Boolean] Whether it doesn't exist258 #259 def has_no_link?(locator, options={})260 has_no_selector?(:link, locator, options)261 end262 ##263 #264 # Checks if the page or current node has a button with the given265 # text, value or id.266 #267 # @param [String] locator The text, value or id of a button to check for268 # @return [Boolean] Whether it exists269 #270 def has_button?(locator, options={})271 has_selector?(:button, locator, options)272 end273 ##274 #275 # Checks if the page or current node has no button with the given276 # text, value or id.277 #278 # @param [String] locator The text, value or id of a button to check for279 # @return [Boolean] Whether it doesn't exist280 #281 def has_no_button?(locator, options={})282 has_no_selector?(:button, locator, options)283 end284 ##285 #286 # Checks if the page or current node has a form field with the given287 # label, name or id.288 #289 # For text fields and other textual fields, such as textareas and290 # HTML5 email/url/etc. fields, it's possible to specify a :with291 # option to specify the text the field should contain:292 #293 # page.has_field?('Name', :with => 'Jonas')294 #295 # It is also possible to filter by the field type attribute:296 #297 # page.has_field?('Email', :type => 'email')298 #299 # Note: 'textarea' and 'select' are valid type values, matching the associated tag names.300 #301 # @param [String] locator The label, name or id of a field to check for302 # @option options [String] :with The text content of the field303 # @option options [String] :type The type attribute of the field304 # @return [Boolean] Whether it exists305 #306 def has_field?(locator, options={})307 has_selector?(:field, locator, options)308 end309 ##310 #311 # Checks if the page or current node has no form field with the given312 # label, name or id. See {Capybara::Node::Matchers#has_field?}.313 #314 # @param [String] locator The label, name or id of a field to check for315 # @option options [String] :with The text content of the field316 # @option options [String] :type The type attribute of the field317 # @return [Boolean] Whether it doesn't exist318 #319 def has_no_field?(locator, options={})320 has_no_selector?(:field, locator, options)321 end322 ##323 #324 # Checks if the page or current node has a radio button or325 # checkbox with the given label, value or id, that is currently326 # checked.327 #328 # @param [String] locator The label, name or id of a checked field329 # @return [Boolean] Whether it exists330 #331 def has_checked_field?(locator, options={})332 has_selector?(:field, locator, options.merge(:checked => true))333 end334 ##335 #336 # Checks if the page or current node has no radio button or337 # checkbox with the given label, value or id, that is currently338 # checked.339 #340 # @param [String] locator The label, name or id of a checked field341 # @return [Boolean] Whether it doesn't exist342 #343 def has_no_checked_field?(locator, options={})344 has_no_selector?(:field, locator, options.merge(:checked => true))345 end346 ##347 #348 # Checks if the page or current node has a radio button or349 # checkbox with the given label, value or id, that is currently350 # unchecked.351 #352 # @param [String] locator The label, name or id of an unchecked field353 # @return [Boolean] Whether it exists354 #355 def has_unchecked_field?(locator, options={})356 has_selector?(:field, locator, options.merge(:unchecked => true))357 end358 ##359 #360 # Checks if the page or current node has no radio button or361 # checkbox with the given label, value or id, that is currently362 # unchecked.363 #364 # @param [String] locator The label, name or id of an unchecked field365 # @return [Boolean] Whether it doesn't exist366 #367 def has_no_unchecked_field?(locator, options={})368 has_no_selector?(:field, locator, options.merge(:unchecked => true))369 end370 ##371 #372 # Checks if the page or current node has a select field with the373 # given label, name or id.374 #375 # It can be specified which option should currently be selected:376 #377 # page.has_select?('Language', :selected => 'German')378 #379 # For multiple select boxes, several options may be specified:380 #381 # page.has_select?('Language', :selected => ['English', 'German'])382 #383 # It's also possible to check if the exact set of options exists for384 # this select box:385 #386 # page.has_select?('Language', :options => ['English', 'German', 'Spanish'])387 #388 # You can also check for a partial set of options:389 #390 # page.has_select?('Language', :with_options => ['English', 'German'])391 #392 # @param [String] locator The label, name or id of a select box393 # @option options [Array] :options Options which should be contained in this select box394 # @option options [Array] :with_options Partial set of options which should be contained in this select box395 # @option options [String, Array] :selected Options which should be selected396 # @return [Boolean] Whether it exists397 #398 def has_select?(locator, options={})399 has_selector?(:select, locator, options)400 end401 ##402 #403 # Checks if the page or current node has no select field with the404 # given label, name or id. See {Capybara::Node::Matchers#has_select?}.405 #406 # @param (see Capybara::Node::Matchers#has_select?)407 # @return [Boolean] Whether it doesn't exist408 #409 def has_no_select?(locator, options={})410 has_no_selector?(:select, locator, options)411 end412 ##413 #414 # Checks if the page or current node has a table with the given id415 # or caption:416 #417 # page.has_table?('People')418 #419 # @param [String] locator The id or caption of a table420 # @return [Boolean] Whether it exist421 #422 def has_table?(locator, options={})423 has_selector?(:table, locator, options)424 end425 ##426 #427 # Checks if the page or current node has no table with the given id428 # or caption. See {Capybara::Node::Matchers#has_table?}.429 #430 # @param (see Capybara::Node::Matchers#has_table?)431 # @return [Boolean] Whether it doesn't exist432 #433 def has_no_table?(locator, options={})434 has_no_selector?(:table, locator, options)435 end436 def ==(other)437 self.eql?(other) or (other.respond_to?(:base) and base == other.base)...
has_selector
Using AI Code Generation
1t.has_selector?(:xpath, '//input[@type="text"]')2t.has_selector?(:xpath, '//input[@type="text"]', :visible => true)3t.has_selector?(:xpath, '//input[@type="text"]', :visible => false)4t.has_selector?(:xpath, '//input[@type="text"]', :visible => :all)5t.has_selector?(:xpath, '//input[@type="text"]', :visible => :hidden)6t.has_selector?(:xpath, '//input[@type="text"]', :visible => :visible)7t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'all')8t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'hidden')9t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'visible')10t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'all')11t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'hidden')12t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'visible')13t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :count => 1)14t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :count => 2)15t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :count => 3)16t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :minimum => 1)17t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :minimum => 2)18t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :minimum => 3)19t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :maximum => 1)20t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :maximum => 2)21t.has_selector?(:xpath, '//input[@
has_selector
Using AI Code Generation
1 Capybara.visit('/')2 def search_for(query)3google.search_for('Capybara')
has_selector
Using AI Code Generation
1 def has_selector?(selector, options = {})2 has_selector?(selector, options)3Capybara::Session.new(:selenium).visit('/').has_selector?('input')4Capybara::Session.new(:selenium).visit('/').has_selector?('input')5Capybara::Session.new(:selenium).visit('/').has_selector?('input')6 def has_selector?(selector, options = {})7 has_selector?(selector, options)8Capybara::Session.new(:selenium).visit('/').has_selector?('input')9Capybara::Session.new(:selenium).visit('/').has_selector?('input')10 def has_selector?(selector, options = {})11 has_selector?(selector, options)12Capybara::Session.new(:selenium).visit('/').has_selector?('input')13Capybara::Session.new(:selenium).visit('/').has_selector?('input')
has_selector
Using AI Code Generation
1visit('/')2puts has_selector?('//input[@id="gbqfq"]')3puts has_selector?('//input[@id="gbqfq"]', :visible => true)4puts has_selector?('//input[@id="gbqfq"]', :visible => false)5puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 1)6puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 2)7puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 3)8puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 4)9puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 5)10puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 6)11puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 7)12puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 8)13puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 9)14puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 10)15puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 11)16puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 12)17puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 13)18puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count
has_selector
Using AI Code Generation
1 visit('/')2 fill_in('q', with: 'capybara')3 click_button('Google Search')4 assert has_selector?(:link, 'Capybara', visible: true)5 visit('/')6 fill_in('q', with: 'capybara')7 click_button('Google Search')8 assert has_link?('Capybara', visible: true)9Capybara::Session.new(:webkit).visit('http://www.google.com')10fill_in('q', with: 'capybara')11click_button('Google Search')12assert has_link?('Capybara', visible: true)
has_selector
Using AI Code Generation
1page.find(:css, "li").has_selector?(:link, "Home").click2page.find(:css, "li").find(:link, "Home").click3page.find(:css, "li").click_link "Home"4page.find(:css, "li").click_link "Home", :match => :first5page.find(:css, "li").click_link "Home", :match => :prefer_exact6page.find(:css, "li").click_link "Home", :match => :one7page.find(:css, "li").click_link "Home", :match => :smart8page.find(:css, "li").click_link "Home", :match => :first, :exact => true9page.find(:css, "li").click_link "Home", :match => :first, :exact => false
has_selector
Using AI Code Generation
1t.has_selector?(:xpath, '//input[@type="text"]')2t.has_selector?(:xpath, '//input[@type="text"]', :visible => true)3t.has_selector?(:xpath, '//input[@type="text"]', :visible => false)4t.has_selector?(:xpath, '//input[@type="text"]', :visible => :all)5t.has_selector?(:xpath, '//input[@type="text"]', :visible => :hidden)6t.has_selector?(:xpath, '//input[@type="text"]', :visible => :visible)7t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'all')8t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'hidden')9t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'visible')10t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'all')11t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'hidden')12t.has_selector?(:xpath, '//input[@type="text"]', :visible => 'visible')13t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :count => 1)14t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :count => 2)15t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :count => 3)16t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :minimum => 1)17t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :minimum => 2)18t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :minimum => 3)19t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :maximum => 1)20t.has_selector?(:xpath, '//input[@type="text"]', :visible => true, :maximum => 2)21t.has_selector?(:xpath, '//input[@
has_selector
Using AI Code Generation
1visit('/')2puts has_selector?('//input[@id="gbqfq"]')3puts has_selector?('//input[@id="gbqfq"]', :visible => true)4puts has_selector?('//input[@id="gbqfq"]', :visible => false)5puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 1)6puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 2)7puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 3)8puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 4)9puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 5)10puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 6)11puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 7)12puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 8)13puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 9)14puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 10)15puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 11)16puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 12)17puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count => 13)18puts has_selector?('//input[@id="gbqfq"]', :visible => false, :count
has_selector
Using AI Code Generation
1 visit('/')2 fill_in('q', with: 'capybara')3 click_button('Google Search')4 assert has_selector?(:link, 'Capybara', visible: true)5 visit('/')6 fill_in('q', with: 'capybara')7 click_button('Google Search')8 assert has_link?('Capybara', visible: true)9Capybara::Session.new(:webkit).visit('http://www.google.com')10fill_in('q', with: 'capybara')11click_button('Google Search')12assert has_link?('Capybara', visible: true)
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!!