Tech deep-dive where we look at how users can be automatically logged into Qlik Sense to generate realistic, artificial load on the system. PowerShell and Selenium is used to automate Chrome.
During recent work to add monitoring and auto-release of Qlik Sense access licenses I had a need to automatically log in Sense users and assigning various licenses types to the correct users. Generating artificial load on the Sense environment, in other words.
This can be done using Qlik's very useful Qlik Sense Enterprise Scalability Tools, but those tools need to be installed and configured, which can be a bit challenging.
I wanted a stand-alone solution written in PowerShell exclusively, to make it portable and easy to get started with.
Turned out to be possible - but not without challenges.
This post dives into the details of using PowerShell to interact with Qlik Sense, using both header authentication, as well as using Selenium to really pretend to be a user logging into Sense with a username and password, using Qlik's standard form based login page.
💡
Please check out this post too for information about Butler's new feature that monitor and automatically release Qlik Sense Enterprise access licenses.
Requirements & goal
The goal was to
Create 60 users in Active Directory, then sync them to the LAB user directory in Sense.
Log in 20 users and assign professional licenses to them using a license allocation rule.
Log in another 20 users and assign analyzer licenses to them using another license allocation rule.
Log in another 20 users and give them access via analyzer capacity licenses, also handled by a license allocation rule.
A general requirement was that each user should be logged in with some random delay (a few seconds) after the previous login.
Create users in Active directory using PowerShell
Here PowerShell is used to create users with account name testuser_<num> and username Testuser<num>.
Note that all users get the same password. This is of course a really bad idea for general use, but given the current test it's considered ok.
# Import the Active Directory module
Import-Module ActiveDirectory
# Loop through and create new users
for ($i = 1 $i -le 60; $i++) {
$username = "testuser_$i"
$firstname = "Testuser$i"
$password = (ConvertTo-SecureString "abc123" -AsPlainText -Force)
$ou = "OU=Users,DC=mycompany,DC=net" # Replace with your own OU/DC information
$userParams = @{
GivenName = "$firstname"
Surname = ""
Name = "$firstname"
SamAccountName = "$username"
UserPrincipalName = "$username"
AccountPassword = $password
Enabled = $true
}
Write-Host "Creating user $username"
New-ADUser @userParams
}
Running it looks pretty boring, but gets the job done:
PS C:\tool\script> .\create_testusers.ps1
Creating user testuser_1
Creating user testuser_2
Creating user testuser_3
Creating user testuser_4
Creating user testuser_5
Creating user testuser_6
Creating user testuser_7
Creating user testuser_8
Creating user testuser_9
...
...
License allocation rules
Professional and Analyzer
Users with these licenses types will make a very basic https request to the Qlik Sense hub. The users won't open any apps or anything - just connecting via the Sense proxy service will trigger the license allocation, using these rules:
Users testuser_1 to testuser_20 will get professional licenses.
Users testuser_21 to testuser_40 will get analyzer licenses.
The rules are very basic:
Analyzer capacity
Users will only consume time chunks (in Sense's standard six minute blocks) from the available license pool once they open an actual app. Just accessing the Sense hub won't do it - an actual app has to be opened.
For that reason Selenium is used to emulate real user interactions with a web page.
The license allocation rule is very similar to the previous ones:
Opening the hub using header authentication
Let's assume that none of testuser_1 to testuser_40 have a license allocated to them.
When those users make a https call to Qlik Sense using header authentication (why header auth? Because it's easy to set up... but it's not for production scenarios, typically) they get professional and analyzer licenses, respectively.
A Sense virtual proxy with header auth has been set up with a prefix of hdr, resulting in a Sense URL of "https://qliksense.ptarmiganlabs.net/hdr/hub".
For the users that will receive professional licenses (i.e. testuser_1 to testuser_20) the PowerShell script looks like this:
Note how the user ID is sent in the X-Qlik-User http header in the script above.
The script for the Analyzer users is identical, except that it loops from 21 to 40 instead.
Using Selenium from PowerShell
This turned out to be somewhat of a challenge, but in the end possible after all.
The PowerShell script tries to install everything needed, Chrome being the exception. A recent/latest Chrome version is assumed to be installed before running the script.
The script has been tested on Windows 11, running in Visual Studio Code's (VSC) embedded PowerShell environment.
I could not get the script to work in a stand-alone PowerShell environment. Visual Studio Code does something behind the scenes that makes .Net packages available to Selenium... Running in VSC works well though.
The script will
Download and install Nuget (Windows package manager from Microsoft)
Download and install Selenium WebDriver. Used to automate/remote-control Chrome.
Get the correct Selenium Chrome Driver, matching the installed version of Chrome.
Loop over all 20 users, opening a specific Sense app for each of them.
A different virtual proxy is used compared to the previous script. It uses form based authentication, which means that Sense will present a login web page that Selenium can use and navigate.
Selenium outputs a bunch of warnings in the console window, but nothing to worry about it seems.
Looks like this:
The script is a bit more complex than previous one: