ruby - Using Watir to check for bad links -
i have unordered list of links save off side, , want click each link , make sure goes real page , doesnt 404, 500, etc.
the issue not know how it. there object can inspect give me http status code or anything?
mylinks = browser.ul(:id, 'my_ul_id').links mylinks.each |link| link.click # need check 200 status or here! how? browser.back end
my answer similar idea tin man's.
require 'net/http' require 'uri' mylinks = browser.ul(:id, 'my_ul_id').links mylinks.each |link| u = uri.parse link.href status_code = net::http.start(u.host,u.port){|http| http.head(u.request_uri).code } # testing rspec status_code.should == '200' end if use test::unit testing framework, can test following, think
assert_equal '200',status_code
another sample (including chuck van der linden's idea): check status code , log out urls if status not good.
require 'net/http' require 'uri' mylinks = browser.ul(:id, 'my_ul_id').links mylinks.each |link| u = uri.parse link.href status_code = net::http.start(u.host,u.port){|http| http.head(u.request_uri).code } unless status_code == '200' file.open('error_log.txt','a+'){|file| file.puts "#{link.href} #{status_code}" } end end
Comments
Post a Comment