Our vBulletin migration is complete.
Welcome vBulletin users! All content and user information from the Micro Focus Forums (vBulletin) site has been migrated to this site. READ MORE.

Basic Authentication

Gerald Ehmayer Absent Member.
Absent Member.
2 0 794

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

 

The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.