Sometimes you’re on a secure page, sometimes you’re not. How to know? There’s a very simple method you can write to find out, based on the browser’s current url. Here’s the method:
def is_session_secure?
@browser.url =~ /^https/ ? true : false
end
It’s worth noting that the method highlights how much you can get done with very little code using ruby and watir. Here’s what it does: first, get the url out of the browser. Then, see if the url matches a simple regex. Since this operation is done in a ternary operation, a simple true/false will result. And, since ruby returns what the final line in a method resolves to, the true/false result of the match will be returned. Another nail in the coffin of big and bloated UI automation tools!
Here’s an example of the code in use…
require "rubygems"
require "firewatir"
require "test/unit"
include FireWatir
class GoogleSecurePageTests < Test::Unit::TestCase
def setup
@browser = Firefox.start("http://www.google.com")
end
def test_google_https
assert(!is_session_secure?)
@browser.link(:text, "Sign in").click
assert(is_session_secure?)
end
def is_session_secure?
@browser.url =~ /^https/ ? true : false
end
end