Test sites with native Windows authentication

By mark

For some sites (especially in .NET world), users are presented a native windows' authentication window (like below) before can access to it.

The standard open_browser in Watir won't work (get stuck), as it will be waiting response from the browser.

    open_browser("http://secure.site.com") # will stuck
  

To add a bit more complexity, we expect the same tests to pass whether user has already logged in or not.

The solution is:

  • Detecting Login Window using Ruby's timeout
  • Use AutoIt to fill user name and password in native Windows Login Window.
    begin
      Timeout::timeout(5) {
        open_browser("http://secure.site.com")
      }
    rescue Timeout::Error : e
      # debug "Timeout error on get to site, maybe asking for login"
      autoit = WIN32OLE.new('AutoItX3.Control')
      win_title = "Connect to edam.bluefirems.com.au"
      ret = autoit.WinWait(win_title, '', 10)
      if ret
        autoit.ControlSetText(win_title, "", "Edit2", username)
        autoit.ControlSetText(win_title, "", "Edit3", password)
        autoit.ControlClick(win_title, "", "Button3")
      end
   end
  

The above test scripts will work, but not readable. By applying 'Extract Function' and "Move to Helper" Refactorings in TestWise, we get:

    # ...
    rescue Timeout::Error : e
      # debug "Timeout error on get to site, maybe asking for login"
      # calling helper function in test_helper.rb
       
      login_as("zhimin@secure.site.com", "Secret")    
    end