How to use builder method of Capybara Package

Best Capybara code snippet using Capybara.builder

poltergeist_phantomjs_builder.rb

Source:poltergeist_phantomjs_builder.rb Github

copy

Full Screen

1require 'capybara'2require 'capybara/poltergeist'3require_relative '../capybara_configuration'4require_relative '../capybara_ext/poltergeist/driver'5require_relative '../capybara_ext/session'6module Kimurai::BrowserBuilder7 class PoltergeistPhantomjsBuilder8 attr_reader :logger, :spider9 def initialize(config, spider:)10 @config = config11 @spider = spider12 @logger = spider.logger13 end14 def build15 # Register driver16 Capybara.register_driver :poltergeist_phantomjs do |app|17 # Create driver options18 driver_options = {19 js_errors: false, debug: false, inspector: false, phantomjs_options: []20 }21 if extensions = @config[:extensions].presence22 driver_options[:extensions] = extensions23 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled extensions"24 end25 # Window size26 if size = @config[:window_size].presence27 driver_options[:window_size] = size28 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled window_size"29 end30 # SSL31 if ssl_cert_path = @config[:ssl_cert_path].presence32 driver_options[:phantomjs_options] << "--ssl-certificates-path=#{ssl_cert_path}"33 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled custom ssl_cert"34 end35 if @config[:ignore_ssl_errors].present?36 driver_options[:phantomjs_options].push("--ignore-ssl-errors=yes", "--ssl-protocol=any")37 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled ignore_ssl_errors"38 end39 # Disable images40 if @config[:disable_images].present?41 driver_options[:phantomjs_options] << "--load-images=no"42 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled disable_images"43 end44 Capybara::Poltergeist::Driver.new(app, driver_options)45 end46 # Create browser instance (Capybara session)47 @browser = Capybara::Session.new(:poltergeist_phantomjs)48 @browser.spider = spider49 logger.debug "BrowserBuilder (poltergeist_phantomjs): created browser instance"50 # Proxy51 if proxy = @config[:proxy].presence52 proxy_string = (proxy.class == Proc ? proxy.call : proxy).strip53 ip, port, type = proxy_string.split(":")54 if %w(http socks5).include?(type)55 @browser.driver.set_proxy(*proxy_string.split(":"))56 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled #{type} proxy, ip: #{ip}, port: #{port}"57 else58 logger.error "BrowserBuilder (poltergeist_phantomjs): wrong type of proxy: #{type}, skipped"59 end60 end61 # Headers62 if headers = @config[:headers].presence63 @browser.driver.headers = headers64 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled custom headers"65 end66 if user_agent = @config[:user_agent].presence67 user_agent_string = (user_agent.class == Proc ? user_agent.call : user_agent).strip68 @browser.driver.add_header("User-Agent", user_agent_string)69 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled custom user_agent"70 end71 # Cookies72 if cookies = @config[:cookies].presence73 cookies.each do |cookie|74 @browser.driver.set_cookie(cookie[:name], cookie[:value], cookie)75 end76 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled custom cookies"77 end78 # Browser instance options79 # skip_request_errors80 if skip_errors = @config[:skip_request_errors].presence81 @browser.config.skip_request_errors = skip_errors82 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled skip_request_errors"83 end84 # retry_request_errors85 if retry_errors = @config[:retry_request_errors].presence86 @browser.config.retry_request_errors = retry_errors87 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled retry_request_errors"88 end89 # restart_if90 if requests_limit = @config.dig(:restart_if, :requests_limit).presence91 @browser.config.restart_if[:requests_limit] = requests_limit92 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled restart_if.requests_limit >= #{requests_limit}"93 end94 if memory_limit = @config.dig(:restart_if, :memory_limit).presence95 @browser.config.restart_if[:memory_limit] = memory_limit96 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled restart_if.memory_limit >= #{memory_limit}"97 end98 # before_request clear_cookies99 if @config.dig(:before_request, :clear_cookies)100 @browser.config.before_request[:clear_cookies] = true101 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled before_request.clear_cookies"102 end103 # before_request clear_and_set_cookies104 if @config.dig(:before_request, :clear_and_set_cookies)105 if cookies = @config[:cookies].presence106 @browser.config.cookies = cookies107 @browser.config.before_request[:clear_and_set_cookies] = true108 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled before_request.clear_and_set_cookies"109 else110 logger.error "BrowserBuilder (poltergeist_phantomjs): cookies should be present to enable before_request.clear_and_set_cookies, skipped"111 end112 end113 # before_request change_user_agent114 if @config.dig(:before_request, :change_user_agent)115 if @config[:user_agent].present? && @config[:user_agent].class == Proc116 @browser.config.user_agent = @config[:user_agent]117 @browser.config.before_request[:change_user_agent] = true118 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled before_request.change_user_agent"119 else120 logger.error "BrowserBuilder (poltergeist_phantomjs): user_agent should be present and has lambda format to enable before_request.change_user_agent, skipped"121 end122 end123 # before_request change_proxy124 if @config.dig(:before_request, :change_proxy)125 if @config[:proxy].present? && @config[:proxy].class == Proc126 @browser.config.proxy = @config[:proxy]127 @browser.config.before_request[:change_proxy] = true128 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled before_request.change_proxy"129 else130 logger.error "BrowserBuilder (poltergeist_phantomjs): proxy should be present and has lambda format to enable before_request.change_proxy, skipped"131 end132 end133 # before_request delay134 if delay = @config.dig(:before_request, :delay).presence135 @browser.config.before_request[:delay] = delay136 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled before_request.delay"137 end138 # encoding139 if encoding = @config[:encoding]140 @browser.config.encoding = encoding141 logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled encoding: #{encoding}"142 end143 # return Capybara session instance144 @browser145 end146 end147end...

Full Screen

Full Screen

mechanize_builder.rb

Source:mechanize_builder.rb Github

copy

Full Screen

1require 'capybara'2require 'capybara/mechanize'3require_relative '../capybara_configuration'4require_relative '../capybara_ext/mechanize/driver'5require_relative '../capybara_ext/session'6module Kimurai::BrowserBuilder7 class MechanizeBuilder8 attr_reader :logger, :spider9 def initialize(config, spider:)10 @config = config11 @spider = spider12 @logger = spider.logger13 end14 def build15 # Register driver16 Capybara.register_driver :mechanize do |app|17 driver = Capybara::Mechanize::Driver.new("app")18 # keep the history as small as possible (by default it's unlimited)19 driver.configure { |a| a.history.max_size = 2 }20 driver21 end22 # Create browser instance (Capybara session)23 @browser = Capybara::Session.new(:mechanize)24 @browser.spider = spider25 logger.debug "BrowserBuilder (mechanize): created browser instance"26 if @config[:extensions].present?27 logger.error "BrowserBuilder (mechanize): `extensions` option not supported, skipped"28 end29 # Proxy30 if proxy = @config[:proxy].presence31 proxy_string = (proxy.class == Proc ? proxy.call : proxy).strip32 ip, port, type = proxy_string.split(":")33 if type == "http"34 @browser.driver.set_proxy(*proxy_string.split(":"))35 logger.debug "BrowserBuilder (mechanize): enabled http proxy, ip: #{ip}, port: #{port}"36 else37 logger.error "BrowserBuilder (mechanize): can't set #{type} proxy (not supported), skipped"38 end39 end40 # SSL41 if ssl_cert_path = @config[:ssl_cert_path].presence42 @browser.driver.browser.agent.http.ca_file = ssl_cert_path43 logger.debug "BrowserBuilder (mechanize): enabled custom ssl_cert"44 end45 if @config[:ignore_ssl_errors].present?46 @browser.driver.browser.agent.verify_mode = OpenSSL::SSL::VERIFY_NONE47 logger.debug "BrowserBuilder (mechanize): enabled ignore_ssl_errors"48 end49 # Headers50 if headers = @config[:headers].presence51 @browser.driver.headers = headers52 logger.debug "BrowserBuilder (mechanize): enabled custom headers"53 end54 if user_agent = @config[:user_agent].presence55 user_agent_string = (user_agent.class == Proc ? user_agent.call : user_agent).strip56 @browser.driver.add_header("User-Agent", user_agent_string)57 logger.debug "BrowserBuilder (mechanize): enabled custom user_agent"58 end59 # Cookies60 if cookies = @config[:cookies].presence61 cookies.each do |cookie|62 @browser.driver.set_cookie(cookie[:name], cookie[:value], cookie)63 end64 logger.debug "BrowserBuilder (mechanize): enabled custom cookies"65 end66 # Browser instance options67 # skip_request_errors68 if skip_errors = @config[:skip_request_errors].presence69 @browser.config.skip_request_errors = skip_errors70 logger.debug "BrowserBuilder (mechanize): enabled skip_request_errors"71 end72 # retry_request_errors73 if retry_errors = @config[:retry_request_errors].presence74 @browser.config.retry_request_errors = retry_errors75 logger.debug "BrowserBuilder (mechanize): enabled retry_request_errors"76 end77 # restart_if78 if @config[:restart_if].present?79 logger.warn "BrowserBuilder (mechanize): restart_if options not supported by Mechanize, skipped"80 end81 # before_request clear_cookies82 if @config.dig(:before_request, :clear_cookies)83 @browser.config.before_request[:clear_cookies] = true84 logger.debug "BrowserBuilder (mechanize): enabled before_request.clear_cookies"85 end86 # before_request clear_and_set_cookies87 if @config.dig(:before_request, :clear_and_set_cookies)88 if cookies = @config[:cookies].presence89 @browser.config.cookies = cookies90 @browser.config.before_request[:clear_and_set_cookies] = true91 logger.debug "BrowserBuilder (mechanize): enabled before_request.clear_and_set_cookies"92 else93 logger.error "BrowserBuilder (mechanize): cookies should be present to enable before_request.clear_and_set_cookies, skipped"94 end95 end96 # before_request change_user_agent97 if @config.dig(:before_request, :change_user_agent)98 if @config[:user_agent].present? && @config[:user_agent].class == Proc99 @browser.config.user_agent = @config[:user_agent]100 @browser.config.before_request[:change_user_agent] = true101 logger.debug "BrowserBuilder (mechanize): enabled before_request.change_user_agent"102 else103 logger.error "BrowserBuilder (mechanize): user_agent should be present and has lambda format to enable before_request.change_user_agent, skipped"104 end105 end106 # before_request change_proxy107 if @config.dig(:before_request, :change_proxy)108 if @config[:proxy].present? && @config[:proxy].class == Proc109 @browser.config.proxy = @config[:proxy]110 @browser.config.before_request[:change_proxy] = true111 logger.debug "BrowserBuilder (mechanize): enabled before_request.change_proxy"112 else113 logger.error "BrowserBuilder (mechanize): proxy should be present and has lambda format to enable before_request.change_proxy, skipped"114 end115 end116 # before_request delay117 if delay = @config.dig(:before_request, :delay).presence118 @browser.config.before_request[:delay] = delay119 logger.debug "BrowserBuilder (mechanize): enabled before_request.delay"120 end121 # encoding122 if encoding = @config[:encoding]123 @browser.config.encoding = encoding124 logger.debug "BrowserBuilder (mechanize): enabled encoding: #{encoding}"125 end126 # return Capybara session instance127 @browser128 end129 end130end...

Full Screen

Full Screen

Gemfile.lock

Source:Gemfile.lock Github

copy

Full Screen

...5 actionpack (= 4.0.1)6 mail (~> 2.5.4)7 actionpack (4.0.1)8 activesupport (= 4.0.1)9 builder (~> 3.1.0)10 erubis (~> 2.7.0)11 rack (~> 1.5.2)12 rack-test (~> 0.6.2)13 activemodel (4.0.1)14 activesupport (= 4.0.1)15 builder (~> 3.1.0)16 activerecord (4.0.1)17 activemodel (= 4.0.1)18 activerecord-deprecated_finders (~> 1.0.2)19 activesupport (= 4.0.1)20 arel (~> 4.0.0)21 activerecord-deprecated_finders (1.0.3)22 activesupport (4.0.1)23 i18n (~> 0.6, >= 0.6.4)24 minitest (~> 4.2)25 multi_json (~> 1.3)26 thread_safe (~> 0.1)27 tzinfo (~> 0.3.37)28 arel (4.0.1)29 atomic (1.1.14)30 better_errors (1.1.0)31 coderay (>= 1.0.0)32 erubis (>= 2.6.6)33 binding_of_caller (0.7.2)34 debug_inspector (>= 0.0.1)35 builder (3.1.4)36 capybara (2.1.0)37 mime-types (>= 1.16)38 nokogiri (>= 1.3.3)39 rack (>= 1.0.0)40 rack-test (>= 0.5.4)41 xpath (~> 2.0)42 coderay (1.1.0)43 coffee-rails (4.0.1)44 coffee-script (>= 2.2.0)45 railties (>= 4.0.0, < 5.0)46 coffee-script (2.2.0)47 coffee-script-source48 execjs49 coffee-script-source (1.7.0)50 daemons (1.1.9)51 database_cleaner (1.2.0)52 debug_inspector (0.0.2)53 docile (1.1.2)54 email_validator (1.4.0)55 activemodel56 erubis (2.7.0)57 eventmachine (1.0.3)58 execjs (2.0.2)59 hike (1.2.3)60 i18n (0.6.9)61 jbuilder (1.5.3)62 activesupport (>= 3.0.0)63 multi_json (>= 1.2.0)64 jquery-rails (3.1.0)65 railties (>= 3.0, < 5.0)66 thor (>= 0.14, < 2.0)67 json (1.8.1)68 mail (2.5.4)69 mime-types (~> 1.16)70 treetop (~> 1.4.8)71 mime-types (1.25.1)72 mini_portile (0.5.2)73 minitest (4.7.5)74 minitest-capybara (0.5.0)75 capybara (~> 2.1.0)76 minitest (~> 4.0)77 rake78 minitest-metadata (0.4.0)79 minitest (~> 4.7)80 minitest-rails (0.9.2)81 minitest (~> 4.7)82 rails (>= 3.0)83 minitest-rails-capybara (0.10.0)84 capybara (~> 2.0)85 minitest-capybara (~> 0.4)86 minitest-metadata (~> 0.4)87 minitest-rails (~> 0.9.1)88 multi_json (1.8.4)89 nokogiri (1.6.1)90 mini_portile (~> 0.5.0)91 polyglot (0.3.3)92 rack (1.5.2)93 rack-test (0.6.2)94 rack (>= 1.0)95 rails (4.0.1)96 actionmailer (= 4.0.1)97 actionpack (= 4.0.1)98 activerecord (= 4.0.1)99 activesupport (= 4.0.1)100 bundler (>= 1.3.0, < 2.0)101 railties (= 4.0.1)102 sprockets-rails (~> 2.0.0)103 railties (4.0.1)104 actionpack (= 4.0.1)105 activesupport (= 4.0.1)106 rake (>= 0.8.7)107 thor (>= 0.18.1, < 2.0)108 rake (10.1.1)109 rdoc (4.1.1)110 json (~> 1.4)111 sass (3.2.14)112 sass-rails (4.0.1)113 railties (>= 4.0.0, < 5.0)114 sass (>= 3.1.10)115 sprockets-rails (~> 2.0.0)116 sdoc (0.4.0)117 json (~> 1.8)118 rdoc (~> 4.0, < 5.0)119 shoulda (3.5.0)120 shoulda-context (~> 1.0, >= 1.0.1)121 shoulda-matchers (>= 1.4.1, < 3.0)122 shoulda-context (1.1.6)123 shoulda-matchers (2.5.0)124 activesupport (>= 3.0.0)125 simplecov (0.8.2)126 docile (~> 1.1.0)127 multi_json128 simplecov-html (~> 0.8.0)129 simplecov-html (0.8.0)130 sprockets (2.10.1)131 hike (~> 1.2)132 multi_json (~> 1.0)133 rack (~> 1.0)134 tilt (~> 1.1, != 1.3.0)135 sprockets-rails (2.0.1)136 actionpack (>= 3.0)137 activesupport (>= 3.0)138 sprockets (~> 2.8)139 sqlite3 (1.3.8)140 thin (1.6.1)141 daemons (>= 1.0.9)142 eventmachine (>= 1.0.0)143 rack (>= 1.0.0)144 thor (0.18.1)145 thread_safe (0.1.3)146 atomic147 tilt (1.4.1)148 treetop (1.4.15)149 polyglot150 polyglot (>= 0.3.1)151 turbolinks (2.2.0)152 coffee-rails153 tzinfo (0.3.38)154 uglifier (2.4.0)155 execjs (>= 0.3.0)156 json (>= 1.8.0)157 xpath (2.0.0)158 nokogiri (~> 1.3)159PLATFORMS160 ruby161DEPENDENCIES162 better_errors163 binding_of_caller164 coffee-rails (~> 4.0.0)165 database_cleaner166 email_validator167 jbuilder (~> 1.2)168 jquery-rails169 minitest-rails170 minitest-rails-capybara171 rails (= 4.0.1)172 rake173 sass-rails (~> 4.0.0)174 sdoc175 shoulda176 shoulda-matchers177 simplecov178 sqlite3179 thin180 turbolinks181 uglifier (>= 1.3.0)...

Full Screen

Full Screen

builder

Using AI Code Generation

copy

Full Screen

1 visit('/')2 fill_in('q', :with => 'capybara')3 click_button('btnG')4 visit('/')5 fill_in('q', :with => 'capybara')6 click_button('btnG')7 visit('/')8 fill_in('q', :with => 'capybara')9 click_button('btnG')10 visit('/')11 fill_in('q', :with => 'c

Full Screen

Full Screen

builder

Using AI Code Generation

copy

Full Screen

1 Capybara::Selenium::Driver.new(app, :browser => :firefox)2Capybara::Session.new(:selenium)3Capybara.visit('/')4Capybara.fill_in('q', :with => 'Capybara')5Capybara.click_button('Google Search')6Capybara.save_screenshot('google.png')7 Capybara.page.driver.browser.switch_to.window(handle)8 Capybara.save_screenshot('google.png')9Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[1])10 Capybara.page.driver.browser.switch_to.window(handle)11 Capybara.save_screenshot('google.png')12Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[0])13Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[1])14Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[0])15Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[1])16Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[0])17Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[1])18Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[0])19Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[1])20Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[0])21Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[1])22Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles[0])

Full Screen

Full Screen

builder

Using AI Code Generation

copy

Full Screen

1World(Capybara::DSL)2World(RSpec::Matchers)3World(Capybara::DSL)4World(RSpec::Matchers)5World(Capybara::DSL)6World(RSpec::Matchers)7World(Capybara::DSL)8World(RSpec::Matchers)9World(Capybara::DSL)10World(RSpec::Matchers)

Full Screen

Full Screen

builder

Using AI Code Generation

copy

Full Screen

1 def search_for(search_term)2 let(:google) { Google.new }3 expect(google.results).to include 'Capybara'4 def search_for(search_term)5 let(:google) { GooglePage.new }6 expect(google.results).to include 'Capybara'7 def search_for(search_term)8 def self.for(search_term)

Full Screen

Full Screen

builder

Using AI Code Generation

copy

Full Screen

1 visit('http://www.google.com')2 fill_in('q', :with => 'Selenium')3 click_button('Google Search')4 visit('http://www.google.com')5 fill_in('q', :with => 'Selenium')6 click_button('Google Search')7 2.rb:1:in `require': cannot load such file -- capybara (LoadError)8Your name to display (optional):9Your name to display (optional):10Your name to display (optional):

Full Screen

Full Screen

builder

Using AI Code Generation

copy

Full Screen

1session = Capybara::Session.new(:selenium)2session.visit('/')3session.fill_in('q', :with => 'capybara')4session.click_button('btnG')5session.save_screenshot('screenshot.png')6session.save_page('page.html')7session = Capybara::Session.new(:selenium)8session.visit('/')9session.fill_in('q', :with => 'capybara')10session.click_button('btnG')11session.save_screenshot('screenshot.png')12session.save_page('page.html')13session = Capybara::Session.new(:selenium)14session.visit('/')15session.fill_in('q', :with => 'capybara')16session.click_button('btnG')17session.save_screenshot('screenshot.png')18session.save_page('page.html')19session = Capybara::Session.new(:selenium)20session.visit('/')21session.fill_in('q', :with => 'capybara')22session.click_button('btnG')23session.save_screenshot('screenshot.png')24session.save_page('page.html')

Full Screen

Full Screen

builder

Using AI Code Generation

copy

Full Screen

1 let(:google) { Google.new }2 expect(google.results).to include 'Capybara'3 def search_for(search_term)4 let(:google) { GooglePage.new }5 expect(google.results).to include 'Capybara'6 def search_for(search_term)7 def self.for(search_term)

Full Screen

Full Screen

builder

Using AI Code Generation

copy

Full Screen

1session = Capybara::Session.new(:selenium)2session.visit('/')3session.fill_in('q', :with => 'capybara')4session.click_button('btnG')5session.save_screenshot('screenshot.png')6session.save_page('page.html')7session = Capybara::Session.new(:selenium)8session.visit('/')9session.fill_in('q', :with => 'capybara')10session.click_button('btnG')11session.save_screenshot('screenshot.png')12session.save_page('page.html')13session = Capybara::Session.new(:selenium)14session.visit('/')15session.fill_in('q', :with => 'capybara')16session.click_button('btnG')17session.save_screenshot('screenshot.png')18session.save_page('page.html')19session = Capybara::Session.new(:selenium)20session.visit('/')21session.fill_in('q', :with => 'capybara')22session.click_button('btnG')23session.save_screenshot('screenshot.png')24session.save_page('page.html')

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