2

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

Gabe Spradlin
  • 1,647
  • 3
  • 22
  • 44
  • 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 Nov 10 '11 at 20:27
  • 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 Nov 11 '11 at 17:04
  • 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 Nov 11 '11 at 17:06
  • 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 Nov 11 '11 at 18:04
  • 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 Nov 12 '11 at 19:43
  • I would just put it into your own utility library, rather than trying to add it to watir proper (as your addition might get removed on an upgrade to watir) – Chuck van der Linden Nov 14 '11 at 22:32

2 Answers2

1

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

Community
  • 1
  • 1
Sam King
  • 1,940
  • 16
  • 25
  • Thanks for the link. I think the justification given for not supporting the response code (at http://code.google.com/p/selenium/issues/detail?id=141) is just boneheaded. I understand feature creep vs quality but a response code has to be something that is already available somewhere since the browsers can display it. And telling everyone they can just go learn/use some other gem for the response code misses the point entirely. The first thing I want to check on a returned page is the response code - no need to proceed if it isn't 200. I'm not sure what they are thinking. Thanks again – Gabe Spradlin Aug 13 '12 at 23:18
  • I agree with you. One part of their justification (from digging through the forms) was that, in an attempt to automate a webbrowser as a user would see it, checking HTTP codes becomes less meaningful. That is, if I go to example.com and it redirects me to www.example.com, should I get a redirect code or a 200? If I go to a page that 404s, it often redirects to a perfectly valid page that tells the user that the page wasn't found. Should the response code be a 404 or a 200? Thus, the design decision isn't clear on how they would implement response codes. – Sam King Aug 14 '12 at 03:18
  • I agree that getting that correct is a tricky thing. However, I would guess, that providing the last response code would satisfy the majority of testing purposes. I also wonder why they seem to think they can only provide 1 response code. Why not an array where the response code "on top" is the last page's code and the first page's code is "on bottom". The 404 is special case and I'd think about adding something unique to 404 so your test could verify that your 404 page loaded correctly. That way, as a tester, you could determine if you got all the redirects, etc. you were supposed to get. – Gabe Spradlin Aug 15 '12 at 01:06
-2

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

Community
  • 1
  • 1
Chuck van der Linden
  • 6,589
  • 2
  • 24
  • 43
  • 1
    browser.status is empty when I check it – Gabe Spradlin Nov 10 '11 at 03:34
  • 1
    browser.status is empty when I check it. The link you provide is one of the resources I'd already seen. However, the first time I read it I thought it was using the additional gem to call the page a 2nd time and thus negating any possible use I might have for it. Upon reading it a second time I'm not sure. My reading of that link suggests that Watir was used to build a list of links. Then Net::HTTP.start is used to call the page on the other end of that link to grab the head and then look for status 200. But I need to know the status of the page Watir is on right now not some other page. – Gabe Spradlin Nov 10 '11 at 03:41
  • I don't think the error you described 'no response from socket' is a HTTP level error. So it may be that tryingt to figure out the HTTP status of the page is not going to help you. This might be a question better directed at the webdriver folks. – Chuck van der Linden Nov 10 '11 at 17:42
  • I saw the error in Firefox today for the first time while testing my code on a second machine. Not that this necessarily narrows it down. It could still the site or the webdriver. Hmmm... – Gabe Spradlin Nov 10 '11 at 19:20
  • `browser.status` returns js `window.status`, not response status https://www.rubydoc.info/gems/watir/Watir/Browser#status-instance_method The status property of the Window interface sets the text in the status bar at the bottom of the browser or returns the previously set text. – Yurii Verbytskyi Apr 03 '19 at 12:55
  • As the answer below indicates, there really isn't a way to do this with webdriver, and thus also not a way to do it with watir. Yes, `browser.status` returns the status, which 8+ years ago was more of a thing than it is now. Most browsers have hidden the status bar, if not outright removed it, sites no longer seek to populate it.. in 2011, you MIGHT have been able to get some clue, on some sites via the status bar.. now? yeah that suggestion has little to no use. – Chuck van der Linden Apr 04 '19 at 23:20