Advanced software testing concepts can save you significant time and energy. Repeato helps you improve your testing processes.
Ever wondered how tests could be created without repeating yourself? Test automation makes it possible to record tests and re-use them as you like – a huge advantage of automated vs. manual testing.
In this video, we show how you can divide your tests into reusable parts. We will also cover some of the scripting possibilities of Repeato and show why it can be useful to make test steps optional. Our subject under test is the “Asana” to-do list app. We will test creating to-do’s.
Video content
Here is an example of a more complex automation case. We automate a game (power pop bubbles) with a couple of optional “check content steps” and with some lines of code.
Check out the detailed code described below.
When you define a function, usually it is only accessible inside the same step. We want to call our function in other steps, so what we need to do is to use one of the longer living objects for storing the function. data is such an object and we can define a function like this:
data.checkLastMatchAndShoot = async function() {
// myCode goes here
}
We add async because we are using asynchronous code inside of our function. Basically, all functions which are used with await, need to be wrapped in async functions. Otherwise our script will fail. You can read more about async / await here.
After executing an optional step, we can check in our script if the returned result was successful. We can access the last step execution result via testRunner.lastStepResult.
There is also a way to check how many matches there were (how many times was the object found):
testRunner.lastStepResult.matches.length
So if there were no matches, meaning that there were no balls of a certain color found, we just return.
Otherwise, we sort the matches from top to bottom. This is going to be important because we need to distinguish between the ball in the launcher and the balls in the game field above.
We do this by checking the y position value of the match:
// if y position of last match is lower than 70% of the screen height
if(lastMatch.position.y > 0.7)
So if we found a ball in a certain color we need to know in which direction to shoot right?
We do this by checking the result set of matches and take a closer look at the previous match (the one right above the ball in the launcher)
const oneBeforeLastMatch = matches[matches.length - 2]
We can use this match to define the direction for our shoot. To shoot we instruct a drag gesture from the ball in the launcher to the ball of the same color right above.
To get the big picture, here is the whole code:
data.checkLastMatchAndShoot = async function() {
var matches = testRunner.lastStepResult.matches
if(matches.length === 0){ // no ball of this color found
return false
}
// we sort the matches from top to bottom
matches = matches.sort((match1, match2) => match1.position.y - match2.position.y)
// get the most bottom match
const lastMatch = matches[matches.length - 1]
// if most bottom match is not in the field, but at the very bottom, this means that it is inside the launcher
if(lastMatch.position.y > 0.7) {
const oneBeforeLastMatch = matches[matches.length - 2]
testRunner.deviceConnector.sendDown(lastMatch.position.x, lastMatch.position.y)
await sleep(1000)
testRunner.deviceConnector.sendMove(oneBeforeLastMatch.position.x, oneBeforeLastMatch.position.y)
await sleep(1000)
testRunner.deviceConnector.sendUp()
return true
} else {
return false
}
}