Search code examples
rubytimeoutwatirrescue

Retry testing sites after timeout error in Watir


I am going through a list of sites and going to each one using Watir to look for something in the source code of each page. However, after about 20 or 30 sites, the browser times out when loading a certain page and it breaks my script and I get this error:

rbuf_fill: execution expired (Timeout::Error)

I am trying to implement a way to detect when it times out and then restart testing the sites from where it left off but am having trouble. This is my code:

ie = Watir::Browser.new :firefox, :profile => "default"
testsite_array = Array.new
y=0
File.open('topsites.txt').each do |line|
testsite_array[y] = line
y=y+1
end
total = testsite_array.length
count = 0
begin
    while count <= total
        site = testsite_array[count]
        ie.goto site
        if ie.html.include? 'teststring'
            puts site + ' yes'
        else
            puts site + ' no'
        end

rescue
retry
    count = count+1
    end
end
ie.close

Solution

  • Your loop can be:

    #Use Ruby's method for iterating through the array
    testsite_array.each do |site|
        attempt = 1
        begin
            ie.goto site
            if ie.html.include? 'teststring'
                puts site + ' yes'
            else
                puts site + ' no'
            end 
        rescue
            attempt += 1
    
            #Retry accessing the site or stop trying
            if attempt > MAX_ATTEMPTS
                puts site + ' site failed, moving on'
            else
                retry
            end
        end
    end