循环时出错

时间:2012-04-06 18:11:12

标签: ruby watir

我正在编写一些代码,用于从文本文件中提取URL,然后检查它们是否加载。我的代码是:

require 'rubygems'
require 'watir'
require 'timeout'

Watir::Browser.default = "firefox"
browser = Watir::Browser.new

File.open('pl.txt').each_line do |urls|
  begin
    Timeout::timeout(10) do
      browser.goto(urls.chomp)
      if browser.text.include? "server"
        puts 'here the page didnt' 
      else
        puts 'here site was found'
        File.open('works.txt', 'a') { |f| f.puts urls }
      end
    end
  rescue Timeout::Error => e
    puts e
  end
end

browser.close

事情是我收到了错误:

execution expired
/Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/jssh_socket.rb:19:in `const_get': wrong number of arguments (2 for 1) (ArgumentError)
    from /Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/jssh_socket.rb:19:in `js_eval'
    from /Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/firefox.rb:303:in `open_window'
    from /Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/firefox.rb:94:in `get_window_number'
    from /Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/firefox.rb:103:in `goto'
    from samplecodestack.rb:17
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
    from samplecodestack.rb:16
    from samplecodestack.rb:13:in `each_line'
    from samplecodestack.rb:13

任何人都知道如何让它发挥作用?

2 个答案:

答案 0 :(得分:1)

您也可以使用net / http并处理超时。

require "net/http"
require "uri"
File.open('pl.txt').each_line do |urls|
    uri = URI.parse(urls.chomp)
    begin
        response = Net::HTTP.get_response(uri)
    rescue Exception=> e
        puts e.message
        puts "did not load!"
    end
end

我无法跟踪您的堆栈跟踪,但它似乎在您的goto语句中。

答案 1 :(得分:0)

execution expired是超出Timeout::timeout的块时发生的错误。请注意,超时正在检查其整个块是否在指定时间内完成。鉴于行号错误,我猜测正在加载的URL花了将近10秒,然后文本检查超时。

我认为你真的只是意味着如果页面加载时间超过10秒而超时发生,而不是整个测试需要10秒才能完成。所以你应该将if语句移出Timeout块:

File.open('pl.txt').each_line do |urls|
  begin
    Timeout::timeout(10) do
      browser.goto(urls.chomp)
    end
    if browser.text.include? "server"
      puts 'here the page didnt' 
    else
      puts 'here site was found'
      File.open('works.txt', 'a') { |f| f.puts urls }
    end
  rescue Timeout::Error => e
    puts 'here the page took too long to load'
    puts e
  end
end