2016-03-07 16:52:19 -08:00
# frozen_string_literal: true
2012-12-05 19:46:00 +01:00
2012-08-01 13:24:43 +02:00
module Capybara
2013-03-05 00:35:11 +03:00
# @api private
2012-08-01 13:24:43 +02:00
module Helpers
2019-05-30 15:07:27 -07:00
module_function
2012-08-01 13:24:43 +02:00
2013-03-17 15:48:04 +01:00
##
2018-03-12 10:23:32 -07:00
# @deprecated
2013-03-17 15:48:04 +01:00
# Normalizes whitespace space by stripping leading and trailing
# whitespace and replacing sequences of whitespace characters
# with a single space.
#
# @param [String] text Text to normalize
# @return [String] Normalized text
#
def normalize_whitespace ( text )
2020-09-07 04:21:53 +09:00
Capybara :: Helpers . warn 'DEPRECATED: Capybara::Helpers::normalize_whitespace is deprecated, please update your driver'
2013-03-17 15:48:04 +01:00
text . to_s . gsub ( / [[:space:]]+ / , ' ' ) . strip
end
2013-02-19 23:57:34 +01:00
2013-03-17 15:48:04 +01:00
##
#
# Escapes any characters that would have special meaning in a regexp
# if text is not a regexp
#
# @param [String] text Text to escape
2018-02-27 15:46:13 -08:00
# @param [Boolean] exact (false) Whether or not this should be an exact text match
# @param [Fixnum, Boolean, nil] options Options passed to Regexp.new when creating the Regexp
# @return [Regexp] Regexp to match the passed in text and options
2013-03-17 15:48:04 +01:00
#
2018-03-05 14:57:33 -08:00
def to_regexp ( text , exact : false , all_whitespace : false , options : nil )
2018-01-13 13:06:03 -08:00
return text if text . is_a? ( Regexp )
2018-03-05 14:57:33 -08:00
escaped = Regexp . escape ( text )
2018-07-10 14:18:39 -07:00
escaped = escaped . gsub ( '\\ ' , '[[:blank:]]' ) if all_whitespace
2018-01-13 13:06:03 -08:00
escaped = " \\ A #{ escaped } \\ z " if exact
2018-02-27 15:46:13 -08:00
Regexp . new ( escaped , options )
2013-03-17 15:48:04 +01:00
end
##
#
# Injects a `<base>` tag into the given HTML code, pointing to
2019-05-22 09:48:31 +09:00
# {Capybara.configure asset_host}.
2013-03-17 15:48:04 +01:00
#
# @param [String] html HTML code to inject into
2018-02-27 15:46:13 -08:00
# @param [URL] host (Capybara.asset_host) The host from which assets should be loaded
2013-03-29 11:15:07 +09:00
# @return [String] The modified HTML code
2013-03-17 15:48:04 +01:00
#
2018-02-27 15:46:13 -08:00
def inject_asset_host ( html , host : Capybara . asset_host )
2018-07-10 14:18:39 -07:00
if host && Nokogiri :: HTML ( html ) . css ( 'base' ) . empty?
2018-10-16 16:06:50 -07:00
html . match ( / <head[^<]*?> / ) do | m |
return html . clone . insert m . end ( 0 ) , " <base href=' #{ host } ' /> "
end
2013-02-19 23:57:34 +01:00
end
2015-05-03 19:13:35 -07:00
html
2012-08-01 13:24:43 +02:00
end
2013-03-04 02:04:23 +03:00
2013-03-17 15:48:04 +01:00
##
#
# A poor man's `pluralize`. Given two declensions, one singular and one
# plural, as well as a count, this will pick the correct declension. This
2014-03-19 18:28:26 -05:00
# way we can generate grammatically correct error message.
2013-03-17 15:48:04 +01:00
#
# @param [String] singular The singular form of the word
# @param [String] plural The plural form of the word
# @param [Integer] count The number of items
#
def declension ( singular , plural , count )
2018-01-13 13:06:03 -08:00
count == 1 ? singular : plural
2013-03-04 02:04:23 +03:00
end
2015-04-14 14:41:01 -07:00
2020-07-05 13:18:37 -07:00
def filter_backtrace ( trace )
return 'No backtrace' unless trace
filter = %r{ lib/capybara/|lib/rspec/|lib/minitest/ }
new_trace = trace . take_while { | line | line !~ filter }
2021-10-23 22:17:35 -07:00
new_trace = trace . grep_v ( filter ) if new_trace . empty?
2020-07-05 13:18:37 -07:00
new_trace = trace . dup if new_trace . empty?
new_trace . first . split ( / :in / , 2 ) . first
end
2020-09-07 04:21:53 +09:00
def warn ( message , uplevel : 1 )
2021-03-27 12:23:04 -07:00
Kernel . warn ( message , uplevel : uplevel )
2020-09-07 04:21:53 +09:00
end
2015-04-14 14:41:01 -07:00
if defined? ( Process :: CLOCK_MONOTONIC )
2018-02-27 15:46:13 -08:00
def monotonic_time ; Process . clock_gettime Process :: CLOCK_MONOTONIC ; end
2015-04-14 14:41:01 -07:00
else
2018-02-27 15:46:13 -08:00
def monotonic_time ; Time . now . to_f ; end
2015-04-14 14:41:01 -07:00
end
2018-06-06 10:51:25 -07:00
def timer ( expire_in : )
Timer . new ( expire_in )
end
class Timer
def initialize ( expire_in )
@start = current
@expire_in = expire_in
end
def expired?
2019-10-15 18:02:35 -07:00
if stalled?
raise Capybara :: FrozenInTime , 'Time appears to be frozen. Capybara does not work with libraries which freeze time, consider using time travelling instead'
end
2018-09-24 09:43:46 -07:00
2018-06-06 10:51:25 -07:00
current - @start > = @expire_in
end
def stalled?
@start == current
end
private
def current
Capybara :: Helpers . monotonic_time
end
end
2013-03-04 02:04:23 +03:00
end
2012-08-01 13:24:43 +02:00
end