Best Capybara code snippet using Capybara.Node.text
matchers.rb
Source:matchers.rb
...16 #17 # This will check if the expression occurs exactly 4 times.18 #19 # It also accepts all options that {Capybara::Node::Finders#all} accepts,20 # such as :text and :visible.21 #22 # page.has_selector?('li', :text => 'Horse', :visible => true)23 #24 # has_selector? can also accept XPath expressions generated by the25 # XPath gem:26 #27 # page.has_selector?(:xpath, XPath.descendant(:p))28 #29 # @param (see Capybara::Node::Finders#all)30 # @param args31 # @option args [Integer] :count (nil) Number of times the text should occur32 # @option args [Integer] :minimum (nil) Minimum number of times the text should occur33 # @option args [Integer] :maximum (nil) Maximum number of times the text should occur34 # @option args [Range] :between (nil) Range of times that should contain number of times text occurs35 # @return [Boolean] If the expression exists36 #37 def has_selector?(*args)38 assert_selector(*args)39 rescue Capybara::ExpectationNotMet40 return false41 end42 ##43 #44 # Checks if a given selector is not on the page or current node.45 # Usage is identical to Capybara::Node::Matchers#has_selector?46 #47 # @param (see Capybara::Node::Finders#has_selector?)48 # @return [Boolean]49 #50 def has_no_selector?(*args)51 assert_no_selector(*args)52 rescue Capybara::ExpectationNotMet53 return false54 end55 ##56 #57 # Asserts that a given selector is on the page or current node.58 #59 # page.assert_selector('p#foo')60 # page.assert_selector(:xpath, './/p[@id="foo"]')61 # page.assert_selector(:foo)62 #63 # By default it will check if the expression occurs at least once,64 # but a different number can be specified.65 #66 # page.assert_selector('p#foo', :count => 4)67 #68 # This will check if the expression occurs exactly 4 times. See69 # {Capybara::Node::Finders#all} for other available result size options.70 #71 # If a :count of 0 is specified, it will behave like {#assert_no_selector};72 # however, use of that method is preferred over this one.73 #74 # It also accepts all options that {Capybara::Node::Finders#all} accepts,75 # such as :text and :visible.76 #77 # page.assert_selector('li', :text => 'Horse', :visible => true)78 #79 # `assert_selector` can also accept XPath expressions generated by the80 # XPath gem:81 #82 # page.assert_selector(:xpath, XPath.descendant(:p))83 #84 # @param (see Capybara::Node::Finders#all)85 # @option options [Integer] :count (nil) Number of times the expression should occur86 # @raise [Capybara::ExpectationNotMet] If the selector does not exist87 #88 def assert_selector(*args)89 query = Capybara::Query.new(*args)90 synchronize(query.wait) do91 result = query.resolve_for(self)92 matches_count = Capybara::Helpers.matches_count?(result.size, query.options)93 unless matches_count && ((result.size > 0) || Capybara::Helpers.expects_none?(query.options))94 raise Capybara::ExpectationNotMet, result.failure_message95 end96 end97 return true98 end99 ##100 #101 # Asserts that a given selector is not on the page or current node.102 # Usage is identical to Capybara::Node::Matchers#assert_selector103 #104 # Query options such as :count, :minimum, :maximum, and :between are105 # considered to be an integral part of the selector. This will return106 # true, for example, if a page contains 4 anchors but the query expects 5:107 #108 # page.assert_no_selector('a', :minimum => 1) # Found, raises Capybara::ExpectationNotMet109 # page.assert_no_selector('a', :count => 4) # Found, raises Capybara::ExpectationNotMet110 # page.assert_no_selector('a', :count => 5) # Not Found, returns true111 #112 # @param (see Capybara::Node::Finders#assert_selector)113 # @raise [Capybara::ExpectationNotMet] If the selector exists114 #115 def assert_no_selector(*args)116 query = Capybara::Query.new(*args)117 synchronize(query.wait) do118 result = query.resolve_for(self)119 matches_count = Capybara::Helpers.matches_count?(result.size, query.options)120 if matches_count && ((result.size > 0) || Capybara::Helpers.expects_none?(query.options))121 raise Capybara::ExpectationNotMet, result.negative_failure_message122 end123 end124 return true125 end126 alias_method :refute_selector, :assert_no_selector127 ##128 #129 # Checks if a given XPath expression is on the page or current node.130 #131 # page.has_xpath?('.//p[@id="foo"]')132 #133 # By default it will check if the expression occurs at least once,134 # but a different number can be specified.135 #136 # page.has_xpath?('.//p[@id="foo"]', :count => 4)137 #138 # This will check if the expression occurs exactly 4 times.139 #140 # It also accepts all options that {Capybara::Node::Finders#all} accepts,141 # such as :text and :visible.142 #143 # page.has_xpath?('.//li', :text => 'Horse', :visible => true)144 #145 # has_xpath? can also accept XPath expressions generate by the146 # XPath gem:147 #148 # xpath = XPath.generate { |x| x.descendant(:p) }149 # page.has_xpath?(xpath)150 #151 # @param [String] path An XPath expression152 # @param options (see Capybara::Node::Finders#all)153 # @option options [Integer] :count (nil) Number of times the expression should occur154 # @return [Boolean] If the expression exists155 #156 def has_xpath?(path, options={})157 has_selector?(:xpath, path, options)158 end159 ##160 #161 # Checks if a given XPath expression is not on the page or current node.162 # Usage is identical to Capybara::Node::Matchers#has_xpath?163 #164 # @param (see Capybara::Node::Finders#has_xpath?)165 # @return [Boolean]166 #167 def has_no_xpath?(path, options={})168 has_no_selector?(:xpath, path, options)169 end170 ##171 #172 # Checks if a given CSS selector is on the page or current node.173 #174 # page.has_css?('p#foo')175 #176 # By default it will check if the selector occurs at least once,177 # but a different number can be specified.178 #179 # page.has_css?('p#foo', :count => 4)180 #181 # This will check if the selector occurs exactly 4 times.182 #183 # It also accepts all options that {Capybara::Node::Finders#all} accepts,184 # such as :text and :visible.185 #186 # page.has_css?('li', :text => 'Horse', :visible => true)187 #188 # @param [String] path A CSS selector189 # @param options (see Capybara::Node::Finders#all)190 # @option options [Integer] :count (nil) Number of times the selector should occur191 # @return [Boolean] If the selector exists192 #193 def has_css?(path, options={})194 has_selector?(:css, path, options)195 end196 ##197 #198 # Checks if a given CSS selector is not on the page or current node.199 # Usage is identical to Capybara::Node::Matchers#has_css?200 #201 # @param (see Capybara::Node::Finders#has_css?)202 # @return [Boolean]203 #204 def has_no_css?(path, options={})205 has_no_selector?(:css, path, options)206 end207 ##208 #209 # Checks if the page or current node has a link with the given210 # text or id.211 #212 # @param [String] locator The text or id of a link to check for213 # @param options214 # @option options [String, Regexp] :href The value the href attribute must be215 # @return [Boolean] Whether it exists216 #217 def has_link?(locator, options={})218 has_selector?(:link, locator, options)219 end220 ##221 #222 # Checks if the page or current node has no link with the given223 # text or id.224 #225 # @param (see Capybara::Node::Finders#has_link?)226 # @return [Boolean] Whether it doesn't exist227 #228 def has_no_link?(locator, options={})229 has_no_selector?(:link, locator, options)230 end231 ##232 #233 # Checks if the page or current node has a button with the given234 # text, value or id.235 #236 # @param [String] locator The text, value or id of a button to check for237 # @return [Boolean] Whether it exists238 #239 def has_button?(locator, options={})240 has_selector?(:button, locator, options)241 end242 ##243 #244 # Checks if the page or current node has no button with the given245 # text, value or id.246 #247 # @param [String] locator The text, value or id of a button to check for248 # @return [Boolean] Whether it doesn't exist249 #250 def has_no_button?(locator, options={})251 has_no_selector?(:button, locator, options)252 end253 ##254 #255 # Checks if the page or current node has a form field with the given256 # label, name or id.257 #258 # For text fields and other textual fields, such as textareas and259 # HTML5 email/url/etc. fields, it's possible to specify a :with260 # option to specify the text the field should contain:261 #262 # page.has_field?('Name', :with => 'Jonas')263 #264 # It is also possible to filter by the field type attribute:265 #266 # page.has_field?('Email', :type => 'email')267 #268 # Note: 'textarea' and 'select' are valid type values, matching the associated tag names.269 #270 # @param [String] locator The label, name or id of a field to check for271 # @option options [String] :with The text content of the field272 # @option options [String] :type The type attribute of the field273 # @return [Boolean] Whether it exists274 #275 def has_field?(locator, options={})276 has_selector?(:field, locator, options)277 end278 ##279 #280 # Checks if the page or current node has no form field with the given281 # label, name or id. See {Capybara::Node::Matchers#has_field?}.282 #283 # @param [String] locator The label, name or id of a field to check for284 # @option options [String] :with The text content of the field285 # @option options [String] :type The type attribute of the field286 # @return [Boolean] Whether it doesn't exist287 #288 def has_no_field?(locator, options={})289 has_no_selector?(:field, locator, options)290 end291 ##292 #293 # Checks if the page or current node has a radio button or294 # checkbox with the given label, value or id, that is currently295 # checked.296 #297 # @param [String] locator The label, name or id of a checked field298 # @return [Boolean] Whether it exists299 #300 def has_checked_field?(locator, options={})301 has_selector?(:field, locator, options.merge(:checked => true))302 end303 ##304 #305 # Checks if the page or current node has no radio button or306 # checkbox with the given label, value or id, that is currently307 # checked.308 #309 # @param [String] locator The label, name or id of a checked field310 # @return [Boolean] Whether it doesn't exist311 #312 def has_no_checked_field?(locator, options={})313 has_no_selector?(:field, locator, options.merge(:checked => true))314 end315 ##316 #317 # Checks if the page or current node has a radio button or318 # checkbox with the given label, value or id, that is currently319 # unchecked.320 #321 # @param [String] locator The label, name or id of an unchecked field322 # @return [Boolean] Whether it exists323 #324 def has_unchecked_field?(locator, options={})325 has_selector?(:field, locator, options.merge(:unchecked => true))326 end327 ##328 #329 # Checks if the page or current node has no radio button or330 # checkbox with the given label, value or id, that is currently331 # unchecked.332 #333 # @param [String] locator The label, name or id of an unchecked field334 # @return [Boolean] Whether it doesn't exist335 #336 def has_no_unchecked_field?(locator, options={})337 has_no_selector?(:field, locator, options.merge(:unchecked => true))338 end339 ##340 #341 # Checks if the page or current node has a select field with the342 # given label, name or id.343 #344 # It can be specified which option should currently be selected:345 #346 # page.has_select?('Language', :selected => 'German')347 #348 # For multiple select boxes, several options may be specified:349 #350 # page.has_select?('Language', :selected => ['English', 'German'])351 #352 # It's also possible to check if the exact set of options exists for353 # this select box:354 #355 # page.has_select?('Language', :options => ['English', 'German', 'Spanish'])356 #357 # You can also check for a partial set of options:358 #359 # page.has_select?('Language', :with_options => ['English', 'German'])360 #361 # @param [String] locator The label, name or id of a select box362 # @option options [Array] :options Options which should be contained in this select box363 # @option options [Array] :with_options Partial set of options which should be contained in this select box364 # @option options [String, Array] :selected Options which should be selected365 # @return [Boolean] Whether it exists366 #367 def has_select?(locator, options={})368 has_selector?(:select, locator, options)369 end370 ##371 #372 # Checks if the page or current node has no select field with the373 # given label, name or id. See {Capybara::Node::Matchers#has_select?}.374 #375 # @param (see Capybara::Node::Matchers#has_select?)376 # @return [Boolean] Whether it doesn't exist377 #378 def has_no_select?(locator, options={})379 has_no_selector?(:select, locator, options)380 end381 ##382 #383 # Checks if the page or current node has a table with the given id384 # or caption:385 #386 # page.has_table?('People')387 #388 # @param [String] locator The id or caption of a table389 # @return [Boolean] Whether it exist390 #391 def has_table?(locator, options={})392 has_selector?(:table, locator, options)393 end394 ##395 #396 # Checks if the page or current node has no table with the given id397 # or caption. See {Capybara::Node::Matchers#has_table?}.398 #399 # @param (see Capybara::Node::Matchers#has_table?)400 # @return [Boolean] Whether it doesn't exist401 #402 def has_no_table?(locator, options={})403 has_no_selector?(:table, locator, options)404 end405 ##406 # Asserts that the page or current node has the given text content,407 # ignoring any HTML tags.408 #409 # @!macro text_query_params410 # @overload $0(type, text, options = {})411 # @param [:all, :visible] type Whether to check for only visible or all text412 # @param [String, Regexp] text The string/regexp to check for. If it's a string, text is expected to include it. If it's a regexp, text is expected to match it.413 # @option options [Integer] :count (nil) Number of times the text is expected to occur414 # @option options [Integer] :minimum (nil) Minimum number of times the text is expected to occur415 # @option options [Integer] :maximum (nil) Maximum number of times the text is expected to occur416 # @option options [Range] :between (nil) Range of times that is expected to contain number of times text occurs417 # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for text to eq/match given string/regexp argument418 # @overload $0(text, options = {})419 # @param [String, Regexp] text The string/regexp to check for. If it's a string, text is expected to include it. If it's a regexp, text is expected to match it.420 # @option options [Integer] :count (nil) Number of times the text is expected to occur421 # @option options [Integer] :minimum (nil) Minimum number of times the text is expected to occur422 # @option options [Integer] :maximum (nil) Maximum number of times the text is expected to occur423 # @option options [Range] :between (nil) Range of times that is expected to contain number of times text occurs424 # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for text to eq/match given string/regexp argument425 # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time426 # @return [true]427 #428 def assert_text(*args)429 query = Capybara::Queries::TextQuery.new(*args)430 synchronize(query.wait) do431 count = query.resolve_for(self)432 matches_count = Capybara::Helpers.matches_count?(count, query.options)433 unless matches_count && ((count > 0) || Capybara::Helpers.expects_none?(query.options))434 raise Capybara::ExpectationNotMet, query.failure_message435 end436 end437 return true438 end439 ##440 # Asserts that the page or current node doesn't have the given text content,441 # ignoring any HTML tags.442 #443 # @macro text_query_params444 # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time445 # @return [true]446 #447 def assert_no_text(*args)448 query = Capybara::Queries::TextQuery.new(*args)449 synchronize(query.wait) do450 count = query.resolve_for(self)451 matches_count = Capybara::Helpers.matches_count?(count, query.options)452 if matches_count && ((count > 0) || Capybara::Helpers.expects_none?(query.options))453 raise Capybara::ExpectationNotMet, query.negative_failure_message454 end455 end456 return true457 end458 ##459 # Checks if the page or current node has the given text content,460 # ignoring any HTML tags.461 #462 # Whitespaces are normalized in both node's text and passed text parameter.463 # Note that whitespace isn't normalized in passed regexp as normalizing whitespace464 # in regexp isn't easy and doesn't seem to be worth it.465 #466 # By default it will check if the text occurs at least once,467 # but a different number can be specified.468 #469 # page.has_text?('lorem ipsum', between: 2..4)470 #471 # This will check if the text occurs from 2 to 4 times.472 #473 # @macro text_query_params474 # @return [Boolean] Whether it exists475 #476 def has_text?(*args)477 assert_text(*args)478 rescue Capybara::ExpectationNotMet479 return false480 end481 alias_method :has_content?, :has_text?482 ##483 # Checks if the page or current node does not have the given text484 # content, ignoring any HTML tags and normalizing whitespace.485 #486 # @macro text_query_params487 # @return [Boolean] Whether it doesn't exist488 #489 def has_no_text?(*args)490 assert_no_text(*args)491 rescue Capybara::ExpectationNotMet492 return false493 end494 alias_method :has_no_content?, :has_no_text?495 def ==(other)496 self.eql?(other) || (other.respond_to?(:base) && base == other.base)497 end498 end499 end500end...
capybara.rb
Source:capybara.rb
...70 # - **automatic_label_click** (Boolean = `false`) - Whether {Capybara::Node::Element#choose Element#choose}, {Capybara::Node::Element#check Element#check},71 # {Capybara::Node::Element#uncheck Element#uncheck} will attempt to click the associated `<label>` element if the checkbox/radio button are non-visible.72 # - **automatic_reload** (Boolean = `true`) - Whether to automatically reload elements as Capybara is waiting.73 # - **default_max_wait_time** (Numeric = `2`) - The maximum number of seconds to wait for asynchronous processes to finish.74 # - **default_normalize_ws** (Boolean = `false`) - Whether text predicates and matchers use normalize whitespace behavior.75 # - **default_selector** (`:css`, `:xpath` = `:css`) - Methods which take a selector use the given type by default. See also {Capybara::Selector}.76 # - **default_set_options** (Hash = `{}`) - The default options passed to {Capybara::Node::Element#set Element#set}.77 # - **enable_aria_label** (Boolean = `false`) - Whether fields, links, and buttons will match against `aria-label` attribute.78 # - **exact** (Boolean = `false`) - Whether locators are matched exactly or with substrings. Only affects selector conditions79 # written using the `XPath#is` method.80 # - **exact_text** (Boolean = `false`) - Whether the text matchers and `:text` filter match exactly or on substrings.81 # - **ignore_hidden_elements** (Boolean = `true`) - Whether to ignore hidden elements on the page.82 # - **match** (`:one`, `:first`, `:prefer_exact`, `:smart` = `:smart`) - The matching strategy to find nodes.83 # - **predicates_wait** (Boolean = `true`) - Whether Capybara's predicate matchers use waiting behavior by default.84 # - **raise_server_errors** (Boolean = `true`) - Should errors raised in the server be raised in the tests?85 # - **reuse_server** (Boolean = `true`) - Whether to reuse the server thread between multiple sessions using the same app object.86 # - **run_server** (Boolean = `true`) - Whether to start a Rack server for the given Rack app.87 # - **save_path** (String = `Dir.pwd`) - Where to put pages saved through {Capybara::Session#save_page save_page}, {Capybara::Session#save_screenshot save_screenshot},88 # {Capybara::Session#save_and_open_page save_and_open_page}, or {Capybara::Session#save_and_open_screenshot save_and_open_screenshot}.89 # - **server** (Symbol = `:default` (which uses puma)) - The name of the registered server to use when running the app under test.90 # - **server_errors** (Array\<Class> = `[Exception]`) - Error classes that should be raised in the tests if they are raised in the server91 # and {configure raise_server_errors} is `true`.92 # - **test_id** (Symbol, String, `nil` = `nil`) - Optional attribute to match locator against with built-in selectors along with id.93 # - **threadsafe** (Boolean = `false`) - Whether sessions can be configured individually.94 # - **w3c_click_offset** (Boolean = 'false') - Whether click offsets should be from element center (true) or top left (false)95 #96 # #### DSL Options97 #98 # When using `capybara/dsl`, the following options are also available:99 #100 # - **default_driver** (Symbol = `:rack_test`) - The name of the driver to use by default.101 # - **javascript_driver** (Symbol = `:selenium`) - The name of a driver to use for JavaScript enabled tests.102 #103 def configure104 yield config105 end106 ##107 #108 # Register a new driver for Capybara.109 #110 # Capybara.register_driver :rack_test do |app|111 # Capybara::RackTest::Driver.new(app)112 # end113 #114 # @param [Symbol] name The name of the new driver115 # @yield [app] This block takes a rack app and returns a Capybara driver116 # @yieldparam [<Rack>] app The rack application that this driver runs against. May be nil.117 # @yieldreturn [Capybara::Driver::Base] A Capybara driver instance118 #119 def register_driver(name, &block)120 drivers[name] = block121 end122 ##123 #124 # Register a new server for Capybara.125 #126 # Capybara.register_server :webrick do |app, port, host|127 # require 'rack/handler/webrick'128 # Rack::Handler::WEBrick.run(app, ...)129 # end130 #131 # @param [Symbol] name The name of the new driver132 # @yield [app, port, host] This block takes a rack app and a port and returns a rack server listening on that port133 # @yieldparam [<Rack>] app The rack application that this server will contain.134 # @yieldparam port The port number the server should listen on135 # @yieldparam host The host/ip to bind to136 #137 def register_server(name, &block)138 servers[name.to_sym] = block139 end140 ##141 #142 # Add a new selector to Capybara. Selectors can be used by various methods in Capybara143 # to find certain elements on the page in a more convenient way. For example adding a144 # selector to find certain table rows might look like this:145 #146 # Capybara.add_selector(:row) do147 # xpath { |num| ".//tbody/tr[#{num}]" }148 # end149 #150 # This makes it possible to use this selector in a variety of ways:151 #152 # find(:row, 3)153 # page.find('table#myTable').find(:row, 3).text154 # page.find('table#myTable').has_selector?(:row, 3)155 # within(:row, 3) { expect(page).to have_content('$100.000') }156 #157 # Here is another example:158 #159 # Capybara.add_selector(:id) do160 # xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }161 # end162 #163 # Note that this particular selector already ships with Capybara.164 #165 # @param [Symbol] name The name of the selector to add166 # @yield A block executed in the context of the new {Capybara::Selector}167 #168 def add_selector(name, **options, &block)169 Capybara::Selector.add(name, **options, &block)170 end171 ##172 #173 # Modify a selector previously created by {Capybara.add_selector}.174 # For example, adding a new filter to the :button selector to filter based on175 # button style (a class) might look like this176 #177 # Capybara.modify_selector(:button) do178 # filter (:btn_style, valid_values: [:primary, :secondary]) { |node, style| node[:class].split.include? "btn-#{style}" }179 # end180 #181 #182 # @param [Symbol] name The name of the selector to modify183 # @yield A block executed in the context of the existing {Capybara::Selector}184 #185 def modify_selector(name, &block)186 Capybara::Selector.update(name, &block)187 end188 def drivers189 @drivers ||= {}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 = false448 config.run_server = true449 config.server = :default450 config.default_selector = :css451 config.default_max_wait_time = 2452 config.ignore_hidden_elements = true453 config.default_host = 'http://www.example.com'454 config.automatic_reload = true455 config.match = :smart456 config.exact = false457 config.exact_text = false458 config.raise_server_errors = true459 config.server_errors = [Exception]460 config.visible_text_only = false461 config.automatic_label_click = false462 config.enable_aria_label = false463 config.reuse_server = true464 config.default_set_options = {}465 config.test_id = nil466 config.predicates_wait = true467 config.default_normalize_ws = false468 config.allow_gumbo = false469 config.w3c_click_offset = false470end...
minitest.rb
Source:minitest.rb
...3require 'capybara/dsl'4module Capybara5 module Minitest6 module Assertions7 ## Assert text exists8 #9 # @!method assert_text10 # @see Capybara::Node::Matchers#assert_text11 ## Assert text does not exist12 #13 # @!method assert_no_text14 # @see Capybara::Node::Matchers#assert_no_text15 ##16 # Assertion that page title does match17 #18 # @!method assert_title19 # @see Capybara::Node::DocumentMatchers#assert_title20 ##21 # Assertion that page title does not match22 #23 # @!method refute_title24 # @!method assert_no_title25 # @see Capybara::Node::DocumentMatchers#assert_no_title26 ##27 # Assertion that current path matches28 #29 # @!method assert_current_path30 # @see Capybara::SessionMatchers#assert_current_path31 ##32 # Assertion that current page does not match33 #34 # @!method refute_current_path35 # @!method assert_no_current_path36 # @see Capybara::SessionMatchers#assert_no_current_path37 %w[text no_text title no_title current_path no_current_path].each do |assertion_name|38 class_eval <<-ASSERTION, __FILE__, __LINE__ + 139 def assert_#{assertion_name} *args40 self.assertions +=141 subject, args = determine_subject(args)42 subject.assert_#{assertion_name}(*args)43 rescue Capybara::ExpectationNotMet => e44 raise ::Minitest::Assertion, e.message45 end46 ASSERTION47 ruby2_keywords "assert_#{assertion_name}" if respond_to?(:ruby2_keywords)48 end49 alias_method :refute_title, :assert_no_title50 alias_method :refute_text, :assert_no_text51 alias_method :refute_content, :refute_text52 alias_method :refute_current_path, :assert_no_current_path53 alias_method :assert_content, :assert_text54 alias_method :assert_no_content, :refute_text55 ## Assert selector exists on page56 #57 # @!method assert_selector58 # @see Capybara::Node::Matchers#assert_selector59 ## Assert selector does not exist on page60 #61 # @!method assert_no_selector62 # @see Capybara::Node::Matchers#assert_no_selector63 ## Assert element matches selector64 #65 # @!method assert_matches_selector66 # @see Capybara::Node::Matchers#assert_matches_selector67 ## Assert element does not match selector68 #...
text
Using AI Code Generation
1 def search_for(search_term)2 find('title').text3page.search_for('Capybara')
text
Using AI Code Generation
1 config.allow_url("http://www.example.com")2 visit('http://www.example.com')3puts b.find(:css, 'h1').text4 config.allow_url("http://www.example.com")5 visit('http://www.example.com')6puts b.find(:css, 'h1').native.text7 config.allow_url("http://www.example.com")8 visit('http://www.example.com')9puts b.find(:css, 'h1').native.inner_text10 config.allow_url("http://www.example.com")11 visit('http://www.example.com')12puts b.find(:css, 'h1').native.innerHtml
text
Using AI Code Generation
1 def using(resource)2 evaluate_script("$(this).text()")3 evaluate_script("$(this).html()")4 evaluate_script("$(this).outerHtml()")5 evaluate_script("$(this).val()")6 def css(css)7 evaluate_script("$(this).find(css)")8 def xpath(xpath)9 evaluate_script("$(this).findXPath(xpath)")10 def attr(attribute)11 evaluate_script("$(this).attr(attribute)")12 evaluate_script("$(this).attr('id')")13 evaluate_script("$(this).attr('name')")14 evaluate_script("$(this).attr('class')")15 evaluate_script("$(this).prop('tagName')")16 evaluate_script("$(this).parent()")17 evaluate_script("$(this).children()")18 evaluate_script("$(this).siblings()")
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!!