From 4f1cf5bfae3040bd38880f0a50557cba00d69dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20Mrkvica?= Date: Mon, 28 May 2018 15:01:09 +0200 Subject: [PATCH 1/4] add/edit tests: internal & external --- spec/pages_spec.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/spec/pages_spec.rb b/spec/pages_spec.rb index c4067dc..827ac23 100644 --- a/spec/pages_spec.rb +++ b/spec/pages_spec.rb @@ -4,6 +4,9 @@ site = File.join(File.dirname(__FILE__), '..', '_site', '**', '*.html') PAGES = Dir.glob(site).map{ |p| p.gsub(/[^_]+\/_site(.*)/, '\\1') } +urls = ['http://localhost:4000', 'http://localhost'] +status='' + PAGES.each do |p| describe p do it_behaves_like 'Page' @@ -15,9 +18,23 @@ it 'has only valid internal hyperlinks' do page.all(:css, 'a').each do |link| - next if link.text == '' || link[:href].match(/(http|\/\/).*/) - page.find(:xpath, link.path).click - expect(page.status_code).to be(200), "expected link '#{link.text}' to work" + next if link.text.nil? || link.text.empty? || link[:href].match(/^http.*/) || link.path.nil? || urls.include?(link[:href]) + begin + page.find(:xpath, link.path).click + rescue NoMethodError + require 'irb';binding.irb + end + urls.push link.path + expect(page.status_code).to be(200), "expected link '#{link.text}' to work" + visit p + end + end + + it 'has valid external hyperlinks' do + page.all(:css, 'a').each do |link| + next if link.text.nil? || link.text.empty? || !link[:href].match(/^http.*/) || urls.include?(link[:href]) + urls.push(link[:href]) + expect(url_exists? link[:href], status).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error '#{status}')" visit p end end From 3c7fafab62d62c1c20ef34c9aee7fe070b3c0876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20Mrkvica?= Date: Wed, 30 May 2018 14:01:31 +0200 Subject: [PATCH 2/4] Edit code structure, delete debug code, separate "url_exists?" and "expect" methods --- spec/pages_spec.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/spec/pages_spec.rb b/spec/pages_spec.rb index 827ac23..244f025 100644 --- a/spec/pages_spec.rb +++ b/spec/pages_spec.rb @@ -6,7 +6,6 @@ urls = ['http://localhost:4000', 'http://localhost'] status='' - PAGES.each do |p| describe p do it_behaves_like 'Page' @@ -16,14 +15,15 @@ visit p end - it 'has only valid internal hyperlinks' do + it 'has valid internal hyperlinks' do page.all(:css, 'a').each do |link| - next if link.text.nil? || link.text.empty? || link[:href].match(/^http.*/) || link.path.nil? || urls.include?(link[:href]) - begin - page.find(:xpath, link.path).click - rescue NoMethodError - require 'irb';binding.irb - end + #do not check link, which isn't clickable or hasn't got any path + next if link.text.nil?\ + || link.text.empty?\ + || link[:href].match(/^http.*/)\ + || link.path.nil?\ + || urls.include?(link[:href]) + page.find(:xpath, link.path).click urls.push link.path expect(page.status_code).to be(200), "expected link '#{link.text}' to work" visit p @@ -32,9 +32,13 @@ it 'has valid external hyperlinks' do page.all(:css, 'a').each do |link| - next if link.text.nil? || link.text.empty? || !link[:href].match(/^http.*/) || urls.include?(link[:href]) + next if link.text.nil?\ + || link.text.empty?\ + || !link[:href].match(/^http.*/)\ + || urls.include?(link[:href]) urls.push(link[:href]) - expect(url_exists? link[:href], status).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error '#{status}')" + result = url_exists? link[:href], status + expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error '#{status}')" visit p end end From 80d9bd22c97dcf6cc8b5a1f225aa00a16ba6075f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20Mrkvica?= Date: Thu, 31 May 2018 10:39:07 +0200 Subject: [PATCH 3/4] add Links class --- spec/pages_spec.rb | 91 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 21 deletions(-) diff --git a/spec/pages_spec.rb b/spec/pages_spec.rb index 244f025..df2a768 100644 --- a/spec/pages_spec.rb +++ b/spec/pages_spec.rb @@ -1,12 +1,63 @@ require 'spec_helper' +require 'ap' +require 'uri' +class Links + def initialize(all_links,urls) + @n = Array.new + all_links.each do |link| + if !link.text.nil? \ + && !link.text.empty? \ + && !link.path.nil? \ + && !urls.include?(link[:href]) + urls.push link[:href] + @n.push(link) + end + end + end + + def each + @n.each do |l| + yield l + end + end + + def self.url_exists?(url, status) + 10.times do + begin + res = Net::HTTP.get_response(URI(url)) + status.replace res.code + rescue OpenSSL::SSL::SSLError, SocketError + return false + end + case res + when Net::HTTPRedirection + url = res['location'] + when Net::HTTPSuccess + return true + else + break + end + end + false + end +end + +def is_uri? url + if URI(url).scheme + true + else + false + end +end # Array of all generated pages site = File.join(File.dirname(__FILE__), '..', '_site', '**', '*.html') PAGES = Dir.glob(site).map{ |p| p.gsub(/[^_]+\/_site(.*)/, '\\1') } urls = ['http://localhost:4000', 'http://localhost'] -status='' + PAGES.each do |p| + status='' describe p do it_behaves_like 'Page' it_behaves_like 'Page with search box' unless p == '/search.html' @@ -16,31 +67,29 @@ end it 'has valid internal hyperlinks' do - page.all(:css, 'a').each do |link| - #do not check link, which isn't clickable or hasn't got any path - next if link.text.nil?\ - || link.text.empty?\ - || link[:href].match(/^http.*/)\ - || link.path.nil?\ - || urls.include?(link[:href]) - page.find(:xpath, link.path).click - urls.push link.path - expect(page.status_code).to be(200), "expected link '#{link.text}' to work" - visit p + links = Links.new(page.all(:css, 'a'),urls) + links.each do |link| + url=link[:href] + if !is_uri? url + #ap [link.path, link.text, link[:href]] + page.find(:xpath, link.path).click + expect(page.status_code).to be(200), "expected link '#{link.text}' to work" + end end + visit p end it 'has valid external hyperlinks' do - page.all(:css, 'a').each do |link| - next if link.text.nil?\ - || link.text.empty?\ - || !link[:href].match(/^http.*/)\ - || urls.include?(link[:href]) - urls.push(link[:href]) - result = url_exists? link[:href], status - expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error '#{status}')" - visit p + links = Links.new(page.all(:css, 'a'),urls) + links.each do |link| + url=link[:href] + if is_uri? url + #ap [link.path, link.text, link[:href]] + result = Links.url_exists? url + expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error code '#{status}')" + end end + visit p end end end From b7fc8bc167f7721f53ecccecfd0e99a1c259d2d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20Mrkvica?= Date: Thu, 31 May 2018 10:44:25 +0200 Subject: [PATCH 4/4] remove debug tools --- spec/pages_spec.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/spec/pages_spec.rb b/spec/pages_spec.rb index df2a768..8826e1c 100644 --- a/spec/pages_spec.rb +++ b/spec/pages_spec.rb @@ -1,6 +1,4 @@ require 'spec_helper' -require 'ap' -require 'uri' class Links def initialize(all_links,urls) @n = Array.new @@ -71,7 +69,6 @@ def is_uri? url links.each do |link| url=link[:href] if !is_uri? url - #ap [link.path, link.text, link[:href]] page.find(:xpath, link.path).click expect(page.status_code).to be(200), "expected link '#{link.text}' to work" end @@ -84,7 +81,6 @@ def is_uri? url links.each do |link| url=link[:href] if is_uri? url - #ap [link.path, link.text, link[:href]] result = Links.url_exists? url expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error code '#{status}')" end