I also wondered why this regular expression script was in Watir's
source, since it is actually really easy to get status with Net::HTTP.

Anyway, here are 2 examples I just did which could be used. First one
returns response status code and response class. It will throw
exception if web server isn't running for example. Here's the script
itself:

require 'net/http'
require 'uri'

def page_status url
  url = URI.parse(url)

  http = Net::HTTP.new(url.host, url.port)

  http.start do
    http.request_get(url.path.empty? ? "/" : url.path) do |res|
      return {:name => res.class, :code => res.code}
    end
  end
end

status = page_status("http://localhost";)

puts status[:name]
puts status[:code]

-----
Output would be something similar to:
Net::HTTPOK
200

But I doubt that you actually want to know the exact response code.
You probably just want to know if the page loaded correctly with
status 200, right? Anyway, then you could use something like this
instead:

def page_ok? url
  url = URI.parse(url)

  http = Net::HTTP.new(url.host, url.port)


  begin
    http.start do
      http.request_get(url.path.empty? ? "/" : url.path) do |res|
        return false unless res.kind_of?(Net::HTTPSuccess) # or
Net::HTTPOK if you want to exclude 201, 202, 203, 204, 205 and 206
      end
    end
  rescue => e
    puts "Got error: #{e.inspect}"
    return false
  end

  true
end

puts page_ok?("http://localhost";)

-----
Output would be true

It seems pretty easy and straightforward to me like most of things in
Ruby :) Anyway, maybe also Watir's own script should be changed to use
some similar solution? It would have to be modified a little of course
to work with https also so Watir could check itself if page was loaded
or not and throw error or something?

Anyway, hope that one of these solutions works for you.

You can of course read detailed information about Net::HTTP from Rdoc
at http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html

Regards,
Jarmo


On Apr 23, 5:17 am, Paul Rogers <paul.rog...@shaw.ca> wrote:
> i could never find a way of getting the status code easily. If you look
> through the watir source it does something like use a regular expression
> against the page title to try and figure it out. At one point watir would
> raise an exception when the page finished loading if it was an error, I
> think that is now off by default
>
> Paul
>
> On Wed, Apr 22, 2009 at 7:29 PM, Tony <ynot...@gmail.com> wrote:
>
> > Hi Paul,
>
> > Would suggest you use fiddler which is a free tool.(http://
> >www.fiddler2.com/fiddler2/)
> > You could run fiddler while your scripts are executing and capture the
> > status code, urls hit etc from a different process.
>
> > You could also try httpwatch (not free)http://www.httpwatch.com/
> > This supports ruby programming and could be used along with watir to
> > get the results you need.
>
> > If anyone has a better solution using COM, iam waiting to hear.
>
> > -Tony
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Watir General" group.
To post to this group, send email to watir-general@googlegroups.com
Before posting, please read the following guidelines: 
http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to 
watir-general-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/watir-general
-~----------~----~----~----~------~----~------~--~---

Reply via email to