How to use html method of Capybara Package

Best Capybara code snippet using Capybara.html

capybara.rb

Source:capybara.rb Github

copy

Full Screen

...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 end...

Full Screen

Full Screen

save_page_spec.rb

Source:save_page_spec.rb Github

copy

Full Screen

...7 @session.visit('/foo')8 end9 after do10 Capybara.save_path = @old_save_path11 Dir.glob('capybara-*.html').each do |file|12 FileUtils.rm(file)13 end14 FileUtils.rm_rf alternative_path15 end16 it 'saves the page in the root directory' do17 @session.save_page18 path = Dir.glob('capybara-*.html').first19 expect(File.read(path)).to include('Another World')20 end21 it 'generates a sensible filename' do22 @session.save_page23 filename = Dir.glob('capybara-*.html').first24 expect(filename).to match(/^capybara-\d+\.html$/)25 end26 it 'can store files in a specified directory' do27 Capybara.save_path = alternative_path28 @session.save_page29 path = Dir.glob(alternative_path + '/capybara-*.html').first30 expect(File.read(path)).to include('Another World')31 end32 it 'uses the given filename' do33 @session.save_page('capybara-001122.html')34 expect(File.read('capybara-001122.html')).to include('Another World')35 end36 it 'can store files in a specified directory with a given filename' do37 Capybara.save_path = alternative_path38 @session.save_page('capybara-001133.html')39 path = alternative_path + '/capybara-001133.html'40 expect(File.read(path)).to include('Another World')41 end42 it 'can store files in a specified directory with a given relative filename' do43 Capybara.save_path = alternative_path44 @session.save_page('tmp/capybara-001144.html')45 path = alternative_path + '/tmp/capybara-001144.html'46 expect(File.read(path)).to include('Another World')47 end48 it 'returns an absolute path in pwd' do49 result = @session.save_page50 path = File.expand_path(Dir.glob('capybara-*.html').first, Dir.pwd)51 expect(result).to eq(path)52 end53 it 'returns an absolute path in given directory' do54 Capybara.save_path = alternative_path55 result = @session.save_page56 path = File.expand_path(Dir.glob(alternative_path + '/capybara-*.html').first, alternative_path)57 expect(result).to eq(path)58 end59 context 'asset_host contains a string' do60 before { Capybara.asset_host = 'http://example.com' }61 after { Capybara.asset_host = nil }62 it 'prepends base tag with value from asset_host to the head' do63 @session.visit('/with_js')64 path = @session.save_page65 result = File.read(path)66 expect(result).to include("<head><base href='http://example.com' />")67 end68 it "doesn't prepend base tag to pages when asset_host is nil" do69 Capybara.asset_host = nil70 @session.visit('/with_js')71 path = @session.save_page72 result = File.read(path)73 expect(result).to include('<html')74 expect(result).not_to include('http://example.com')75 end76 it "doesn't prepend base tag to pages which already have it" do77 @session.visit('/with_base_tag')78 path = @session.save_page79 result = File.read(path)80 expect(result).to include('<html')81 expect(result).not_to include('http://example.com')82 end83 it 'executes successfully even if the page is missing a <head>' do84 @session.visit('/with_simple_html')85 path = @session.save_page86 result = File.read(path)87 expect(result).to include('Bar')88 end89 end90end...

Full Screen

Full Screen

html

Using AI Code Generation

copy

Full Screen

1 visit('/')2<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">3<script>(function(){window.google={kEI:'5DkzWJnZJY6Z0gS9z7OoCw',kEXPI:'0,1352948,1352952,1352953,1352954,1352955,1352956,1352957,1352958,1352959,1352960,1352961,1352962,1352963,1352964,1352965,1352966,1352967,1352968,1352969,1352970,1352971,1352972,1352973,1352974,1352975,1352976,1352977,1352978,1352979,1352980,1352981,1352982,1352983,1352984,1352985,1352986,1352987,1352988,1352989,1352990,1352991,1352992,1352993,1352994,1352995,1352996,1352997,1352998,1352999,1353000,1353001,1353002,1353003,1353004,1353005,1353006,1353007,1353008,1353009,1353010

Full Screen

Full Screen

html

Using AI Code Generation

copy

Full Screen

1 def search_for(query)2 visit('/')3google.search_for('Capybara')

Full Screen

Full Screen

html

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', :with => 'Capybara')3click_button('Google Search')4visit('/')5fill_in('q', :with => 'Capybara')6click_button('Google Search')7visit('/')8fill_in('q', :with => 'Capybara')9click_button('Google Search')

Full Screen

Full Screen

html

Using AI Code Generation

copy

Full Screen

1visit('/')2find(:css, 'input[title="Search"]').set('Ruby')3click_button('Google Search')4all(:link, 'Ruby').each do |link|5all(:link, 'Ruby').each do |link|6find(:css, 'input[title="Search"]').set('Selenium')7click_button('Google Search')8all(:link, 'Selenium').each do |link|9all(:link, 'Selenium').each do |link|10find(:css, 'input[title="Search"]').set('Capybara')11click_button('Google Search')

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