Best Capybara code snippet using Capybara.Node.Matchers.has_xpath
matchers.rb
Source:matchers.rb
...86 ##87 #88 # Checks if a given XPath expression is on the page or current node.89 #90 # page.has_xpath?('.//p[@id="foo"]')91 #92 # By default it will check if the expression occurs at least once,93 # but a different number can be specified.94 #95 # page.has_xpath?('.//p[@id="foo"]', :count => 4)96 #97 # This will check if the expression occurs exactly 4 times.98 #99 # It also accepts all options that {Capybara::Node::Finders#all} accepts,100 # such as :text and :visible.101 #102 # page.has_xpath?('.//li', :text => 'Horse', :visible => true)103 #104 # has_xpath? can also accept XPath expressions generate by the105 # XPath gem:106 #107 # xpath = XPath.generate { |x| x.descendant(:p) }108 # page.has_xpath?(xpath)109 #110 # @param [String] path An XPath expression111 # @param options (see Capybara::Node::Finders#all)112 # @option options [Integer] :count (nil) Number of times the expression should occur113 # @return [Boolean] If the expression exists114 #115 def has_xpath?(path, options={})116 has_selector?(:xpath, path, options)117 end118 ##119 #120 # Checks if a given XPath expression is not on the page or current node.121 # Usage is identical to Capybara::Node::Matchers#has_xpath?122 #123 # @param (see Capybara::Node::Finders#has_xpath?)124 # @return [Boolean]125 #126 def has_no_xpath?(path, options={})127 has_no_selector?(:xpath, path, options)128 end129 ##130 #131 # Checks if a given CSS selector is on the page or current node.132 #133 # page.has_css?('p#foo')134 #135 # By default it will check if the selector occurs at least once,136 # but a different number can be specified.137 #138 # page.has_css?('p#foo', :count => 4)139 #140 # This will check if the selector occurs exactly 4 times.141 #142 # It also accepts all options that {Capybara::Node::Finders#all} accepts,143 # such as :text and :visible.144 #145 # page.has_css?('li', :text => 'Horse', :visible => true)146 #147 # @param [String] path A CSS selector148 # @param options (see Capybara::Node::Finders#all)149 # @option options [Integer] :count (nil) Number of times the selector should occur150 # @return [Boolean] If the selector exists151 #152 def has_css?(path, options={})153 has_xpath?(XPath.css(path), options)154 end155 ##156 #157 # Checks if a given CSS selector is not on the page or current node.158 # Usage is identical to Capybara::Node::Matchers#has_css?159 #160 # @param (see Capybara::Node::Finders#has_css?)161 # @return [Boolean]162 #163 def has_no_css?(path, options={})164 has_no_xpath?(XPath.css(path), options)165 end166 ##167 #168 # Checks if the page or current node has the given text content,169 # ignoring any HTML tags and normalizing whitespace.170 #171 # @param [String] content The text to check for172 # @return [Boolean] Whether it exists173 #174 def has_content?(content)175 has_xpath?(XPath::HTML.content(content))176 end177 ##178 #179 # Checks if the page or current node does not have the given text180 # content, ignoring any HTML tags and normalizing whitespace.181 #182 # @param [String] content The text to check for183 # @return [Boolean] Whether it exists184 #185 def has_no_content?(content)186 has_no_xpath?(XPath::HTML.content(content))187 end188 ##189 #190 # Checks if the page or current node has a link with the given191 # text or id.192 #193 # @param [String] locator The text or id of a link to check for194 # @param options195 # @option options [String] :href The value the href attribute must be196 # @return [Boolean] Whether it exists197 #198 def has_link?(locator, options={})199 has_xpath?(XPath::HTML.link(locator, options))200 end201 ##202 #203 # Checks if the page or current node has no link with the given204 # text or id.205 #206 # @param (see Capybara::Node::Finders#has_link?)207 # @return [Boolean] Whether it doesn't exist208 #209 def has_no_link?(locator, options={})210 has_no_xpath?(XPath::HTML.link(locator, options))211 end212 ##213 #214 # Checks if the page or current node has a button with the given215 # text, value or id.216 #217 # @param [String] locator The text, value or id of a button to check for218 # @return [Boolean] Whether it exists219 #220 def has_button?(locator)221 has_xpath?(XPath::HTML.button(locator))222 end223 ##224 #225 # Checks if the page or current node has no button with the given226 # text, value or id.227 #228 # @param [String] locator The text, value or id of a button to check for229 # @return [Boolean] Whether it doesn't exist230 #231 def has_no_button?(locator)232 has_no_xpath?(XPath::HTML.button(locator))233 end234 ##235 #236 # Checks if the page or current node has a form field with the given237 # label, name or id.238 #239 # For text fields and other textual fields, such as textareas and240 # HTML5 email/url/etc. fields, it's possible to specify a :with241 # option to specify the text the field should contain:242 #243 # page.has_field?('Name', :with => 'Jonas')244 #245 # @param [String] locator The label, name or id of a field to check for246 # @option options [String] :with The text content of the field247 # @return [Boolean] Whether it exists248 #249 def has_field?(locator, options={})250 options, with = split_options(options, :with)251 has_xpath?(XPath::HTML.field(locator, options), with)252 end253 ##254 #255 # Checks if the page or current node has no form field with the given256 # label, name or id. See {Capybara::Node::Matchers#has_field?}.257 #258 # @param [String] locator The label, name or id of a field to check for259 # @option options [String] :with The text content of the field260 # @return [Boolean] Whether it doesn't exist261 #262 def has_no_field?(locator, options={})263 options, with = split_options(options, :with)264 has_no_xpath?(XPath::HTML.field(locator, options), with)265 end266 ##267 #268 # Checks if the page or current node has a radio button or269 # checkbox with the given label, value or id, that is currently270 # checked.271 #272 # @param [String] locator The label, name or id of a checked field273 # @return [Boolean] Whether it exists274 #275 def has_checked_field?(locator)276 has_xpath?(XPath::HTML.field(locator), :checked => true)277 end278 ##279 #280 # Checks if the page or current node has no radio button or281 # checkbox with the given label, value or id, that is currently282 # checked.283 #284 # @param [String] locator The label, name or id of a checked field285 # @return [Boolean] Whether it doesn't exists286 #287 def has_no_checked_field?(locator)288 has_no_xpath?(XPath::HTML.field(locator), :checked => true)289 end290 ##291 #292 # Checks if the page or current node has a radio button or293 # checkbox with the given label, value or id, that is currently294 # unchecked.295 #296 # @param [String] locator The label, name or id of an unchecked field297 # @return [Boolean] Whether it exists298 #299 def has_unchecked_field?(locator)300 has_xpath?(XPath::HTML.field(locator), :unchecked => true)301 end302 ##303 #304 # Checks if the page or current node has no radio button or305 # checkbox with the given label, value or id, that is currently306 # unchecked.307 #308 # @param [String] locator The label, name or id of an unchecked field309 # @return [Boolean] Whether it doesn't exists310 #311 def has_no_unchecked_field?(locator)312 has_no_xpath?(XPath::HTML.field(locator), :unchecked => true)313 end314 ##315 #316 # Checks if the page or current node has a select field with the317 # given label, name or id.318 #319 # It can be specified which option should currently be selected:320 #321 # page.has_select?('Language', :selected => 'German')322 #323 # For multiple select boxes, several options may be specified:324 #325 # page.has_select?('Language', :selected => ['English', 'German'])326 #327 # It's also possible to check if a given set of options exists for328 # this select box:329 #330 # page.has_select?('Language', :options => ['English', 'German'])331 #332 # @param [String] locator The label, name or id of a select box333 # @option options [Array] :options Options which should be contained in this select box334 # @option options [String, Array] :selected Options which should be selected335 # @return [Boolean] Whether it exists336 #337 def has_select?(locator, options={})338 options, selected = split_options(options, :selected)339 has_xpath?(XPath::HTML.select(locator, options), selected)340 end341 ##342 #343 # Checks if the page or current node has no select field with the344 # given label, name or id. See {Capybara::Node::Matchers#has_select?}.345 #346 # @param (see Capybara::Node::Matchers#has_select?)347 # @return [Boolean] Whether it doesn't exist348 #349 def has_no_select?(locator, options={})350 options, selected = split_options(options, :selected)351 has_no_xpath?(XPath::HTML.select(locator, options), selected)352 end353 ##354 #355 # Checks if the page or current node has a table with the given id356 # or caption.357 #358 # If the options :rows is given, it will check that the table contains359 # the rows and columns given:360 #361 # page.has_table?('People', :rows => [['Jonas', '24'], ['Peter', '32']])362 #363 # Note that this option is quite strict, the order needs to be correct364 # and the text needs to match exactly.365 #366 # @param [String] locator The id or caption of a table367 # @option options [Array[Array[String]]] :rows A set of rows the table should contain368 # @return [Boolean] Whether it exist369 #370 def has_table?(locator, options={})371 has_xpath?(XPath::HTML.table(locator, options))372 end373 ##374 #375 # Checks if the page or current node has no table with the given id376 # or caption. See {Capybara::Node::Matchers#has_table?}.377 #378 # @param (see Capybara::Node::Matchers#has_table?)379 # @return [Boolean] Whether it doesn't exist380 #381 def has_no_table?(locator, options={})382 has_no_xpath?(XPath::HTML.table(locator, options))383 end384 protected385 def split_options(options, key)...
has_xpath
Using AI Code Generation
1World(Capybara)2Given(/^I am on the google home page$/) do3 visit('/')4When(/^I search for "([^"]*)"$/) do |search_term|5 fill_in('q', :with => search_term)6 click_button('Google Search')7Then(/^I should see "([^"]*)" in the search results$/) do |search_result|8 expect(page).to have_xpath("//h3/a", :text => search_result)91 scenario (1 passed)103 steps (3 passed)
has_xpath
Using AI Code Generation
1 def has_xpath?(xpath)2 has_xpath?(xpath)3 def has_xpath?(xpath)4 has_xpath?(xpath)5 def has_xpath?(xpath)6 has_xpath?(xpath)7 def has_xpath?(xpath)8 has_xpath?(xpath)9 def has_xpath?(xpath)10 has_xpath?(xpath)11 def has_xpath?(xpath)12 has_xpath?(xpath)13 def has_xpath?(xpath)14 has_xpath?(xpath)15 def has_xpath?(xpath)16 has_xpath?(xpath)17 def has_xpath?(xpath)18 has_xpath?(xpath)19 def has_xpath?(xpath)20 has_xpath?(xpath)21 def has_xpath?(xpath)22 has_xpath?(xpath)23 def has_xpath?(xpath)24 has_xpath?(xpath)25 def has_xpath?(xpath)26 has_xpath?(xpath)
has_xpath
Using AI Code Generation
1 config.allow_url("www.google.com")2if has_xpath?('//a[text()="Google" and @href="http://www.google.com"]')3has_xpath?(xpath, options = {})4 config.allow_url("www.google.com")5if has_xpath?('//a[text()="Google" and @href="http://www.google.com"]', :visible => true)
has_xpath
Using AI Code Generation
1 Capybara::Poltergeist::Driver.new(app, {js_errors: false})2 Capybara::Webkit::Driver.new(app, :allow_unknown_urls)3 def has_xpath?(*args)4 has_selector?(:xpath, *args)5Capybara.visit('/')6if Capybara.has_xpath?("//input[@name='q']", :visible => true)7 Capybara::Poltergeist::Driver.new(app, {js_errors: false})8 Capybara::Webkit::Driver.new(app, :allow_unknown_urls)
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!!