LEAPWORK is a great match for automating ServiceNow. In this article, we show one example of how to do that, using dynamic forms and fields from an Excel sheet.
A major part of automating ServiceNow is filling in forms of with varying number of fields and complexity. Instead of using the web blocks to point and click one field at a time, it's possible to add functionality to cases that will allow you to specify field types, field names and field values in an external data source -- for instance, an Excel file. This means you can control what value goes where from within your data source, and you don't have to change the cases when new fields are added or removed. Just update the list of fields in Excel.
In this example (download project sample), we have created a simple Excel file that covers some of the basic fields in a ServiceNow Change Request. The columns in the Excel sheet are:
- Field Type: This value is use to determine if the field is a text field or a dropdown. More field types can be added if needed.
- FieldID: The ID of the field that the value should be set for. In the case of ServiceNow the ID for fields is static and easy to find by selecting "Inspect element" on a field in a form, and then copy the "id" property.
- Value: The value for the field.
The Excel sheet looks something like this:
Added to the flow in a Read Excel block, it will look like this:
The method is set to Iterate, which means all the rows in the selected data range will be iterated over, so the functionality to find the individual field and set the value should "just" be attached to the top connector in the Read Excel block.
Next, we add a Switch block. This is used to branch the flow into different functionality based on the field type. If it's a text field, the procedure is different from a dropdown field. The Switch block contains two "cases", which are matched against the value specified in the Field Type column in the Excel sheet:
If the Field Type is set to "text" then the green connector for "Case 1" is triggered, if the value is "dropdown" then the "Case 2" connector is triggered. Each of the connectors for the two cases are attached to custom building blocks specific to the field type as shown below:
Further the FieldID and the Value are also added to the custom building blocks, by connecting the properties from the Excel sheet to the custom building blocks. This allows the custom building blocks to both find the field and set the value. This is a good example of how you can encapsulate functionality in custom building blocks making it easy to re-use the functionality without knowing everything about the "magic" under the hood.
Let's have a look at the "Set Text Field" custom building block to see how it's using the input parameters:
The custom block has 2 blue Value Input blocks defining the "Field ID" and "Field Value" properties on the custom block. The "Field ID" property is connected to a Click Web Element block, that uses the input as part of the selector strategy, i.e. the criteria used to uniquely find the individual web element on the page. To view how the "Field ID" input is used, we can click on the actual element and select "Edit web element"
This will open the Web Editor, giving access to viewing and editing the conditions used when the test case runs.
In this case the selected strategy is looking for a html field ('input') inside an iframe with an ID that equals 'gsft_main'. The name property of the html field has to match what is added into the token "Field 1". "Field 1" is added by right-clicking inside the field and selecting 'Insert token - Add new field'. After clicking "Save" in the Web Editor, the newly added "Field 1" is now available on the Click Web Element block itself, and in this case connected to the "Field ID" input. You can find more information about working with dynamic selection criteria in the article "Add parameters to selection criteria for captured web elements".
Once the correct html field is found using the dynamic input of the Field ID to the locator strategy in the Click Web Element block, the value can be inserted using a Type Web Text block, that simply outputs the text from the Field Value input.
The complete test case for opening ServiceNow, creating a new Change Request, reading and inserting values from an Excel sheet and finally submitting the Change Request looks like this:
To import the example case into your local LEAPWORK installation, download the project sample, and then follow these steps:
- Select "Projects" in the main menu in LEAPWORK and select the project you want to import the case into.
- Click "In-Out" at the top of the list of the projects.
- Click "Import" and point to the file you just downloaded in the dialog.
- Click Import and close the dialog after the "Import is finished" is shown.
- The full test case is now in the selected project.
- Open the "Open SNOW & Login" custom building block and change the URL, username and password to login to your own ServiceNow instance.
Dig deeper into web automation with the LEAPWORK guide on everything you need to know to automate web-based tests and processes.