Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 72 additions & 6 deletions spec/pages_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
require 'spec_helper'
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']
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made a Links object class's instance variable.


PAGES.each do |p|
status=''
describe p do
it_behaves_like 'Page'
it_behaves_like 'Page with search box' unless p == '/search.html'
Expand All @@ -13,13 +64,28 @@
visit p
end

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"
visit p
it 'has valid internal hyperlinks' do
links = Links.new(page.all(:css, 'a'),urls)
links.each do |link|
url=link[:href]
if !is_uri? url
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use unless instead of if ! .

page.find(:xpath, link.path).click
expect(page.status_code).to be(200), "expected link '#{link.text}' to work"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In be(200) the usual practice is to replace the magic constant with a literal constant (IOW use capitalized variable defined in spec_helper.

end
end
visit p
end

it 'has valid external hyperlinks' do
links = Links.new(page.all(:css, 'a'),urls)
links.each do |link|
url=link[:href]
if is_uri? url
result = Links.url_exists? url
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking of it a bit more the method could return two values instead (you're not passing it anyway and using a global variable).

expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error code '#{status}')"
end
end
visit p
end
end
end