Module: NavigationHelpers
- Defined in:
- lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb
Instance Method Summary collapse
- #comparable_urls_for(target_url, source_url) ⇒ Object
- #default_path_for(page_name) ⇒ Object
-
#get_response_from(uri) ⇒ Object
this returns a hash with a response code and the url to help provide good fail detail.
- #is_javascript_href?(href) ⇒ Boolean
- #is_relative?(path) ⇒ Boolean
- #is_url?(path) ⇒ Boolean
- #is_valid_url_or_path?(path) ⇒ Boolean
- #javascript_references_on(doc) ⇒ Object
- #links_on(doc) ⇒ Object
- #normalize(url) ⇒ Object
- #normalize_path(path) ⇒ Object
- #parse_dom_tree(url) ⇒ Object
-
#path_to(page_name) ⇒ Object
Maps a name to a path.
- #stylesheet_references_on(doc) ⇒ Object
-
#url_from(matches, page_name) ⇒ Object
:nocov:.
-
#url_in_new_window ⇒ Object
FIXME: Code not used.
-
#url_to(path) ⇒ Object
:nocov:.
- #valid_assets_links_from(nokogiri_html_document) ⇒ Object
- #validate_response_from(page_name, href) ⇒ Object
- #wait_for(left_argument, right_argument) ⇒ Object
- #with_encoded_white_space(string) ⇒ Object
- #with_unencoded_white_space(string) ⇒ Object
- #without_trailing_slash(url) ⇒ Object
Instance Method Details
#comparable_urls_for(target_url, source_url) ⇒ Object
190 191 192 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 190 def comparable_urls_for(target_url, source_url) is_url?(target_url) ? [target_url, source_url].map{|u| normalize(u)} : [target_url, URI.parse(source_url).path] end |
#default_path_for(page_name) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 28 def default_path_for(page_name) case page_name when /^home$/ '/' when /^congratulations$/ '/congratulations' when /^other$/ '/other/page' when /^form$/ '/form/page' when /^valid_links$/ '/valid-links' when /^invalid_links$/ '/invalid-links' # Add more mappings here. # Here is an example that pulls values out of the Regexp: # # when /^(.*)'s profile page$/i # user_profile_path(User.find_by_login($1)) else begin page_name =~ /^the (.*) page$/ path_components = $1.split(/\s+/) self.send(path_components.push('path').join('_').to_sym) rescue NoMethodError, ArgumentError raise "Can't find mapping from \"#{page_name}\" to a path.\n" + "Now, go and add a mapping in #{__FILE__}" end end end |
#get_response_from(uri) ⇒ Object
this returns a hash with a response code and the url to help provide good fail detail
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 134 def get_response_from(uri) http = Net::HTTP.new(uri.host, uri.port) http.read_timeout = http.open_timeout = SimpliTest.config[:settings]["HTTP_TIMEOUT"] || 3 request = Net::HTTP::Get.new(uri.request_uri) if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.request(request) end |
#is_javascript_href?(href) ⇒ Boolean
91 92 93 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 91 def is_javascript_href?(href) href =~ /^(javascript|#)/ end |
#is_relative?(path) ⇒ Boolean
82 83 84 85 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 82 def is_relative?(path) uri = URI.parse(path) uri.relative? end |
#is_url?(path) ⇒ Boolean
75 76 77 78 79 80 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 75 def is_url?(path) uri = URI.parse(path) uri if uri.scheme =~ /^https?$/ rescue URI::BadURIError, URI::InvalidURIError false end |
#is_valid_url_or_path?(path) ⇒ Boolean
87 88 89 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 87 def is_valid_url_or_path?(path) is_url?(path) || is_relative?(path) end |
#javascript_references_on(doc) ⇒ Object
109 110 111 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 109 def javascript_references_on(doc) doc.css('script').collect{|l| l['src']}.compact end |
#links_on(doc) ⇒ Object
113 114 115 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 113 def links_on(doc) doc.css('a').collect{|l| l['href']}.compact.select{ |l| !is_javascript_href?(l) } end |
#normalize(url) ⇒ Object
167 168 169 170 171 172 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 167 def normalize(url) normalized_uri = URI.parse(url).normalize url = normalized_uri.to_s encoded_url = URI::encode(url) without_trailing_slash(encoded_url) end |
#normalize_path(path) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 174 def normalize_path(path) if path = is_url?(path) path = path.path end with_encoded_white_space(without_trailing_slash(path)) end |
#parse_dom_tree(url) ⇒ Object
95 96 97 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 95 def parse_dom_tree(url) Nokogiri::HTML(open(url)) end |
#path_to(page_name) ⇒ Object
Maps a name to a path. Used by the
When /^I go to (.+)$/ do |page_name|
TODO: Rewrite without test app for template
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 7 def path_to(page_name) if is_url?(page_name) page_name elsif SimpliTest.config_pages matches = SimpliTest.config_pages.select { |p| unquoted(page_name) == p } matches.any? ? url_from(matches, page_name) : default_path_for(page_name) else default_path_for(page_name) end end |
#stylesheet_references_on(doc) ⇒ Object
105 106 107 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 105 def stylesheet_references_on(doc) doc.css('link').collect{|l| l['href'] }.compact end |
#url_from(matches, page_name) ⇒ Object
:nocov:
18 19 20 21 22 23 24 25 26 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 18 def url_from(matches, page_name) if matches.length > 1 raise "Oops ambigous page name! More than one matched our search. It seems you have similar page names. \n" + "Now, go and modify #{page_name} to something cooler in config/pages.yml" else path = matches.values.first end url_to(path) if path end |
#url_in_new_window ⇒ Object
FIXME: Code not used. Should be deleted
146 147 148 149 150 151 152 153 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 146 def url_in_new_window if handles = page.driver.browser.window_handles rescue nil new_window = handles.last page.within_window(new_window) do return without_trailing_slash(current_url) end end end |
#url_to(path) ⇒ Object
:nocov:
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 62 def url_to(path) if is_url?(path) path else begin URI.parse(SimpliTest.config_environments[SimpliTest.config_environment] + path).normalize.to_s rescue puts "It seems that the #{SimpliTest.config_environment} enviroment has not been set up. Please add it to features/config/environments.yml" raise "Unidentified Environment Requested!!" end end end |
#valid_assets_links_from(nokogiri_html_document) ⇒ Object
99 100 101 102 103 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 99 def valid_assets_links_from(nokogiri_html_document) doc = nokogiri_html_document links = stylesheet_references_on(doc) + javascript_references_on(doc) links.select{ |l| !is_javascript_href?(l) } end |
#validate_response_from(page_name, href) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 117 def validate_response_from(page_name, href) begin root_path = path_to(page_name) uri = is_relative?(href) ? (URI.join(root_path, href)) : URI.parse(href) response = get_response_from(uri) valid_status_groups = [Net::HTTPRedirection, Net::HTTPInformation, Net::HTTPSuccess ] valid_status_groups.should include(response.class.superclass) rescue RSpec::Expectations::ExpectationNotMetError = "Received a #{response.code} #{ response.msg } from #{href} on #{page_name}" fail rescue = "Invalid reference: #{href} found on #{page_name} page" fail end end |
#wait_for(left_argument, right_argument) ⇒ Object
181 182 183 184 185 186 187 188 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 181 def wait_for(left_argument, right_argument) called_at = Time.now until left_argument > right_argument do past_wait_time = called_at < Time.now - SimpliTest.config['HTTP_TIMEOUT'] return if past_wait_time sleep 1 end end |
#with_encoded_white_space(string) ⇒ Object
159 160 161 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 159 def with_encoded_white_space(string) string.gsub(' ', '%20') end |
#with_unencoded_white_space(string) ⇒ Object
163 164 165 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 163 def with_unencoded_white_space(string) string.gsub('%20', ' ') end |
#without_trailing_slash(url) ⇒ Object
155 156 157 |
# File 'lib/SimpliTest/helpers/step_helpers/navigation_helpers.rb', line 155 def without_trailing_slash(url) url.chomp('/') end |