Best Capybara code snippet using Capybara.for
capybara.rb
Source:capybara.rb
...36 #37 # [app_host = String] The default host to use when giving a relative URL to visit 38 # [always_include_port = Boolean] Whether the Rack server's port should automatically be inserted into every visited URL (Default: false) 39 # [asset_host = String] Where dynamic assets are hosted - will be prepended to relative asset locations if present (Default: nil) 40 # [run_server = Boolean] Whether to start a Rack server for the given Rack app (Default: true) 41 # [raise_server_errors = Boolean] Should errors raised in the server be raised in the tests? (Default: true) 42 # [server_errors = Array\<Class\>] Error classes that should be raised in the tests if they are raised in the server and Capybara.raise_server_errors is true (Default: [StandardError]) 43 # [default_selector = :css/:xpath] Methods which take a selector use the given type by default (Default: :css) 44 # [default_max_wait_time = Numeric] The maximum number of seconds to wait for asynchronous processes to finish (Default: 2) 45 # [ignore_hidden_elements = Boolean] Whether to ignore hidden elements on the page (Default: true) 46 # [automatic_reload = Boolean] Whether to automatically reload elements as Capybara is waiting (Default: true) 47 # [save_and_open_page_path = String] Where to put pages saved through save_and_open_page (Default: Dir.pwd) 48 # [wait_on_first_by_default = Boolean] Whether Node#first defaults to Capybara waiting behavior for at least 1 element to match (Default: false) 49 # === DSL Options50 #51 # when using capybara/dsl, the following options are also available:52 #53 # [default_driver = Symbol] The name of the driver to use by default. (Default: :rack_test) 54 # [javascript_driver = Symbol] The name of a driver to use for JavaScript enabled tests. (Default: :selenium) 55 #56 def configure57 yield self58 end59 ##60 #61 # Register a new driver for Capybara.62 #63 # Capybara.register_driver :rack_test do |app|64 # Capybara::RackTest::Driver.new(app)65 # end66 #67 # @param [Symbol] name The name of the new driver68 # @yield [app] This block takes a rack app and returns a Capybara driver69 # @yieldparam [<Rack>] app The rack application that this driver runs against. May be nil.70 # @yieldreturn [Capybara::Driver::Base] A Capybara driver instance71 #72 def register_driver(name, &block)73 drivers[name] = block74 end75 ##76 #77 # Add a new selector to Capybara. Selectors can be used by various methods in Capybara78 # to find certain elements on the page in a more convenient way. For example adding a79 # selector to find certain table rows might look like this:80 #81 # Capybara.add_selector(:row) do82 # xpath { |num| ".//tbody/tr[#{num}]" }83 # end84 #85 # This makes it possible to use this selector in a variety of ways:86 #87 # find(:row, 3)88 # page.find('table#myTable').find(:row, 3).text89 # page.find('table#myTable').has_selector?(:row, 3)90 # within(:row, 3) { expect(page).to have_content('$100.000') }91 #92 # Here is another example:93 #94 # Capybara.add_selector(:id) do95 # xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }96 # end97 #98 # Note that this particular selector already ships with Capybara.99 #100 # @param [Symbol] name The name of the selector to add101 # @yield A block executed in the context of the new {Capybara::Selector}102 #103 def add_selector(name, &block)104 Capybara::Selector.add(name, &block)105 end106 def drivers107 @drivers ||= {}108 end109 ##110 #111 # Register a proc that Capybara will call to run the Rack application.112 #113 # Capybara.server do |app, port|114 # require 'rack/handler/mongrel'115 # Rack::Handler::Mongrel.run(app, :Port => port)116 # end117 #118 # By default, Capybara will try to run webrick.119 #120 # @yield [app, port] This block receives a rack app and port and should run a Rack handler121 #122 def server(&block)123 if block_given?124 @server = block125 else126 @server127 end128 end129 ##130 #131 # Wraps the given string, which should contain an HTML document or fragment132 # in a {Capybara::Node::Simple} which exposes all {Capybara::Node::Matchers},133 # {Capybara::Node::Finders} and {Capybara::Node::DocumentMatchers}. This allows you to query134 # any string containing HTML in the exact same way you would query the current document in a Capybara135 # session. For example:136 #137 # node = Capybara.string <<-HTML138 # <ul>139 # <li id="home">Home</li>140 # <li id="projects">Projects</li>141 # </ul>142 # HTML143 #144 # node.find('#projects').text # => 'Projects'145 # node.has_selector?('li#home', :text => 'Home')146 # node.has_selector?('#projects')147 # node.find('ul').find('li:first-child').text # => 'Home'148 #149 # @param [String] html An html fragment or document150 # @return [Capybara::Node::Simple] A node which has Capybara's finders and matchers151 #152 def string(html)153 Capybara::Node::Simple.new(html)154 end155 ##156 #157 # Runs Capybara's default server for the given application and port158 # under most circumstances you should not have to call this method159 # manually.160 #161 # @param [Rack Application] app The rack application to run162 # @param [Fixnum] port The port to run the application on163 #164 def run_default_server(app, port)165 require 'rack/handler/webrick'166 Rack::Handler::WEBrick.run(app, :Host => server_host, :Port => port, :AccessLog => [], :Logger => WEBrick::Log::new(nil, 0))167 end168 ##169 #170 # @return [Symbol] The name of the driver to use by default171 #172 def default_driver173 @default_driver || :rack_test174 end175 ##176 #177 # @return [Symbol] The name of the driver currently in use178 #179 def current_driver180 @current_driver || default_driver181 end182 alias_method :mode, :current_driver183 ##184 #185 # @return [Symbol] The name of the driver used when JavaScript is needed186 #187 def javascript_driver188 @javascript_driver || :selenium189 end190 ##191 #192 # Use the default driver as the current driver193 #194 def use_default_driver195 @current_driver = nil196 end197 ##198 #199 # Yield a block using a specific driver200 #201 def using_driver(driver)202 previous_driver = Capybara.current_driver203 Capybara.current_driver = driver204 yield205 ensure206 @current_driver = previous_driver207 end208 ##209 #210 # @return [String] The IP address bound by default server211 #212 def server_host213 @server_host || '127.0.0.1'214 end215 ##216 #217 # Yield a block using a specific wait time218 #219 def using_wait_time(seconds)220 previous_wait_time = Capybara.default_max_wait_time221 Capybara.default_max_wait_time = seconds222 yield223 ensure224 Capybara.default_max_wait_time = previous_wait_time225 end226 ##227 #228 # The current Capybara::Session based on what is set as Capybara.app and Capybara.current_driver229 #230 # @return [Capybara::Session] The currently used session231 #232 def current_session233 session_pool["#{current_driver}:#{session_name}:#{app.object_id}"] ||= Capybara::Session.new(current_driver, app)234 end235 ##236 #237 # Reset sessions, cleaning out the pool of sessions. This will remove any session information such238 # as cookies.239 #240 def reset_sessions!241 session_pool.each { |mode, session| session.reset! }242 end243 alias_method :reset!, :reset_sessions!244 ##245 #246 # The current session name.247 #248 # @return [Symbol] The name of the currently used session.249 #250 def session_name251 @session_name ||= :default252 end253 ##254 #255 # Yield a block using a specific session name.256 #257 def using_session(name)258 previous_session_name = self.session_name259 self.session_name = name260 yield261 ensure262 self.session_name = previous_session_name263 end264 ##265 #266 # Parse raw html into a document using Nokogiri, and adjust textarea contents as defined by the spec.267 #268 # @param [String] html The raw html269 # @return [Nokogiri::HTML::Document] HTML document270 #271 def HTML(html)272 Nokogiri::HTML(html).tap do |document|273 document.xpath('//textarea').each do |textarea|274 textarea.content=textarea.content.sub(/\A\n/,'')275 end276 end277 end278 279 # @deprecated Use default_max_wait_time instead280 def default_wait_time281 deprecate('default_wait_time', 'default_max_wait_time', true)282 default_max_wait_time283 end284 285 # @deprecated Use default_max_wait_time= instead286 def default_wait_time=(t)287 deprecate('default_wait_time=', 'default_max_wait_time=')288 self.default_max_wait_time = t289 end290 def included(base)291 base.send(:include, Capybara::DSL)292 warn "`include Capybara` is deprecated. Please use `include Capybara::DSL` instead."293 end294 def deprecate(method, alternate_method, once=false)295 @deprecation_notified ||= {}296 warn "DEPRECATED: ##{method} is deprecated, please use ##{alternate_method} instead" unless once and @deprecation_notified[method]297 @deprecation_notified[method]=true298 end299 private300 def session_pool301 @session_pool ||= {}302 end303 end304 self.default_driver = nil305 self.current_driver = nil306 self.server_host = nil307 module Driver; end308 module RackTest; end309 module Selenium; end310 require 'capybara/helpers'311 require 'capybara/session'312 require 'capybara/dsl'313 require 'capybara/window'314 require 'capybara/server'315 require 'capybara/selector'316 require 'capybara/result'317 require 'capybara/version'318 require 'capybara/queries/base_query'319 require 'capybara/query'320 require 'capybara/queries/text_query'321 require 'capybara/queries/title_query'322 require 'capybara/queries/current_path_query'323 324 require 'capybara/node/finders'325 require 'capybara/node/matchers'326 require 'capybara/node/actions'327 require 'capybara/node/document_matchers'328 require 'capybara/node/simple'329 require 'capybara/node/base'330 require 'capybara/node/element'331 require 'capybara/node/document'332 require 'capybara/driver/base'333 require 'capybara/driver/node'334 require 'capybara/rack_test/driver'335 require 'capybara/rack_test/node'336 require 'capybara/rack_test/form'337 require 'capybara/rack_test/browser'338 require 'capybara/rack_test/css_handlers.rb'339 require 'capybara/selenium/node'340 require 'capybara/selenium/driver'341end342Capybara.configure do |config|343 config.always_include_port = false344 config.run_server = true345 config.server {|app, port| Capybara.run_default_server(app, port)}346 config.default_selector = :css347 config.default_max_wait_time = 2348 config.ignore_hidden_elements = true349 config.default_host = "http://www.example.com"350 config.automatic_reload = true...
grep_plus_offset.rb
Source:grep_plus_offset.rb
...8#++9require 'facets/enumerable/map_send'10require_relative 'grep_indexes'11module Enumerable12 # For each element that matches +regexp+ return the element that is +offset+ elements forward.13 #14 # Examples:15 #16 # %w[1 2 3].grep_plus_offset(/1/, -1) => [nil]17 # %w[1 2 3].grep_plus_offset(/1/, 0) => ['1']18 # %w[1 2 3].grep_plus_offset(/1/, 1) => ['2']19 # %w[1 2 3].grep_plus_offset(/1/, 2) => ['3']20 # %w[1 2 3].grep_plus_offset(/1/, 3) => [nil]21 #22 # caller(0).grep_plus_offset(/in `synchronize'/, 1) => the line that *called* synchronize23 #24 def grep_plus_offset(regexp, offset, wrap_around = false)25 indexes = grep_indexes(regexp).map_send(:+, offset)26 # If any indexes are negative, replace with (maximum index + 1) so that values_at will return27 # nil for that element (instead of returning an element from the end -- values_at(-1) returns28 # the last element, for example), the same as how providing a positive that results in an offset29 # > maximum_index (length - 1) results in a nil being returned for that index.30 indexes.map! {|_| _ < 0 ? length : _ } unless wrap_around31 values_at *indexes32 end33end34# _____ _35# |_ _|__ ___| |_36# | |/ _ \/ __| __|37# | | __/\__ \ |_38# |_|\___||___/\__|39#40=begin test41require 'rspec/autorun'42describe 'Enumerable#grep_indexes' do43 describe %w[1 2 3] do...
for
Using AI Code Generation
1viit("/")2page.ave_screenshot("screenshot.png")3capybara (2.0.2)4capybara-webkit (0.11.2)5phantomjs (
for
Using AI Code Generation
1 def self.visit(url)2Capybara.visit('http://www.google.com')3Capybara.visit('http://www.google.com')4Capybara.visit('http://www.google.com')5 def self.visit(url)6Capybara.visit('http://www.google.com')7 dif self.visit(url)le_phantomjs)8 def self.visit(url)9Casybars.visit('http://www.gooilo.com')10requir( 'capybara/dsl'
for
Using AI Code Generation
1Capybara.visit('/')2Capybara.visit('/')3Capybara.visit('/')4Capybara.visit('/')
for
Using AI Code Generation
1 def self.visit(url)2Capybara.visit('http://www.google.com')3Capybara.visit('http://www.google.com')4 def self.visit(url)5Capybara.visit('http://www.google.com')6 def self.visit(url)7Capybara.visit('http://www.google.com')8 def self.visit(url)9Capybara.visit('http://www.google.com')10 def self.visit(url)11Capybara.visit('http://www.google.com')12 def self.visit(url)
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!!