2
votes

So I'm using Watir WebDriver with both Firefox and Chrome. Chrome is much faster but seems to have a number of eccentricities. First, in Firefox when I use something like:

ff.link(:text, "Click Here").exists?

It works fine. However, in Chrome it often seems to stall and then exit with a timeout exception. So what I've had to do is narrow the search in Chrome like this:

linkDiv = ch.div(:class, "mydiv")
linkDiv.link(:text, "Click Here").exists?

Typically this type of page reduction down to just the div tag of interest works just fine.

However, there is one other eccentricity I've noticed in Chrome that I haven't noticed in Firefox. In Chrome, I occasionally get an Error 15 - something like no response on socket but since it is intermittent I can't reproduce the error at will and haven't copied the exact text down yet. I haven't seen this in Firefox - at least not yet - but perhaps Chrome is giving me an error where Firefox hides it.

So I'd liked to be able to log when this error occurs and the best idea I've had so far is to use HTTP Codes - i.e. 200, 404, 500, etc. If the code is not 200 then record the code and try again.

Is there a way in Watir to get the HTTP code? Having come to Ruby from PHP/CURL I assumed there would be an easy way to get the HTTP code - something like browser.http_code (where browser = Watir::Browser.new :chrome, browser.goto(...)) but I haven't found it yet and I haven't had any luck finding a way online. The "solutions" I've found so far all entail other gems making a call to the page to get the returned code. However, since these pages work fine 10 times or more for every time I get this error making another call with another gem doesn't help.

Thanks


My workaround...

The title of the intermittent page with nothing on it includes "is not available" at the end of the browser.title string. So I use a browser.title.include? "is not available" check to see if the page never loaded.

This is less than ideal for a lot of reasons like that title may change from site to site or may change when Chrome gets upgraded. Where as a status code of 200 will always tell my code there is a problem...

I guess it's something to suggest to the developers.

Thanks again

2
If you are worried about the title on the error page changing, why not have the test check for the title of the page it expects to land on as a way to ensure you've actually arrived at the right place. Then if the expected title is not found, you could have it write out the actual title, and the page text, to some kind of error-log or results-log, and/or take a screenshot. - Chuck van der Linden
I've been programming in different languages since 1995ish. I used to be an aerospace engineer and there were a number of tasks that other didn't see as repetitive that I did. That served me well as my toolkits at each job I had grew over time. As a result I've gotten in the habit building all of my code to be as generic as the situation will allow. In other words my intent when writing this check would be that this check could be used on every other page I ever visit in order to see if I get this same error. Thus the check for status 200 - good for just about every page I'd ever visit. - Gabe Spradlin
And I think it is more likely that the title of the page I want to visit will change than the title of the error page. Thus I checked for the error. However, checking for the title of the page I want to visit might also allow me to put some form of auto-check into the code that lets me know if the page has changed and I didn't notice/forgot to update the Watir code for that page. Not bad to have the code double check things for you where it can easily do so. Thanks - Gabe Spradlin
yeah an 'arrived_at_right_page' method that took care of that and looking for potential error content might be a really useful thing, that's something I could see having in my utility library. you could have it take the title as a parameter, and return the found title (along with logging if there's a problem) and then use it in validation along the lines of browser.arrived_ok(title).should = title - Chuck van der Linden
I'm still new to Ruby (and really only been using PHP for about 18 months) and there is just a lot out there on writing add-on methods and overwriting methods that I'm just not used to from my previous coding experience. I'll have to look into coding a simple check that I can add to the Watir class and post it here. I could easily write it as a function so any pointers on how to add it to the browser object from Watir would be greatly appreciated. Thanks - Gabe Spradlin

2 Answers

1
votes

You can't do it with Watir-Webdriver. See https://stackoverflow.com/a/6512785/418107.

-2
votes

Have you tried?

browser.status

The other thing would be to check on it the way the user would experience it, by looking at the resulting page, checking the title or text to see if it indicates an error.

If that's not what you are looking for, then you will need to use an HTTP level gem in addition to watir

See this SO question and answer: Using Watir to check for bad links