Web Recorder Functions (Playwright)
Overview
This section provides the commands available in Playwright scripts created with Web Recorder. These functions let you automate and interact with web applications and are grouped into categories to help you find what you need quickly:
Basic Functions: Core actions like opening pages, locating elements, and performing simple operations.
Mouse and Keyboard Functions: Simulate user interactions, including clicks, typing, scrolling, and dragging.
Browser Functions: Manage browser contexts, tabs, and sessions.
Control Flow Functions: Add logic to your automation scripts, including loops, conditions, and waits.
Basic Functions
StartBrowser
Starts a browser within an application script. We support Microsoft Edge and Chrome; the application uses whichever version of these browsers is installed on the target machine. Note that Firefox is currently not supported.
Syntax
StartBrowser()
Parameter | Type | Default Value | Description |
|---|---|---|---|
(none) | - | - | This command does not require any parameters. |
Code example
await StartBrowser();
StopBrowser
Stops a browser within an application script.
Syntax
StopBrowser()
Parameter | Type | Default Value | Description |
|---|---|---|---|
(none) | - | - | This command does not require any parameters. |
Code example
await StopBrowser();
Locator
Identifies a specific element on the page. Used as a starting point for actions such as click, hover, type, or get text.
Syntax
Locator(selector)
The locator value is generated by the Web Recorder. Creating locators manually is not supported. Please use the Web Recorder to capture locators.
Code example
Locator(selector: "#Input_Username");
StartTimer
Creates a custom timer to measure the time one or more actions take within a script. The timers are reported back to the Login Enterprise server as measurements and can be used to trigger a notification if the measurement is over the configured threshold. Measurements are displayed in the Login Enterprise dashboard and charts. StartTimer must be stopped with either CancelTimer or StopTimer.
Syntax
StartTimer(string name);
Parameter | Type | Default Value | Description |
name | string |
| Defines the name of the timer, max of 64 characters |
The timer name must only contain alphanumeric characters and underscores.
Code example
StartTimer(name:"Create_New_File");
StopTimer(name:"Create_New_File");
StopTimer
Stops a timer started by StartTimer. The StopTimer name should be the same as the StartTimer.
Syntax
StopTimer(string name);
Parameter | Type | Default Value | Description |
name | string |
| Defines the name of the timer, max of 64 characters |
The timer name must only contain alphanumeric characters and underscores.
Code example
StartTimer(name:"Save_File_To_Network");
StopTimer(name:"Save_File_To_Network");
CancelTimer
Cancels a timer. The CancelTimer name should be the same as the StartTimer.
Syntax
CancelTimer(string name);
Parameter | Type | Default Value | Description |
name | string |
| Defines the name of the timer, max of 64 characters |
The timer name must only contain alphanumeric characters and underscores.
Code example
StartTimer(name:"Create_New_File");
CancelTimer(name:"Create_New_File");
SetTimer
Helps create your own custom timers. In many cases, you can call an external value for a system resource, such as CPU utilization %, and have it appear in the Login Enterprise reporting.
SetTimer does not require a start and stop function as it collects an integer at the time it is executed.
Syntax
SetTimer("name", value);
Parameter | Type | Default Value | Description |
name | string |
| Defines the name of the timer, max of 64 characters |
value | integer |
| Value collected when the timer is called |
The timer name must only contain alphanumeric characters and underscores.
Code example
int myCounter = {some code that gets a Windows performance counter in milliseconds};
SetTimer(“MyTimer”,myCounter);
Wait
Waits for X seconds before continuing the script.
Syntax
Wait(double seconds);
Parameter | Type | Default Value |
seconds | double | - |
Code example
Wait(10);
Log
Logs to the console if you are using the Scriptrunner. When using it as part of the Login Enterprise appliance, it logs to %temp%\LoginPI\Logs.
Syntax
Log(string message);
Log(object component);
Parameters | Type | Default Value |
message | string |
|
component | object |
|
Code example
Log(message:"Log Message");
Log(component:MainWindow);
Abort
Exits the script and throws an error message. The error will be shown as an 'Application failure' on the Login Enterprise dashboard, under events.
Syntax
ABORT(string error);
Parameter | Type | Default Value |
error | string |
|
Code example
ABORT(error:"An error occurred");
ShellExecute
Runs a script or application in the Windows Shell. Only the first parameter is required; all remaining parameters are optional.
Syntax
ShellExecute(string commandLine, string workingFolder = "", bool waitForProcessEnd = true, int timeout = 180, bool forceKillOnExit = true);
Parameter | Type | Default Value | Description |
commandLine | string |
|
|
workingFolder | string |
|
|
waitForProcessEnd | bool | true | Waits for the process to finish before continuing. |
timeout | int | 180 | Defines how long to wait for the process to end before it's killed. |
forceKillOnExit | bool | true | Forces the application/process to close or stay open after the command has ended. The default is true. |
Code example
ShellExecute("mspaint", waitForProcessEnd: false, timeout: 15, forceKillOnExit: false);
CreateEvent
Generates a custom event on the Appliance, shown in the results page of a Continuous, Load, and Application Test.
Syntax
CreateEvent(title:"", description:"");
Parameters | Type | Default Value |
Title | string | - |
Description | string | - |
Code example
CreateEvent(title:"Windows Update Available", description:"Update is available on hostname x");
GetEnvironmentVariable
Retrieves an environment variable. The GetEnvironmentVariable can be stored in a variable to use multiple times in your script. Note that if a continueOnError is not set and if you do not have permission to retrieve the variable, the script will stop.
Syntax
GetEnvironmentVariable(variableName:"", continueOnError:false);
Parameters | Type | Default Value |
variableName | string |
|
continueOnError | bool | false |
Code example
var TempEnv = GetEnvironmentVariable("temp", continueOnError:true);
Log ($"The environment path = {TempEnv}");
Type ($"{TempEnv}");
TakeScreenshot
Creates custom screenshots that are sent to the Login Enterprise appliance as Events. This functionality can be used to verify an application's visual output. You must enable “Custom Screenshots” in the Test configuration to allow this function. This function is not available for Load Tests.
The Event created has a subject of “The script screenshot '{name}' was created”. The description is the {description} field in your function call. The screenshot bitmap is attached to the Event.
The name field cannot handle spaces; these need to be underscored.
Syntax
TakeScreenshot(name:"", description:"");
Parameters | Type | Default Value |
name | string |
|
description | string | null |
Code example
TakeScreenshot(name:"Notepad",description: "Check if notepad has loaded the correct file");
Mouse and Keyboard Functions
Some functions in this subsection include a timeout parameter. This parameter specifies how long the script will attempt to locate an element and perform the action. By default, Web Recorder sets the timeout to 5 seconds, but you can adjust it either in the global settings or on a per-action basis.
If the timeout is removed, the script will fall back to Playwright’s default timeout of 30 seconds.
Type
Types text or presses keys.
Syntax
await TypeAsync(input: "text", cpm: 0);
Parameter | Type | Default Value | Description |
text | string |
| Text to type or keys to press |
cpm | int | 300 | Characters per minute |
Code example
await TypeAsync(input: "some Text", cpm: 0);
Key Combinations
Key combinations can be performed using the typing function. For example, the Ctrl+V (paste) command appears in the script as:
await TypeAsync(input: "{Control+v}", cpm: 0);
Press
Presses keys, such as Key up/down, tab, etc.
Parameter | Type | Default Value | Description |
|---|---|---|---|
key | string | - | The key to press on the keyboard |
Syntax:
PressAsync(key)
Code example:
await PressAsync("Tab");
KeyCodes
Valid keys for PressAsync()
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12
Insert
Cancel
Help
Backspace
Tab
Clear
Enter
Shift
Control
Alt
Delete
Pause
Space
PageUp
PageDown
End
Home
ArrowLeft
ArrowUp
ArrowRight
ArrowDown
Escape
Meta
LeftClick
Performs a left-mouse click on the specified element.
Syntax
Locator(selector).ClickAsync(options)
Parameter | Description |
|---|---|
selector | Selector identifying the element to click. |
timeout | How long (in seconds) the script should retry locating the element before failing. |
The locator value is generated by the Web Recorder. Creating locators manually is not supported. Please use the Web Recorder to capture locators.
Code example
await Locator(selector: "#password-input").ClickAsync(new(){Timeout = 5 });
RightClick
Performs a right-mouse click on the specified element.
Syntax
RightClick(double x, double y, bool continueOnError = false);
Parameter | Type | Default Value | Description |
x | double |
| Horizontal coordinate |
y | double |
| Vertical coordinate |
The locator value is generated by the Web Recorder. Creating locators manually is not supported. Please use the Web Recorder to capture locators.
Code example
await Locator(selector: "#password-input").RightClickAsync(new(){Timeout = 5 });
DoubleClick
Performs a double-mouse click on the specified element.
Syntax
DoubleClick(double x, double y, bool continueOnError = false);
Parameter | Type | Default Value | Description |
x | double |
| Horizontal coordinate |
y | double |
| Vertical coordinate |
The locator value is generated by the Web Recorder. Creating locators manually is not supported. Please use the Web Recorder to capture locators.
Code example
await Locator(selector: "#password-input").DoubleClickAsync(new(){Timeout = 5 });
Mouse Scroll
Scrolls the page relative to the current mouse position by the given number of pixels horizontally (x) and vertically (y).
Parameter | Type | Default value | Description |
|---|---|---|---|
x | - | - | Horizontal scroll amount in pixels (negative = left, positive = right) |
y | - | - | Vertical scroll amount in pixels (negative = up, positive = down) |
Code example
// Horizontal scroll at (0,0)
await ScrollAsync(x: 0, y: 0);
// Scroll down at (0,659)
await ScrollAsync(x: 0, y: 659);
// Scroll up at (0,-32)
await ScrollAsync(x: 0, y: -32);
Hover
Moves the mouse pointer over the specified element. It is used to trigger hover effects like tooltips, dropdowns, or style changes.
Syntax
Locator(selector).HoverAsync()
The locator value is generated by the Web Recorder. Creating locators manually is not supported. Please use the Web Recorder to capture locators.
Code example
await Locator(selector: "#password-input").HoverAsync();
Control Flow Functions
If Else vs Try Catch
Creates conditional logic in your scripts. If the condition is true, the code within the If block executes. If the condition is false, the code within the Else block executes. Note that the Else statement is not mandatory.
Code example
var Condition = MainWindow.FindControl(title:"Condition");
if (Condition != null)
{
Type("Condition is true");
}
else
{
Type("Condition is not true");
}
Challenges:
Finding a control may throw an exception if the control is not found. If "Continue on Error" is not enabled, this will terminate the workflow.
Solution
Reword the script using a Try...Catch statement. This approach can eliminate inconsistent pop-ups and allow the script to continue running even if a control is not found.
Code example:
Try{
var Condition = MainWindow.FindControl(title:"Condition");
Condition.Click();
}
catch{
Log("Was unable to find conditional window");
};
While
Executes a block of code in a loop as long as the specified condition evaluates to true. The condition is evaluated before each iteration of the loop.
Code example:
In the following example, we specify an integer variable that equals zero. While the number is less than 10, we log the number to the console and add +1 to it until it evaluates to false.
int number = 0;
while(number < 10)
{
Log(number);
number++;
}
ForEach
Iterates through each item in an array of values.
Code example:
In the following example, we iterate through 3 sample values.
string[] example = {"example 1","example 2","example 3"};
foreach (string value in example)
{
Log(value);
}
Additional uses
When working with a browser application, collecting all web selectors can be beneficial. You can then use a ForEach loop to find a specific condition or to identify a particular selector from an array of many.