Automate Safari browser using macOS Script Editor
Using AppleScript the Safari browser can be remote controlled, automatically logging several users into Qlik Sense.
During recent work on the open source Ctrl-Q tool for Qlik Sense I needed to log in as several users simultaneously.
Four users logged into Sense in their own incognito/private browser windows seemed like an easy solution.
This can of course be done manually, but that quickly becomes rather time-consuming and boring. Better to automate it.
There are various tools available for browser automation, Selenium IDE comes to mind.
Seemed like overkill though and it turns out that good old AppleScript does this quite nicely.
The code snippet below works in the standard macOS Script Editor app, just replace URL, userid and password as needed. If needed also adjust the position of each window.
-- Get display size
tell application "Finder"
--gets the coords of the desktop
set screenSize to bounds of window of desktop
end tell
-- display dialog "Display size: " & (item 1 of screenSize) & ", " & (item 2 of screenSize) & ", " & (item 3 of screenSize) & ", " & (item 4 of screenSize)
--
-- testuser_1
tell application "Safari"
activate
tell application "System Events"
click menu item "New Private Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Safari"
end tell
-- The frontmost window is the private Browsing window that just got opened
-- change the URL to the one we want to open.
set URL of document 1 to "https://qliksense.ptarmiganlabs.net"
set bounds of window 1 to {0, 0, (item 3 of screenSize) / 2, (item 4 of screenSize) / 2}
end tell
if page_loaded(10) then
tell application "System Events"
keystroke "LAB\\testuser_1"
delay 1
keystroke tab
keystroke "user_1_secret_pwd"
keystroke return
end tell
else
display alert "The URL Has Not Loaded."
end if
--
-- testuser_2
tell application "Safari"
activate
tell application "System Events"
click menu item "New Private Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Safari"
end tell
set URL of document 1 to "https://qliksense.ptarmiganlabs.net"
set bounds of window 1 to {(item 3 of screenSize) / 2, 0, (item 3 of screenSize), (item 4 of screenSize) / 2}
end tell
if page_loaded(10) then
tell application "System Events"
keystroke "LAB\\testuser_2"
delay 1
keystroke tab
keystroke "user_2_secret_pwd"
keystroke return
end tell
else
display alert "The URL Has Not Loaded."
end if
--
-- testuser_3
tell application "Safari"
activate
tell application "System Events"
click menu item "New Private Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Safari"
end tell
set URL of document 1 to "https://qliksense.ptarmiganlabs.net"
set bounds of window 1 to {0, 550, (item 3 of screenSize) / 2, 1100}
end tell
if page_loaded(10) then
tell application "System Events"
keystroke "LAB\\testuser_3"
delay 1
keystroke tab
keystroke "user_3_secret_pwd"
keystroke return
end tell
else
display alert "The URL Has Not Loaded."
end if
--
-- testuser_4
tell application "Safari"
activate
tell application "System Events"
click menu item "New Private Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Safari"
end tell
set URL of document 1 to "https://qliksense.ptarmiganlabs.net"
set bounds of window 1 to {(item 3 of screenSize) / 2, 550, (item 3 of screenSize), 1100}
end tell
if page_loaded(10) then
tell application "System Events"
keystroke "LAB\\testuser_4"
delay 1
keystroke tab
keystroke "user_4_secret_pwd"
keystroke return
end tell
else
display alert "The URL Has Not Loaded."
end if
--
on page_loaded(timeout_value)
delay 1
repeat with i from 1 to the timeout_value
tell application "Safari"
if (do JavaScript "document.readyState" in document 1) is "complete" then
return true
else if i is the timeout_value then
return false
end if
delay 1
end tell
end repeat
end page_loaded