0

假设,以下是html代码:

<a href="http://www.google.com">Google</a>

通过以下代码,我可以确保 href 用于链接:

expected_href = "http://www.google.com"
actual_href = driver.find_element(:tag_name, "a").attribute("href")

unless actual_href == expected_href
  puts "Link is incorrect"
end

但我想确保链接“ http://www.google.com ”没有损坏意味着如果我向“ http://www.google.com ”发出请求,它的响应应该是状态“200”

我该如何测试呢?是否有任何 Ruby 库来测试 http 请求?

4

2 回答 2

0

以下代码对我有用:

require 'open-uri'
require 'selenium-webdriver'

expected_status = ["200", "OK"]

link = driver.find_element(:tag_name, "a").attribute("href")
io = open(link)
link_status = io.status

unless expected_status == link_status
  puts "Link is broken"
end
于 2013-05-13T14:11:39.817 回答
0

这里是URI doc

require 'uri'

expected_href = "http://www.google.com"
actual_href = "http://google.com"
uri1 = URI(actual_href)
uri2 = URI(expected_href)
puts "Link is incorrect" unless [uri1.host,uri1.scheme] == [uri2.host,uri2.scheme]
#=> Link is incorrect

或者你可以在这里寻找其他方式Net HTTP get source code and status

于 2013-05-13T13:29:59.663 回答