Skip to content

Commit 80d9bd2

Browse files
add Links class
1 parent 3c7fafa commit 80d9bd2

1 file changed

Lines changed: 70 additions & 21 deletions

File tree

spec/pages_spec.rb

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,63 @@
11
require 'spec_helper'
2+
require 'ap'
3+
require 'uri'
4+
class Links
5+
def initialize(all_links,urls)
6+
@n = Array.new
7+
all_links.each do |link|
8+
if !link.text.nil? \
9+
&& !link.text.empty? \
10+
&& !link.path.nil? \
11+
&& !urls.include?(link[:href])
12+
urls.push link[:href]
13+
@n.push(link)
14+
end
15+
end
16+
end
17+
18+
def each
19+
@n.each do |l|
20+
yield l
21+
end
22+
end
23+
24+
def self.url_exists?(url, status)
25+
10.times do
26+
begin
27+
res = Net::HTTP.get_response(URI(url))
28+
status.replace res.code
29+
rescue OpenSSL::SSL::SSLError, SocketError
30+
return false
31+
end
32+
case res
33+
when Net::HTTPRedirection
34+
url = res['location']
35+
when Net::HTTPSuccess
36+
return true
37+
else
38+
break
39+
end
40+
end
41+
false
42+
end
43+
end
44+
45+
def is_uri? url
46+
if URI(url).scheme
47+
true
48+
else
49+
false
50+
end
51+
end
252

353
# Array of all generated pages
454
site = File.join(File.dirname(__FILE__), '..', '_site', '**', '*.html')
555
PAGES = Dir.glob(site).map{ |p| p.gsub(/[^_]+\/_site(.*)/, '\\1') }
656

757
urls = ['http://localhost:4000', 'http://localhost']
8-
status=''
58+
959
PAGES.each do |p|
60+
status=''
1061
describe p do
1162
it_behaves_like 'Page'
1263
it_behaves_like 'Page with search box' unless p == '/search.html'
@@ -16,31 +67,29 @@
1667
end
1768

1869
it 'has valid internal hyperlinks' do
19-
page.all(:css, 'a').each do |link|
20-
#do not check link, which isn't clickable or hasn't got any path
21-
next if link.text.nil?\
22-
|| link.text.empty?\
23-
|| link[:href].match(/^http.*/)\
24-
|| link.path.nil?\
25-
|| urls.include?(link[:href])
26-
page.find(:xpath, link.path).click
27-
urls.push link.path
28-
expect(page.status_code).to be(200), "expected link '#{link.text}' to work"
29-
visit p
70+
links = Links.new(page.all(:css, 'a'),urls)
71+
links.each do |link|
72+
url=link[:href]
73+
if !is_uri? url
74+
#ap [link.path, link.text, link[:href]]
75+
page.find(:xpath, link.path).click
76+
expect(page.status_code).to be(200), "expected link '#{link.text}' to work"
77+
end
3078
end
79+
visit p
3180
end
3281

3382
it 'has valid external hyperlinks' do
34-
page.all(:css, 'a').each do |link|
35-
next if link.text.nil?\
36-
|| link.text.empty?\
37-
|| !link[:href].match(/^http.*/)\
38-
|| urls.include?(link[:href])
39-
urls.push(link[:href])
40-
result = url_exists? link[:href], status
41-
expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error '#{status}')"
42-
visit p
83+
links = Links.new(page.all(:css, 'a'),urls)
84+
links.each do |link|
85+
url=link[:href]
86+
if is_uri? url
87+
#ap [link.path, link.text, link[:href]]
88+
result = Links.url_exists? url
89+
expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error code '#{status}')"
90+
end
4391
end
92+
visit p
4493
end
4594
end
4695
end

0 commit comments

Comments
 (0)