Functions in BrowserAutomationStudio
In this article you will learn how to work with functions in BrowserAutomationStudio. We will examine creation of the first function, its structure, proper setup and ways of calling it in the project logic
This article was prepared based on the old version of the interface. Despite possible visual changes, the functionality and sequence of actions remain relevant
1. Why functions are needed, their advantages
The main purpose of function — simplification of logic reuse and facilitation of code maintenance
Instead of copying the same block of actions to different parts of the script, you create a function and use it in the needed places. At the same time any change inside the function is automatically applied in all its calls
Imagine that you need to use the «Sleep» action in several places of the project. One way is to manually copy this action each time. However, if later it's required to change its settings, you'll have to edit each copy separately, which takes a lot of time
It's much more efficient to design this action as a function:
- Create a function with «Sleep» action
- Set it up once
- Call this function anywhere in the project with just a couple of clicks
Such approach saves time and makes your project more structured and easily maintainable. Let's examine how to properly create a function in BAS and set it up
2. Creating functions
1. Click on the function inspector button
2. Click on the red icon with plus to create a function
3. Click on the «Details» button to display all function fields
The result of executing the steps will be transition to function settings:
2. Detailed description of all sections
Function name — this is a field in which the function name is set. It's recommended to use clear and descriptive names, so that it's easy to understand the function's purpose and quickly find it among others
For example — LoadPage_SelAwait_MoveMouseAroundElem. This name reflects the essence of the function:
Function description — so that later you don't get confused among other functions, try to leave a description
This parameter is hidden when calling the function and is visible only in the function editor
Function return value — this is the result that the function passes after executing its logic. If you want the function to return some value (for example, calculation result, file path, text, etc.), it's necessary to enable the «Return value» option
After activating this option BAS automatically creates a variable with the name FUNCTION_RESULT, into which the result will be written. If necessary you can rename it to any convenient name
Function parameters — this is data that is passed to the function for its work. These can be strings, numbers, boolean values, expressions or resources. The function uses the passed parameters to execute its logic based on this data
Visual representation of all properties using the example of LoadPage_SelAwait_MoveMouseAroundElem function
Function parameter settings: Click the «Add parameter» button to open a form with fields for its configuration
- Name: in this field enter a convenient parameter name
- Type: there are 6 different variations for the «Type» field in total:
- String — value is passed in text format
- Number — value is passed in numeric format
- Boolean type — logical value that can only be true or false
- Expression — value can be an expression that is calculated during script execution
- String or expression — user can choose: enter a string or use an expression
- Number or expression — user can choose: enter a number or use an expression
Number or expression / String or expression — if you choose one of these options, when calling the function the user will be able to choose the value type through a dropdown list: manual input or using an expression. In other cases only one fixed type of value is displayed without choice
- Description: text explanation for the parameter, which is displayed as a hint (question mark in the function interface)
- Default value: this is an auto-filled value substituted into the parameter when calling the function in the project logic
Visual representation of all parameters when calling the function
3. Example of filling a function and its call in project logic
1. Function task - LoadPage_SelAwait_MoveMouseAroundElem
- Load the page specified by user
- Wait for page loading
- Selector waiting
- If an error occurs during function execution, the boolean value
false
will be passed to the return variable. If the function executes successfully —true
will be returned
2. Filling function settings
Function name: LoadPage_SelAwait_MoveMouseAroundElem
Function description: Page loading, selector waiting, moves mouse over selector, after the function will return the result of work to variable RESULT_FUNCTION_LOAD
Return value: Enable the «Return value» option and specify the variable name — RESULT_FUNCTION_LOAD
Return value description: In the description field you can specify the following
«If any action in the function ends with an error, the boolean value false will be passed to the return variable. If the function executes successfully — true will be returned»
Parameter #1
To pass a link to the function add a parameter with type «String or expression». This will provide flexibility when passing both static strings and expressions
Parameter #2
The second parameter passes selector for the «Element waiting» action. Type — «String or expression» for flexibility. Also a default value is set for convenience and input acceleration
After filling all parameters and function body press the green «Save changes» button to save the entered settings
After saving changes, BrowserAutomationStudio will automatically redirect you to the created function and add three basic actions:
- The «Get Function Parameter» action is used to receive resources or values passed to the function. With its help inside the function you can access these parameters and use them in the execution logic
- The «Return» action is used to return the result from the function to the main project logic. It completes the function execution and passes the specified value to the variable set when configuring the return result
3. Calling function into project logic
Then select the function «LoadPage_SelAwait_MoveMouseAroundElem» from the available list
When opening the function call settings, the following four fields are displayed:
Filling fields when calling a function
Now, to correctly implement the function logic, it is necessary to pass data into it. Below is an example of a filled function when calling it:
We have completed filling the parameters and called the function. Now we move to its internal logic to configure and refine the actions inside it.
To ensure parameters are correctly passed to the function, be sure to execute the «Function Call» action. Otherwise, the parameters will have a value of 'null
'.
Final function code
Based on the given task, the final implementation of the function looks as follows:
🎉 The function is ready
Thank you for your attention!
In this article we examined the process of creating functions in BrowserAutomationStudio: studied their structure, filling procedure and methods of calling them in project logic. This will allow you to organize code more efficiently, reduce duplication and simplify its maintenance.
Additional study materials:
1. Step-by-step guide to creating a module in BrowserAutomationStudio
2. Module localization. How to rename actions in the module