How to deal with a page which fails to load and continue testing in Watir-Webdriver

怎甘沉沦 提交于 2019-12-01 12:45:49

I am not sure how would you test a page that did not load, but try something like this:

begin
  browser.goto "http://www.mycounciltax.org.uk/results?postcode=WC1N1DF&search=Search"
rescue => e
  puts "rescued #{e}"
end

This question you are asking, and this sort of problem probably points to a much larger problem in how you are organizing and running scripts.

The first suggestion I would have in most cases would be to use an existing test framework, test/unit cucumber, fitness etc. All of these are designed to run tests as small atomic items which report a failure if anything goes wrong and then move to the next test. (opposed to most homebrewed scripts that are very often a giant long sequence which breaks if something goes amiss and cannot cope with any unexpected failure, or some kind of processing loop stepping through a file, and still have the same kinds of issues. I'm not sure if that would apply to you as you seem to be using watir for scraping more than testing as far as I can tell.

If you are rolling your own framework, then this is something you need to design into the system. It's called 'exception handling' and the basic format is as described by Zeljko in his answer. A quick google search will find you multiple tutorials on this aspect of the ruby language.

Your edited code above is closer to this, but is looking pretty repetitive to me. (although since it is currently showing as all flush left it's hard to make sense of. So I edited it to add indentation, ah that's better...) I would consider turning stuff you repeat three times into a method that takes in the URL and spreadsheet location as parameters which would cut way down on the repetition.

Also you might find it more useful to wrap just a small number of lines of code inside the begin/rescue/end in order to report a more useful message about what failed. That would allow you to report something like "error loading page #{url}" you could even write that out to your spreadsheet.

I'd recommend a little reading on how errors can 'bubble up' through the system so that failures can be a little more informative.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!