Welcome to the fifth lesson in web automation with Leapwork.
In the previous lessons, we explored different methods of filling data into a web form, including using hard-coded data and reading data from an Excel file.
This lesson will focus on the reverse process and learn how to read values from a web page.
Specifically, we will look at how to extract text and numbers from a web page and perform verification.
We will continue from where we left off on the same D365 page in the previous lesson.
Let's proceed to Leapwork and explore how we can retrieve data from the web page.
In the previous lesson, we reached the verification point where we need to scrape the message displayed at the top of the page to confirm the successful completion of the task.
To accomplish this, we will utilize the "Get Web" blocks. In our case, since we need to scrape text, we will use the "Get Web Text" block.
To continue from the previous lesson's flow, remove the "Pass" block first and add the "Get Web Text" block.
Capture the element from the web page by following the standard capture process.
To check the result of the read operation, I'll add a Log message after the Get Web Text block and connect the "Text found" data connector of the ‘Get Web text’ block to the Log Message block.
When we run the flow from the Get Web Text block, we see that the text found matches the value displayed as a message on the page.
In more detail if we want to verify the message contains the task name correctly, we have the option to verify the text displayed in a message, for that we have a Find format attribute in the ‘Get Web Text’ block, this feature helps us to specify exactly which part of the larger text we want to be returned by specifying a pattern – or a format – that the return text should match.
Here in our case, we need to verify the task name is matched with the text entered in the ‘Task description’ field.
To capture the text, we specify the template by keeping the text before and after the “text” in the ‘Find format’ text field.
Here we want to retrieve the task name from the message displayed in the application, we put “The” as a text before
and “job is added to the batch queue.” as an after text.
Let’s do the changes in the Get Web Text block, add “The” as a text before and add “job is added to the batch queue.” as an after text.
Let’s run the flow from the Get Web Text block, we see in the results the text which we have specified in the ‘task description field of the form.
We have another feature in the ‘Get Web text’ block is a filter, which helps in verifying the text, that can be used to check if a text contains, starts with, or ends with a specific word or phrase.
To exemplify this, I select the "contains" option and insert "Task Demo 1" as the word to look for, or to make the selection dynamic I can pass this data from the Type web text block used to enter the data in the ‘task description’.
If this word is not found, the building block will fail and trigger the "Not found" connector.
if the word is found everything is good, and the top connector will trigger.
The next Use case we are automating today is to find the count of active billing schedules for the selected customer which is displayed in the Summary section and verify if it matches with the entries displayed in the table under the "Billing schedules" section.
Let’s record the steps, we need to enter the Customer account details, So capture the “Customer Account” text field and select the “Type Web text” blocks from the options.
Enter the text “000001” in the Text value field, to continue the flow,
we need to simulate pressing the Tab key on the keyboard.
To incorporate keystrokes in the "Type Web Text" block, we have a "Capture" button next to the "Text value" attribute. Click on the "Capture" button, and it will turn green. Now, press the Tab key on the keyboard. We will observe that the "Tab" keystroke is added after the text value "000001".
And click on ‘Apply’.
According to our use case, we need to capture the Active billing schedules whole block and select the Get Web Number block from the displayed options, it will return the number value,
to compare this number with the displayed Active schedules status in the billing schedule we use the Find Web element block.
Hover on the cell which contains the “Active” under the Schedule status and capture the element, Select the Find Web element block from the displayed options.
We are done with the recording part, Stop the recording by selecting the Save and Record option.
The flow is created, let’s make some changes to the flow.
"In the last two blocks, we need to make a change in the strategy. First, open the strategy editor of the 'Get web number' block. Click on the image and select 'Edit web element'.
If you look on the right side, you will see the values of the attributes. There is an attribute named 'Text' with the condition set to 'Starts with', and the value is '4Active billing such. Here, the value contains a number, and we know that the number will change as the count of Active Billing Schedules changes in the application. Our flow fails at this point, so we need to make the strategy dynamic instead of static.
To do this, change the condition to 'Contains' and remove the '4' from the value area.
Now, validate it first, and you will see that the element gets validated, and we are getting a result of 'Found 1 of 1‘. It means there is only one element on the page with this description. Click on the save button to save the changes made in the strategy.
Next, move to the next block. Here, we want the 'Find Web Element' block to return the count of the 'active' schedule status in the table. So, we need to make changes to the strategy again. Open the strategy editor as we did previously.
You will see a field called 'Use Occurrence', which is currently set to 1. We need to specify 'Use Occurrence' when we have multiple elements that match the same description. However, if we want the count of those similar elements, we simply leave this field blank.
Let's clear this field and validate it. Now, you will see that the 'Found' field in the strategy editor shows '1 of 4', and the first element is highlighted in the application. If you click on the arrow in the strategy editor, the 'Found' field will show '2 of 4', and the second element will be displayed in the application.
Once we save this strategy, we can get the total count of the 'Active' schedule status. Click on save and return to the canvas.
Next, we need to add the 'Compare' block for the comparison.
Connect the 'Number found' data connector of the 'Get web number' block to the 'Value A' data connector of the 'Compare' block. We need to compare this value with the count of the Active Schedule status, which we can obtain through the 'Find web element' block. Connect the 'Count' data connector of the 'Find web element' block to the 'Value B' data connector of the 'Compare' block.
Drag the top green connector of the Compare block, add the 'Pass' block, drag the 'Incorrect’ flow connector of the 'Compare' block, and add the 'Fail' block. The 'Pass' block will trigger when the value is matched; otherwise, the 'Fail' block will trigger.
Now, our flow is ready for execution. Let's execute the flow and see the results.
The flow has passed.
In the logs, we can see that the 'Get Web Number' block picked the number displayed in the summary of Active Billing Schedules. The 'Find Web Element' block returns the count, and the comparison is made. Both values matched.
In this lesson, we have learned, how to scrape values from the application and perform comparisons
We suggest you try replicating this flow on your own by following the video.