I have been a Watir user since 2005, and (at least since changes away from the 
original Watir) a repeated theme has been "How do I get the HTTP response 
code?" (Alternatively called "status code", but I believe response code is 
technically correct.)

As many of us know all too well, the Selenium-Webdriver team refuses to address 
this issue. Many developers obviously believe that while their technical 
excuses from the Selenium team may be true, that is insufficient to justify 
their position on the issue, because the lack of this feature seriously weakens 
the end product.

But even if it is a technical and (dare I say it?) religious issue for the 
Selenium team, I see no reason why this feature could not be incorporated into 
Watir-Webdriver. The feature was in the original Watir and I know that many 
people would appreciate its return.

It would be nice to be able to just do "browser.goto(url)", and read the 
response code. But since that isn't practical using the Selenium driver, I 
suggest another method of doing almost the same thing. Since it is fairly 
simple and lightweight, I propose a patch to Watir-Webdriver to implement it.

(*I KNOW* this has been a contentious issue. But I have 4 things to day about 
that: [1] there is a very good argument that response codes actually reproduce 
the user experience BETTER than tedious parsing of many and varied error pages, 
[2] users, who are also contributors, have repeatedly demanded it, [3] while it 
may not be easy to do in Selenium-Webdriver, it SHOULD BE easy to do in 
Watir-Webdriver, and [4] the usefulness of such a feature is both widespread 
and indisputable.)

A simple method is as follows. The cost is a bit of overhead from requiring 
Net::HTTP (that is, if it isn't *already* required by Watir-Webdriver). Since 
it is readily available in a standard Ruby install I see no great difficulty 
there.

This is all it would take:

        require 'net/http'

        def get_response(uri)
            Net::HTTP.get_response(URI(uri))
        end

That's it. Then if "browser" is your browser instance in Watir-Webdriver, you 
could simply do:

        response = browser.get_response("http://blahblahblah.com/something";)

and get your response code. The response consists of:

        response.code       # => "200"

        response.message    # => "OK"

When it fails it looks like this:

        response.code       # => "404"

        response.message    # => "Not Found"

(Or 500 "Internal Server Error ", 302 "Found" for redirects, etc.)

Note that this just does a HEAD request to the server. It does not need to 
distinguish between GET, POST and so on, and it is fast since it does not have 
to load pages.

I understand that some people might say "That's easy enough to do on your own; 
why bother to put it in Watir-Webdriver?"  And the answer is because so many 
people have needed it, and had to both research and implement it on their own. 
Many, many man-days have been unnecessarily wasted doing that. It is important 
functionality, and I do not believe it should be left out of a serious web 
testing tool, when it would be so easy to implement.

A problem with the above approach is that it may not resolve redirects. So an 
additional recursive method might be called for, thus:

        def get_response_with_redirects(uri, limit = 10)
            raise "Holy Crap!!! Too Many Redirects" if limit == 0
            response = Net::HTTP.get_response(URI(uri))
            case response    # Case needed to catch subclass. == doesn't work.
            when Net::HTTPRedirection
                location = response['location']
                warn "Redirecting to #{location}."
                get_response_with_redirects(location, limit - 1)
            else
                response
            end
        end

I have tested this code and it works fine. This follows redirects up to 10 or 
some other value you supply. The response after redirects should reflect the 
response from the final page. A shorter name would probably be better, though.

I strongly suggest incorporating this, or something very much like it, into 
Watir-Webdriver. It will solve problems for lots of people, it is easy to do, 
and if THEY want to just stick to "user experience only" they don't have to use 
it. But I feel they should be able to choose, rather than spending hours 
re-inventing the wheel every time this crops up for a different user. It's only 
16 lines of code total, including the require.

Note, however, that this still does not allow you to just go to a web page in 
your driven browser, and look at the response code. It require an extra line of 
code, and it does make a separate request to the server. But it's just a HEAD 
request so it is much faster than actually loading and rendering a page.

I would be happy to provide a patch if someone will point me at the procedure 
for providing patches to Watir-Webdriver, and maybe what would be an 
appropriate location in the code.

Lonny Eachus
============


-- 
-- 
Before posting, please read http://watir.com/support. In short: search before 
you ask, be nice.

watir-general@googlegroups.com
http://groups.google.com/group/watir-general
watir-general+unsubscr...@googlegroups.com

--- 
You received this message because you are subscribed to the Google Groups 
"Watir General" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to watir-general+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to