How to use document method of Capybara Package

Best Capybara code snippet using Capybara.document

capybara.rb

Source:capybara.rb Github

copy

Full Screen

...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|386 hash[name] = Capybara::Session.new(current_driver, app)387 end388 end389 def specified_session390 if threadsafe391 Thread.current['capybara_specified_session']392 else393 @specified_session ||= nil394 end395 end396 def specified_session=(session)397 if threadsafe398 Thread.current['capybara_specified_session'] = session399 else400 @specified_session = session401 end402 end403 end404 self.default_driver = nil405 self.current_driver = nil406 self.server_host = nil407 module Driver; end408 module RackTest; end409 module Selenium; end410 require 'capybara/helpers'411 require 'capybara/session'412 require 'capybara/window'413 require 'capybara/server'414 require 'capybara/selector'415 require 'capybara/result'416 require 'capybara/version'417 require 'capybara/queries/base_query'418 require 'capybara/queries/selector_query'419 require 'capybara/queries/text_query'420 require 'capybara/queries/title_query'421 require 'capybara/queries/current_path_query'422 require 'capybara/queries/match_query'423 require 'capybara/queries/ancestor_query'424 require 'capybara/queries/sibling_query'425 require 'capybara/queries/style_query'426 require 'capybara/node/finders'427 require 'capybara/node/matchers'428 require 'capybara/node/actions'429 require 'capybara/node/document_matchers'430 require 'capybara/node/simple'431 require 'capybara/node/base'432 require 'capybara/node/element'433 require 'capybara/node/document'434 require 'capybara/driver/base'435 require 'capybara/driver/node'436 require 'capybara/rack_test/driver'437 require 'capybara/rack_test/node'438 require 'capybara/rack_test/form'439 require 'capybara/rack_test/browser'440 require 'capybara/rack_test/css_handlers.rb'441 require 'capybara/selenium/node'442 require 'capybara/selenium/driver'443end444require 'capybara/registrations/servers'445require 'capybara/registrations/drivers'446Capybara.configure do |config|447 config.always_include_port = false...

Full Screen

Full Screen

document

Using AI Code Generation

copy

Full Screen

1 def search_for(term)2google.search_for('cheese')3session = Capybara::Session.new(:webkit)4session.visit('/')5session.fill_in('q', :with => 'cheese')6session.click_button('Google Search')7session = Capybara::Session.new(:webkit)8session.visit('/')9session.document.fill_in('q', :with => 'cheese')10session.document.click_button('Google Search')11session = Capybara::Session.new(:webkit)12session.visit('/')13form = session.find(:css, 'form[action="/search"]')14form.fill_in('

Full Screen

Full Screen

document

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', :with => 'Hello World')3click_button('Google Search')

Full Screen

Full Screen

document

Using AI Code Generation

copy

Full Screen

1 page.should have_content('Capybara')2 page.should have_content('Capybara')3 page.should have_content('Capybara')

Full Screen

Full Screen

document

Using AI Code Generation

copy

Full Screen

1 @session = Capybara::Session.new(:selenium)2 @session.visit("http://www.google.com")3 @session = Capybara::Session.new(:selenium)4 @session.visit("http://www.google.com")5a.instance_variable_get('@session').document

Full Screen

Full Screen

document

Using AI Code Generation

copy

Full Screen

1 visit('/')2 visit('/')3 visit('/')4 visit('/')

Full Screen

Full Screen

document

Using AI Code Generation

copy

Full Screen

1Capybara.visit('/')2Capybara.fill_in('q', :with => 'ruby')3Capybara.click_button('btnG')4Capybara.save_screenshot('google.png')5Capybara.find_link('Ruby (programming language) - Wikipedia, the free encyclopedia').click6Capybara.find_button('Im Feeling Lucky').click7Capybara.find_field('q').set('ruby')

Full Screen

Full Screen

document

Using AI Code Generation

copy

Full Screen

1Capybara.visit('/')2Capybara.fill_in('q', :with => 'Capybara')3Capybara.click_button('Google Search')4Capybara.save_screenshot('capybara.png')5 page.should have_content('Capybara')6 page.should have_content('Capybara')7 page.should have_content('Capybara')

Full Screen

Full Screen

document

Using AI Code Generation

copy

Full Screen

1 visit('/')2 visit('/')3 visit('/')4 visit('/')

Full Screen

Full Screen

document

Using AI Code Generation

copy

Full Screen

1Capybara.visit('/')2Capybara.fill_in('q', :with => 'Capybara')3Capybara.click_button('Google Search')4Capybara.save_screenshot('capybara.png')

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.

Run Capybara automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful