Basic Authentication

Testing a basic authentication dialog works straight forward using record and replay with Silk Test and Internet Explorer. Unfortunately, the WebDriver based browsers do not support testing basic authentication dialogs until now (March, 2018). It is recommended to use implicit authentication for tests. This can be achieved by adding the credentials to the particular URLs:

https://username:password@www.example.com/

However, if this is not feasible, the following code snippets can be used as a work around for some specific scenarios. The used scripting language is VB.NET for Silk Test Workbench but it could also be ported to other Silk Test client languages.

Chrome

When a basic authentication dialog pops up any further test interaction is blocked. The trick is to use native Window key events to navigate to a URL that needs authentication and to enter username/password. Ensure that the URL that is configured in the Browser Application Configuration of the test must not need authentication or is set to 'about:blank':

Imports SilkTest.Ntf.XBrowser
Imports SilkTest.Ntf.Win32
Imports System.Threading

Public Module Main
  Dim _desktop As Desktop = Agent.Desktop
 
  Public Sub Main()
    ' once an encrypted password token is generated it can be used in password fields instead of plain password
    System.Console.WriteLine("Password token: " + Agent.Encrypt("admin"))
    
    ' enter URL and navigate
    With _desktop.AccessibleControl("//BrowserApplication//AccessibleControl[@caption='Address and search bar']")
      .Click()
      .TypeKeys("<Ctrl+A>")
      .TypeKeys("http://the-internet.herokuapp.com/basic_auth")
      .TypeKeys("<Enter>")
    End With
    
    ' wait a moment until document is loaded
    Thread.Sleep(2000)
    
    ' enter credentials
    With _desktop
      .TypeKeys("admin<Tab>")
      .TypePasswordKeys("5ERzHMbN") ' password token for "admin"
      .TypeKeys("<Enter>")
    End With
    
    ' verify basic authentication succeeded
    With _desktop.BrowserWindow()
      Workbench.Verify("Basic Auth", .DomElement("//H3").Text)
    End With
  End Sub
End Module

Edge

Has the same behavior as Chrome. The script differs slightly in how the address field is activated:

Imports SilkTest.Ntf.XBrowser
Imports SilkTest.Ntf.Win32
Imports System.Threading

Public Module Main
  Dim _desktop As Desktop = Agent.Desktop

  Public Sub Main()
    ' once an encrypted password token is generated it can be used in password fields instead of plain password
    System.Console.WriteLine("Password token: " + Agent.Encrypt("admin"))

    ' enter URL and navigate
    With _desktop    
      .TypeKeys("<Tab><Tab><Tab><Tab>", 50) ' activate the address field
      .TypeKeys("http://the-internet.herokuapp.com/basic_auth")
      .TypeKeys("<Enter>")
    End With
          
    ' wait a moment until document is loaded
    Thread.Sleep(2000)            
    
    ' enter credentials
    With _desktop
      .TypeKeys("admin<Tab>")
      .TypePasswordKeys("5ERzHMbN") ' password token for "admin"
      .TypeKeys("<Enter>")
    End With
    
    ' verify basic authentication succeeded
    With _desktop.BrowserWindow()
      Workbench.Verify("Basic Auth", .DomElement("//H3").Text)
    End With
  End Sub
End Module

Firefox

For Firefox the alert API can be used to handle basic authentication dialogs in general. With that no special navigation is needed and username/password can be set:

Imports SilkTest.Ntf.XBrowser

Public Module Main
  Dim _desktop As Desktop = Agent.Desktop
 
  Public Sub Main()
    _desktop.BrowserApplication.Navigate("http://the-internet.herokuapp.com/basic_auth")
    
    ' enter credentials using Alert API backed by Selenium
    Dim seleniumTabKey = Convert.ToChar(&HE004)
    Dim myUserName As String = "admin"
    Dim myPassword As String = "admin"
    _desktop.BrowserWindow().AcceptAlert(myUserName & seleniumTabKey & myPassword)
    
    ' verify basic authentication succeeded
    With _desktop.BrowserWindow()
      Workbench.Verify("Basic Auth", .DomElement("//H3").Text)
    End With
  End Sub
End Module