如何确保使用Selenium webdriver不会破坏链接?

时间:2013-05-13 13:03:24

标签: ruby selenium-webdriver

假设,以下是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请求?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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