You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Watir::Browser遇无限循环页面无响应后如何恢复并继续测试?

Fixing Watir Browser Lockup After Net::ReadTimeout on Infinite Loop Pages

I’ve dealt with this exact headache before—when you hit a page with an infinite JS loop, Watir throws a Net::ReadTimeout, and suddenly every subsequent browser operation (like checking elements or closing) blows up with the same error. The problem is that the browser process gets stuck in an unresponsive state, so you can’t reuse that instance. Here’s how to get your test suite back on track:

1. Forcefully Destroy and Recreate the Browser Instance

The most reliable fix is to kill the stuck browser process entirely and start fresh. Trying to call browser.close or browser.quit directly might fail because the process is unresponsive, so wrap those calls in a rescue to avoid new exceptions.

# Initialize your browser first
browser = Watir::Browser.new(:chrome)

urls_to_test = ['http://infinite-loop-site.com', 'http://next-good-site.com', ...]

urls_to_test.each do |url|
  begin
    browser.goto(url)
    # Your normal test logic here (check elements, interact, etc.)
  rescue Net::ReadTimeout => e
    puts "Skipping unresponsive site: #{url} - #{e.message}"
    
    # Force quit the driver to kill the stuck browser process
    browser.driver.quit rescue nil
    
    # Recreate the browser instance for the next site
    browser = Watir::Browser.new(:chrome)
  end
end

# Clean up the final browser instance
browser.driver.quit rescue nil

The rescue nil ensures that even if driver.quit fails (because the process is already hung), your script won’t crash and will just move on to creating a new browser.

2. Preconfigure Page Load Timeouts and Strategy

Prevent the timeout from taking so long, or even triggering unnecessarily, by adjusting how Watir waits for pages to load. Using a more lenient page load strategy and setting a short timeout can help:

options = Selenium::WebDriver::Chrome::Options.new
# Use 'eager' to stop waiting once the DOM is loaded (doesn't wait for all resources/JS)
options.page_load_strategy = 'eager'

browser = Watir::Browser.new(:chrome, options: options)
# Set a strict page load timeout (e.g., 10 seconds)
browser.driver.manage.timeouts.page_load = 10

This way, Watir won’t wait around for the infinite loop page to "finish" loading—it’ll stop as soon as the DOM is ready, and if even that takes too long, it’ll trigger the timeout faster so you can skip the site and reset the browser.

3. Isolate Tests in Separate Threads (Advanced)

For larger test suites, running each site’s test in its own thread with a dedicated browser instance prevents one stuck browser from taking down the entire suite. Each thread handles its own cleanup:

urls_to_test = ['http://bad-site.com', 'http://good-site.com', ...]

threads = urls_to_test.map do |url|
  Thread.new do
    browser = Watir::Browser.new(:chrome)
    begin
      browser.goto(url)
      # Test logic here
      puts "Successfully tested #{url}"
    rescue Net::ReadTimeout => e
      puts "Skipping #{url}: Unresponsive page - #{e}"
    ensure
      # Always clean up the browser for this thread
      browser.driver.quit rescue nil
    end
  end
end

# Wait for all threads to finish
threads.each(&:join)

This is great for parallelizing tests too, but keep an eye on system resources—too many concurrent browsers can slow down your machine.


内容的提问来源于stack exchange,提问作者GregF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:13:49