0
votes

I know that there are several questions related to implementation of waiting and timeouts in Watir, however I have not found an answer to my problem (which must be common). I use Watir-webdriver for testing of a page that due to AJAX implementation loads portion-by-portion for very long time (more than 5 min). I need to be able just to sample this page for a limited time (20-40 sec) and to be able to analyze the information that is loaded during this short time. However, as I know, there is no straightforward direct mechanism to tell Watir::Browser to stop. I can use the Timeout, but although my script gets the control after rescue, it is impossible to interrogate the browser and verify the information that it is able to received during the timeout window. All I can do at this point is to kill the process and restart the browser as discussed here: Make headless browser stop loading page and elsewhere.

The code below illustrates my situation. In this example I have a global timeout (30 sec) and a local timeout (15 sec) used for reading the page. It never gets to b.text call; the script just outputs the first exception after 15 sec and then it keeps waiting for the browser to be released and after the global timeout of 30 sec prints the second exception message.

Time out. Got into exception branch 
Dropped to bottom rescue. 
The end.

I also tried to send an 'escape' key to the browser, but any communication with it while it is in the goto method is impossible. Any tips and suggestions will be appreciated!

require 'watir-webdriver'
require 'timeout'

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 30                      # Set the global timeout 

b = Watir::Browser.new :chrome, :http_client => client
my_url = '...here is my address...'

begin
  begin
   Timeout::timeout(15) { b.goto my_url } # Access the page with local timeout
   b.close # if all is unbelievably good and the page is loaded

  rescue Exception => e
    puts 'Time out. Got into exception branch'

    if b.text.include? 'my_text'   # NEVER GETS HERE
      puts 'Yes, I see the text!'
    else
      puts 'I do not see the text.'
    end
  end
rescue Exception => e
 puts 'Dropped to bottom rescue.'
end

puts 'The end.'
1

1 Answers

2
votes

Watir relies on Selenium WebDriver to handle calls to the browser. At this time all browsers require that the document.readyState of the current frame return "complete" before returning control to your code.

A recent update to the webdriver specification appears to allow for the possibility of a browser driver implementing a page loading strategy that is not blocking, but it is not a requirement and is not supported at this time.

https://w3c.github.io/webdriver/webdriver-spec.html#the-page-load-strategy