How to use xpath method of Capybara.Queries Package

Best Capybara code snippet using Capybara.Queries.xpath

capybara.rb

Source:capybara.rb Github

copy

Full Screen

1# frozen_string_literal: true2require 'timeout'3require 'nokogiri'4require 'xpath'5require 'forwardable'6require 'capybara/config'7module Capybara8 class CapybaraError < StandardError; end9 class DriverNotFoundError < CapybaraError; end10 class FrozenInTime < CapybaraError; end11 class ElementNotFound < CapybaraError; end12 class ModalNotFound < CapybaraError; end13 class Ambiguous < ElementNotFound; end14 class ExpectationNotMet < ElementNotFound; end15 class FileNotFound < CapybaraError; end16 class UnselectNotAllowed < CapybaraError; end17 class NotSupportedByDriverError < CapybaraError; end18 class InfiniteRedirectError < CapybaraError; end19 class ScopeError < CapybaraError; end20 class WindowError < CapybaraError; end21 class ReadOnlyElementError < CapybaraError; end22 class << self23 extend Forwardable24 # DelegateCapybara global configurations25 # @!method app26 # See {Capybara.configure}27 # @!method reuse_server28 # See {Capybara.configure}29 # @!method threadsafe30 # See {Capybara.configure}31 # @!method server32 # See {Capybara.configure}33 # @!method default_driver34 # See {Capybara.configure}35 # @!method javascript_driver36 # See {Capybara.configure}37 # @!method allow_gumbo38 # See {Capybara.configure}39 Config::OPTIONS.each do |method|40 def_delegators :config, method, "#{method}="41 end42 # Delegate Capybara global configurations43 # @!method default_selector44 # See {Capybara.configure}45 # @!method default_max_wait_time46 # See {Capybara.configure}47 # @!method app_host48 # See {Capybara.configure}49 # @!method always_include_port50 # See {Capybara.configure}51 SessionConfig::OPTIONS.each do |method|52 def_delegators :config, method, "#{method}="53 end54 ##55 #56 # Configure Capybara to suit your needs.57 #58 # Capybara.configure do |config|59 # config.run_server = false60 # config.app_host = 'http://www.google.com'61 # end62 #63 # #### Configurable options64 #65 # - **allow_gumbo** (Boolean = `false`) - When `nokogumbo` is available, whether it will be used to parse HTML strings.66 # - **always_include_port** (Boolean = `false`) - Whether the Rack server's port should automatically be inserted into every visited URL67 # unless another port is explicitly specified.68 # - **app_host** (String, `nil`) - The default host to use when giving a relative URL to visit, must be a valid URL e.g. `http://www.example.com`.69 # - **asset_host** (String = `nil`) - Where dynamic assets are hosted - will be prepended to relative asset locations if present.70 # - **automatic_label_click** (Boolean = `false`) - Whether {Capybara::Node::Element#choose Element#choose}, {Capybara::Node::Element#check Element#check},71 # {Capybara::Node::Element#uncheck Element#uncheck} will attempt to click the associated `<label>` element if the checkbox/radio button are non-visible.72 # - **automatic_reload** (Boolean = `true`) - Whether to automatically reload elements as Capybara is waiting.73 # - **default_max_wait_time** (Numeric = `2`) - The maximum number of seconds to wait for asynchronous processes to finish.74 # - **default_normalize_ws** (Boolean = `false`) - Whether text predicates and matchers use normalize whitespace behavior.75 # - **default_selector** (`:css`, `:xpath` = `:css`) - Methods which take a selector use the given type by default. See also {Capybara::Selector}.76 # - **default_set_options** (Hash = `{}`) - The default options passed to {Capybara::Node::Element#set Element#set}.77 # - **enable_aria_label** (Boolean = `false`) - Whether fields, links, and buttons will match against `aria-label` attribute.78 # - **exact** (Boolean = `false`) - Whether locators are matched exactly or with substrings. Only affects selector conditions79 # written using the `XPath#is` method.80 # - **exact_text** (Boolean = `false`) - Whether the text matchers and `:text` filter match exactly or on substrings.81 # - **ignore_hidden_elements** (Boolean = `true`) - Whether to ignore hidden elements on the page.82 # - **match** (`:one`, `:first`, `:prefer_exact`, `:smart` = `:smart`) - The matching strategy to find nodes.83 # - **predicates_wait** (Boolean = `true`) - Whether Capybara's predicate matchers use waiting behavior by default.84 # - **raise_server_errors** (Boolean = `true`) - Should errors raised in the server be raised in the tests?85 # - **reuse_server** (Boolean = `true`) - Whether to reuse the server thread between multiple sessions using the same app object.86 # - **run_server** (Boolean = `true`) - Whether to start a Rack server for the given Rack app.87 # - **save_path** (String = `Dir.pwd`) - Where to put pages saved through {Capybara::Session#save_page save_page}, {Capybara::Session#save_screenshot save_screenshot},88 # {Capybara::Session#save_and_open_page save_and_open_page}, or {Capybara::Session#save_and_open_screenshot save_and_open_screenshot}.89 # - **server** (Symbol = `:default` (which uses puma)) - The name of the registered server to use when running the app under test.90 # - **server_errors** (Array\<Class> = `[Exception]`) - Error classes that should be raised in the tests if they are raised in the server91 # and {configure raise_server_errors} is `true`.92 # - **test_id** (Symbol, String, `nil` = `nil`) - Optional attribute to match locator against with built-in selectors along with id.93 # - **threadsafe** (Boolean = `false`) - Whether sessions can be configured individually.94 # - **w3c_click_offset** (Boolean = 'false') - Whether click offsets should be from element center (true) or top left (false)95 #96 # #### DSL Options97 #98 # When using `capybara/dsl`, the following options are also available:99 #100 # - **default_driver** (Symbol = `:rack_test`) - The name of the driver to use by default.101 # - **javascript_driver** (Symbol = `:selenium`) - The name of a driver to use for JavaScript enabled tests.102 #103 def configure104 yield config105 end106 ##107 #108 # Register a new driver for Capybara.109 #110 # Capybara.register_driver :rack_test do |app|111 # Capybara::RackTest::Driver.new(app)112 # end113 #114 # @param [Symbol] name The name of the new driver115 # @yield [app] This block takes a rack app and returns a Capybara driver116 # @yieldparam [<Rack>] app The rack application that this driver runs against. May be nil.117 # @yieldreturn [Capybara::Driver::Base] A Capybara driver instance118 #119 def register_driver(name, &block)120 drivers[name] = block121 end122 ##123 #124 # Register a new server for Capybara.125 #126 # Capybara.register_server :webrick do |app, port, host|127 # require 'rack/handler/webrick'128 # Rack::Handler::WEBrick.run(app, ...)129 # end130 #131 # @param [Symbol] name The name of the new driver132 # @yield [app, port, host] This block takes a rack app and a port and returns a rack server listening on that port133 # @yieldparam [<Rack>] app The rack application that this server will contain.134 # @yieldparam port The port number the server should listen on135 # @yieldparam host The host/ip to bind to136 #137 def register_server(name, &block)138 servers[name.to_sym] = block139 end140 ##141 #142 # Add a new selector to Capybara. Selectors can be used by various methods in Capybara143 # to find certain elements on the page in a more convenient way. For example adding a144 # selector to find certain table rows might look like this:145 #146 # Capybara.add_selector(:row) do147 # xpath { |num| ".//tbody/tr[#{num}]" }148 # end149 #150 # This makes it possible to use this selector in a variety of ways:151 #152 # find(:row, 3)153 # page.find('table#myTable').find(:row, 3).text154 # page.find('table#myTable').has_selector?(:row, 3)155 # within(:row, 3) { expect(page).to have_content('$100.000') }156 #157 # Here is another example:158 #159 # Capybara.add_selector(:id) do160 # xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }161 # end162 #163 # Note that this particular selector already ships with Capybara.164 #165 # @param [Symbol] name The name of the selector to add166 # @yield A block executed in the context of the new {Capybara::Selector}167 #168 def add_selector(name, **options, &block)169 Capybara::Selector.add(name, **options, &block)170 end171 ##172 #173 # Modify a selector previously created by {Capybara.add_selector}.174 # For example, adding a new filter to the :button selector to filter based on175 # button style (a class) might look like this176 #177 # Capybara.modify_selector(:button) do178 # filter (:btn_style, valid_values: [:primary, :secondary]) { |node, style| node[:class].split.include? "btn-#{style}" }179 # end180 #181 #182 # @param [Symbol] name The name of the selector to modify183 # @yield A block executed in the context of the existing {Capybara::Selector}184 #185 def modify_selector(name, &block)186 Capybara::Selector.update(name, &block)187 end188 def drivers189 @drivers ||= {}190 end191 def servers192 @servers ||= {}193 end194 # Wraps the given string, which should contain an HTML document or fragment195 # in a {Capybara::Node::Simple} which exposes all {Capybara::Node::Matchers},196 # {Capybara::Node::Finders} and {Capybara::Node::DocumentMatchers}. This allows you to query197 # any string containing HTML in the exact same way you would query the current document in a Capybara198 # session.199 #200 # @example A single element201 # node = Capybara.string('<a href="foo">bar</a>')202 # anchor = node.first('a')203 # anchor[:href] #=> 'foo'204 # anchor.text #=> 'bar'205 #206 # @example Multiple elements207 # node = Capybara.string <<-HTML208 # <ul>209 # <li id="home">Home</li>210 # <li id="projects">Projects</li>211 # </ul>212 # HTML213 #214 # node.find('#projects').text # => 'Projects'215 # node.has_selector?('li#home', text: 'Home')216 # node.has_selector?('#projects')217 # node.find('ul').find('li:first-child').text # => 'Home'218 #219 # @param [String] html An html fragment or document220 # @return [Capybara::Node::Simple] A node which has Capybara's finders and matchers221 #222 def string(html)223 Capybara::Node::Simple.new(html)224 end225 ##226 #227 # Runs Capybara's default server for the given application and port228 # under most circumstances you should not have to call this method229 # manually.230 #231 # @param [Rack Application] app The rack application to run232 # @param [Integer] port The port to run the application on233 #234 def run_default_server(app, port)235 servers[:puma].call(app, port, server_host)236 end237 ##238 #239 # @return [Symbol] The name of the driver currently in use240 #241 def current_driver242 if threadsafe243 Thread.current['capybara_current_driver']244 else245 @current_driver246 end || default_driver247 end248 alias_method :mode, :current_driver249 def current_driver=(name)250 if threadsafe251 Thread.current['capybara_current_driver'] = name252 else253 @current_driver = name254 end255 end256 ##257 #258 # Use the default driver as the current driver259 #260 def use_default_driver261 self.current_driver = nil262 end263 ##264 #265 # Yield a block using a specific driver266 #267 def using_driver(driver)268 previous_driver = Capybara.current_driver269 Capybara.current_driver = driver270 yield271 ensure272 self.current_driver = previous_driver273 end274 ##275 #276 # Yield a block using a specific wait time277 #278 def using_wait_time(seconds)279 previous_wait_time = Capybara.default_max_wait_time280 Capybara.default_max_wait_time = seconds281 yield282 ensure283 Capybara.default_max_wait_time = previous_wait_time284 end285 ##286 #287 # The current {Capybara::Session} based on what is set as {app} and {current_driver}.288 #289 # @return [Capybara::Session] The currently used session290 #291 def current_session292 specified_session || session_pool["#{current_driver}:#{session_name}:#{app.object_id}"]293 end294 ##295 #296 # Reset sessions, cleaning out the pool of sessions. This will remove any session information such297 # as cookies.298 #299 def reset_sessions!300 # reset in reverse so sessions that started servers are reset last301 session_pool.reverse_each { |_mode, session| session.reset! }302 end303 alias_method :reset!, :reset_sessions!304 ##305 #306 # The current session name.307 #308 # @return [Symbol] The name of the currently used session.309 #310 def session_name311 if threadsafe312 Thread.current['capybara_session_name'] ||= :default313 else314 @session_name ||= :default315 end316 end317 def session_name=(name)318 if threadsafe319 Thread.current['capybara_session_name'] = name320 else321 @session_name = name322 end323 end324 ##325 #326 # Yield a block using a specific session name or {Capybara::Session} instance.327 #328 def using_session(name_or_session)329 previous_session_info = {330 specified_session: specified_session,331 session_name: session_name,332 current_driver: current_driver,333 app: app334 }335 self.specified_session = self.session_name = nil336 if name_or_session.is_a? Capybara::Session337 self.specified_session = name_or_session338 else339 self.session_name = name_or_session340 end341 yield342 ensure343 self.session_name, self.specified_session = previous_session_info.values_at(:session_name, :specified_session)344 self.current_driver, self.app = previous_session_info.values_at(:current_driver, :app) if threadsafe345 end346 ##347 #348 # Parse raw html into a document using Nokogiri, and adjust textarea contents as defined by the spec.349 #350 # @param [String] html The raw html351 # @return [Nokogiri::HTML::Document] HTML document352 #353 def HTML(html) # rubocop:disable Naming/MethodName354 if Nokogiri.respond_to?(:HTML5) && Capybara.allow_gumbo # Nokogumbo installed and allowed for use355 Nokogiri::HTML5(html).tap do |document|356 document.xpath('//template').each do |template|357 # template elements content is not part of the document358 template.inner_html = ''359 end360 document.xpath('//textarea').each do |textarea|361 # The Nokogumbo HTML5 parser already returns spec compliant contents362 textarea['_capybara_raw_value'] = textarea.content363 end364 end365 else366 Nokogiri::HTML(html).tap do |document|367 document.xpath('//template').each do |template|368 # template elements content is not part of the document369 template.inner_html = ''370 end371 document.xpath('//textarea').each do |textarea|372 textarea['_capybara_raw_value'] = textarea.content.sub(/\A\n/, '')373 end374 end375 end376 end377 def session_options378 config.session_options379 end380 private381 def config382 @config ||= Capybara::Config.new383 end384 def session_pool385 @session_pool ||= Hash.new do |hash, name|...

Full Screen

Full Screen

xpath

Using AI Code Generation

copy

Full Screen

1 def initialize(locator, options)2 def resolve_for(node)3 node.find(:xpath, @locator, @options)4Capybara::Queries.add(:xpath, Capybara::Queries::XPathQuery)5if page.has_xpath?(xpath)6page.save_screenshot('1.png')7 def xpath(locator, options = {})8 find(:xpath, locator, options)9if page.has_xpath?(xpath)10page.save_screenshot('2.png')11 def xpath(locator, options = {})12 find(:xpath, locator, options)

Full Screen

Full Screen

xpath

Using AI Code Generation

copy

Full Screen

1find(:xpath, '//input[@name="q"]').set 'capybara'2find(:xpath, '//input[@name="btnG"]').click3find(:xpath, '//a[contains(text(),"Capybara")]').click4find(:xpath, '//input[@name="q"]').set 'capybara'5find(:xpath, '//input[@name="btnG"]').click6find(:xpath, '//a[contains(text(),"Capybara")]').click7find(:xpath, '//input[@name="q"]').set 'capybara'8find(:xpath, '//input[@name="btnG"]').click9find(:xpath, '//a[contains(text(),"Capybara")]').click10find(:xpath, '//input[@name="q"]').set 'capybara'11find(:xpath, '//input[@name="btnG"]').click12find(:xpath, '//a[contains(text(),"Capybara")]').click13find(:xpath, '//input[@name="q"]').set 'capybara'14find(:xpath, '//input[@name="btnG"]').click15find(:xpath, '//a[contains(text(),"Capybara")]').click16find(:xpath, '//input[@name="q"]').set 'capybara'17find(:xpath, '//input[@name="btnG"]').click18find(:xpath, '//a[contains(text(),"Capybara")]').click19find(:xpath, '//input[@name="q"]').set 'capybara'20find(:xpath, '//input[@name="btnG"]').click21find(:xpath, '//a[contains(text(),"Capybara")]').click

Full Screen

Full Screen

xpath

Using AI Code Generation

copy

Full Screen

1session = Capybara::Session.new(:selenium)2session.visit("http://www.google.com")3session.find(:xpath, "//input[@name='q']").set("Hello World")4session = Capybara::Session.new(:selenium)5session.visit("http://www.google.com")6session.find(:xpath, "//input[@name='q']").set("Hello World")7RSpec is a Ruby gem that helps you write better code. It is a testing framework that allows you to write test cases in a human-readable format. It is a domain-specific language (DSL) that lets

Full Screen

Full Screen

xpath

Using AI Code Generation

copy

Full Screen

1 def resolve_for(node)2 node.all(:xpath, @selector)3 def resolve_for(node)4 node.all(:xpath, @selector)5 def resolve_for(node)6 node.all(:xpath, @selector)7 def resolve_for(node)8 node.all(:xpath, @selector)9 def resolve_for(node)10 node.all(:xpath, @selector)

Full Screen

Full Screen

xpath

Using AI Code Generation

copy

Full Screen

1 def resolve_for(node)2 node.all(:xpath, @selector)3 def resolve_for(node)4 node.all(:xpath, @selector 5 def resolce_for(node)6 node.alp(:xpath, @selector)7 def resolve_for(node)8 node.all(:xpath, @selector)9 def resolve_for(node)10 node.all(:xpath, @selector)11find(:xpath, '//input[@name="btnG"]').click12find(:xpath, '//a[contains(text(),"Capybara")]').click13find(:xpath, '//input[@name="q"]').set 'capybara'14find(:xpath, '//input[@name="btnG"]').click15find(:xpath, '//a[contains(text(),"Capybara")]').click16find(:xpath, '//input[@name="q"]').set 'capybara'17find(:xpath, '//input[@name="btnG"]').click18find(:xpath, '//a[contains(text(),"Capybara")]').click19find(:xpath, '//input[@name="q"]').set 'capybara'20find(:xpath, '//input[@name="btnG"]').click21find(:xpath, '//a[contains(text(),"Capybara")]').click22find(:xpath, '//input[@name="q"]').set 'capybara'23find(:xpath, '//input[@name="btnG"]').click24find(:xpath, '//a[contains(text(),"Capybara")]').click25find(:xpath, '//input[@name="q"]').set 'capybara'26find(:xpath, '//input[@name="btnG"]').click27find(:xpath, '//a[contains(text(),"Capybara")]').click28find(:xpath, '//input[@name="q"]').set 'capybara'29find(:xpath, '//input[@name="btnG"]').click30find(:xpath, '//a[contains(text(),"Capybara")]').click

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful