In this video we gonna look at how you can work with numbers in Leaptest and how you can use variables in your cases.
I’m gonna use a very simple example on my local computer as basis for the video it’s just the amount of free space on my hard drive – in this case I have 107 GB free.
First I want to read the amount of free space on the hard disk and I will use a Get Number block for this.
If I just run this simple test case, it will search the entire screen and return whatever number it found. This takes too long and it’s not accurate so we can do 3
things to ensure we get the amount of free disk space returned.
The first one is to add a filter, which tells the building block to search for a number that exists in the context of the filter. In this case we will add the text “GB free” after the number token, so the building block will search the screen for a number that is located in front of the text “GB free”.
Let’s run the case
It found the value “107” correctly.
The second way to ensure, we find the right number, is to define an area to search for a number in. If I right-click in the area field, I can capture an area where I know that the number will be in.
- Capture the area around the number
As you can see, I captured an area that included other numbers which could affect finding the correct number. This leads me directly to the third way of ensuring that we read the right number – the combination of using an area and a filter.
Using both an area and a filter optimizes speed and accuracy, and is definitely an example of best practices.
To illustrate what you can do with the numbers I’m just gonna capture the total disc space and will do a comparison of the 2 numbers found. I shouldn’t never have more free space than the total size of my hard drive, so let’s test that.
I’ll add a new Get Number after the first block and add both an area and a format as we did previously.
- Add Get Number
- Set Area
- Set format: of NUMBER GB
Now I add a Compare block and add the found numbers in as Number A and B.
- Connect found numbers to compare block
In this case I would like to make sure that the free space is less than the total size, so I select the method “Less than”. If Number A is less than Number B then
the top connector will be triggered and we can add a Pass block.
If this is not true, then it will trigger the “Incorrect” connector and we can add a Fail block.
Let’s run the test case
The test case passed so I don’t have more free space than my total disk size.
The next thing we will look at is variables. What I would like to test is that the amount of free space on my disc between each run of the test case has not decreased by more than 20%. So if I had 100 GB free I can accept that it falls to 80 GB between 2 runs of the test case, but if it decreases more, the test case should fail.
I’ll create a new test case for this, and add a Get Number as the first building block.
I’m reading the amount of free disc space the same way as before.
When I work with variables I can choose between 2 building blocks: Get and Set variable. Let’s add a Set Variable after the Get Number.
In this building block you can add a name to the variable. In this case we will call it ‘FreeGB’. The second field is the value to add to the variable, and we will
connect the found number with the value field.
If I expand the Set Variable building block we get the option of selecting the Scope of the variable. The variable can exist in 3 different Scopes which all
decides how long the Variable should live:
- If I select “Only this case” the variable will only be available while this test case run.
- If I select “all cases in this run” the variable will exist for the duration of a set of test cases executed in a schedule.
- In this case we select “Permanent” which means that the variable will live for ever and can be access by this and other test cases at any time.
If we run the test case now we should see that the amount of disc space is added to the new variable.
As we can see the number is added to the new variable.
To read the value, I use a Get Variable block and use the same variable name and type as before. I’m just gonna move the first 2 blocks and add both a Get Variable and a Pass block.
- Move blocks
- Add Pass block
- Add Get Variable
- Set Name and Type
If I run the case now we should see the value read from the variable in the log.
As we can see the value was correctly read from the permanent variable.
So with this concept in place we can construct a real case.
I’ll start by reading the current free disc space using the Get Number block, and then I’ll add a Compare block. The first value is the current value and the second
is the value of the variable multiplied by 0.8. This way we can check if the amount of free space is larger than 80% of the amount from the last run.
I add a Calculate block and multiplies the value from the last run with 0,8 and add the result number to the Compare block. I’ll change the comparison method
to “Greater than”. If the new value is larger than 80% of the saved value we are all good and the top connector is triggered.
We then use the Set Variable to update the value in the variable and end with a Pass block. If the new value is below 80% of the old value then the amount of free disc space decreased to fast and we will fail the test case.
One last thing: The first time you run the test case, the variable is not set, so we will use the newest read value as the default value for the Get Variable block.
We are now ready to run the test case.
The case passed and we can see, that the value 107 was compared to 85,6. To make a little cleaner, we can change the Calculate block to return a whole number by checking the Round result and specify 0 as the number of decimals.
This video showed examples of how to work with numbers, using the math functionality and how to Set and Get variables in different scope.