Best Capybara code snippet using Capybara.Node.driver
capybara.rb
Source:capybara.rb
...16 attr_accessor :asset_host, :app_host, :run_server, :default_host, :always_include_port17 attr_accessor :server_host, :server_port, :exact, :match, :exact_options, :visible_text_only18 attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements19 attr_accessor :save_and_open_page_path, :automatic_reload, :raise_server_errors20 attr_writer :default_driver, :current_driver, :javascript_driver, :session_name21 attr_accessor :app22 ##23 #24 # Configure Capybara to suit your needs.25 #26 # Capybara.configure do |config|27 # config.run_server = false28 # config.app_host = 'http://www.google.com'29 # end30 #31 # === Configurable options32 #33 # [app_host = String] The default host to use when giving a relative URL to visit34 # [always_include_port = Boolean] Whether the Rack server's port should automatically be inserted into every visited URL (Default: false)35 # [asset_host = String] Where dynamic assets are hosted - will be prepended to relative asset locations if present (Default: nil)36 # [run_server = Boolean] Whether to start a Rack server for the given Rack app (Default: true)37 # [default_selector = :css/:xpath] Methods which take a selector use the given type by default (Default: CSS)38 # [default_wait_time = Integer] The number of seconds to wait for asynchronous processes to finish (Default: 2)39 # [ignore_hidden_elements = Boolean] Whether to ignore hidden elements on the page (Default: false)40 # [automatic_reload = Boolean] Whether to automatically reload elements as Capybara is waiting (Default: true)41 # [save_and_open_page_path = String] Where to put pages saved through save_and_open_page (Default: Dir.pwd)42 #43 # === DSL Options44 #45 # when using capybara/dsl, the following options are also available:46 #47 # [default_driver = Symbol] The name of the driver to use by default. (Default: :rack_test)48 # [javascript_driver = Symbol] The name of a driver to use for JavaScript enabled tests. (Default: :selenium)49 #50 def configure51 yield self52 end53 ##54 #55 # Register a new driver for Capybara.56 #57 # Capybara.register_driver :rack_test do |app|58 # Capybara::Driver::RackTest.new(app)59 # end60 #61 # @param [Symbol] name The name of the new driver62 # @yield [app] This block takes a rack app and returns a Capybara driver63 # @yieldparam [<Rack>] app The rack application that this driver runs agains. May be nil.64 # @yieldreturn [Capybara::Driver::Base] A Capybara driver instance65 #66 def register_driver(name, &block)67 drivers[name] = block68 end69 ##70 #71 # Add a new selector to Capybara. Selectors can be used by various methods in Capybara72 # to find certain elements on the page in a more convenient way. For example adding a73 # selector to find certain table rows might look like this:74 #75 # Capybara.add_selector(:row) do76 # xpath { |num| ".//tbody/tr[#{num}]" }77 # end78 #79 # This makes it possible to use this selector in a variety of ways:80 #81 # find(:row, 3)82 # page.find('table#myTable').find(:row, 3).text83 # page.find('table#myTable').has_selector?(:row, 3)84 # within(:row, 3) { page.should have_content('$100.000') }85 #86 # Here is another example:87 #88 # Capybara.add_selector(:id) do89 # xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }90 # end91 #92 # Note that this particular selector already ships with Capybara.93 #94 # @param [Symbol] name The name of the selector to add95 # @yield A block executed in the context of the new {Capybara::Selector}96 #97 def add_selector(name, &block)98 Capybara::Selector.add(name, &block)99 end100 def drivers101 @drivers ||= {}102 end103 ##104 #105 # Register a proc that Capybara will call to run the Rack application.106 #107 # Capybara.server do |app, port|108 # require 'rack/handler/mongrel'109 # Rack::Handler::Mongrel.run(app, :Port => port)110 # end111 #112 # By default, Capybara will try to run webrick.113 #114 # @yield [app, port] This block recieves a rack app and port and should run a Rack handler115 #116 def server(&block)117 if block_given?118 @server = block119 else120 @server121 end122 end123 ##124 #125 # Wraps the given string, which should contain an HTML document or fragment126 # in a {Capybara::Node::Simple} which exposes all {Capybara::Node::Matchers} and127 # {Capybara::Node::Finders}. This allows you to query any string containing128 # HTML in the exact same way you would query the current document in a Capybara129 # session. For example:130 #131 # node = Capybara.string <<-HTML132 # <ul>133 # <li id="home">Home</li>134 # <li id="projects">Projects</li>135 # </ul>136 # HTML137 #138 # node.find('#projects').text # => 'Projects'139 # node.has_selector?('li#home', :text => 'Home')140 # node.has_selector?(:projects)141 # node.find('ul').find('li').text # => 'Home'142 #143 # @param [String] html An html fragment or document144 # @return [Capybara::Node::Simple] A node which has Capybara's finders and matchers145 #146 def string(html)147 Capybara::Node::Simple.new(html)148 end149 ##150 #151 # Runs Capybara's default server for the given application and port152 # under most circumstances you should not have to call this method153 # manually.154 #155 # @param [Rack Application] app The rack application to run156 # @param [Fixnum] port The port to run the application on157 #158 def run_default_server(app, port)159 require 'rack/handler/webrick'160 Rack::Handler::WEBrick.run(app, :Port => port, :AccessLog => [], :Logger => WEBrick::Log::new(nil, 0))161 end162 ##163 #164 # @return [Symbol] The name of the driver to use by default165 #166 def default_driver167 @default_driver || :rack_test168 end169 ##170 #171 # @return [Symbol] The name of the driver currently in use172 #173 def current_driver174 @current_driver || default_driver175 end176 alias_method :mode, :current_driver177 ##178 #179 # @return [Symbol] The name of the driver used when JavaScript is needed180 #181 def javascript_driver182 @javascript_driver || :selenium183 end184 ##185 #186 # Use the default driver as the current driver187 #188 def use_default_driver189 @current_driver = nil190 end191 ##192 #193 # Yield a block using a specific driver194 #195 def using_driver(driver)196 previous_driver = Capybara.current_driver197 Capybara.current_driver = driver198 yield199 ensure200 @current_driver = previous_driver201 end202 ##203 #204 # Yield a block using a specific wait time205 #206 def using_wait_time(seconds)207 previous_wait_time = Capybara.default_wait_time208 Capybara.default_wait_time = seconds209 yield210 ensure211 Capybara.default_wait_time = previous_wait_time212 end213 ##214 #215 # The current Capybara::Session based on what is set as Capybara.app and Capybara.current_driver216 #217 # @return [Capybara::Session] The currently used session218 #219 def current_session220 session_pool["#{current_driver}:#{session_name}:#{app.object_id}"] ||= Capybara::Session.new(current_driver, app)221 end222 ##223 #224 # Reset sessions, cleaning out the pool of sessions. This will remove any session information such225 # as cookies.226 #227 def reset_sessions!228 session_pool.each { |mode, session| session.reset! }229 end230 alias_method :reset!, :reset_sessions!231 ##232 #233 # The current session name.234 #235 # @return [Symbol] The name of the currently used session.236 #237 def session_name238 @session_name ||= :default239 end240 ##241 #242 # Yield a block using a specific session name.243 #244 def using_session(name)245 self.session_name = name246 yield247 ensure248 self.session_name = :default249 end250 def included(base)251 base.send(:include, Capybara::DSL)252 warn "`include Capybara` is deprecated. Please use `include Capybara::DSL` instead."253 end254 def deprecate(method, alternate_method)255 warn "DEPRECATED: ##{method} is deprecated, please use ##{alternate_method} instead"256 end257 private258 def session_pool259 @session_pool ||= {}260 end261 end262 self.default_driver = nil263 self.current_driver = nil264 module Driver; end265 module RackTest; end266 module Selenium; end267 require 'capybara/helpers'268 require 'capybara/session'269 require 'capybara/dsl'270 require 'capybara/server'271 require 'capybara/selector'272 require 'capybara/query'273 require 'capybara/result'274 require 'capybara/version'275 require 'capybara/node/finders'276 require 'capybara/node/matchers'277 require 'capybara/node/actions'278 require 'capybara/node/simple'279 require 'capybara/node/base'280 require 'capybara/node/element'281 require 'capybara/node/document'282 require 'capybara/driver/base'283 require 'capybara/driver/node'284 require 'capybara/rack_test/driver'285 require 'capybara/rack_test/node'286 require 'capybara/rack_test/form'287 require 'capybara/rack_test/browser'288 require 'capybara/rack_test/css_handlers.rb'289 require 'capybara/selenium/node'290 require 'capybara/selenium/driver'291end292Capybara.configure do |config|293 config.always_include_port = false294 config.run_server = true295 config.server {|app, port| Capybara.run_default_server(app, port)}296 config.default_selector = :css297 config.default_wait_time = 2298 config.ignore_hidden_elements = true299 config.default_host = "http://www.example.com"300 config.automatic_reload = true301 config.match = :smart302 config.exact = false303 config.raise_server_errors = true304 config.visible_text_only = false305end306Capybara.register_driver :rack_test do |app|307 Capybara::RackTest::Driver.new(app)308end309Capybara.register_driver :selenium do |app|310 Capybara::Selenium::Driver.new(app)311end...
driver
Using AI Code Generation
1Capybara::Session.new(:webkit).visit('/')2puts Capybara::Session.new(:webkit).driver.name3Capybara::Session.new(:webkit).visit('/')4puts Capybara::Session.new(:webkit).driver.browser.title
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!!