Botmation Documentation
Loops
Loops
These BotActions are dedicated to running lines of BotActions in loops.
For All
forAll()()() is a BotAction that iterates a collection (an array or an object with key-value pairs) provided in the first call firstAll()
with the callback function provided in the second call firstAll()()
. The callback function returns a BotAction or an array of BotAction's to run on each loop iteration.
Let's see an example:
await chain( forAll(['google', 'facebook'])( (siteDomain) => ([ // name the var whatever desired in the closure goTo('http://' + siteDomain + '.com'), screenshot(siteDomain + '-homepage') ]) ))(page)
Here we pass in a collection of site domain's in the first call of forAll()
then in the second call pass in a callback function, to run BotAction(s) on each iteration of the collection. Therefore, the BotAction's returned by the callback function, in the second call, are ran in a loop for each item of the iteration, in this case, each domain name.
When the forAll()()() BotAction completes, it will have visited and taken screenshots of each site in the array.
Learn more about forAll()()
by reading its documentation.
For As Long
forAsLong()()() is a BotAction that works like a functional while loop. It resolves the ConditionalBotAction before running each loop iteration. If it does not resolve true
, then it stops.
With this you could write a dynamic BotAction that handles multiple items on a page, when you're not sure how many there are. For example, in looping through the content posted in a social media feed, news feed, etc.
Here is a concept:
await chain( // imaginary BotAction's: notAtEndOfNewsFeed & scrollToNextNewsFeedItem forAsLong(notAtEndOfNewsFeed)( screenshot(siteDomain + '-homepage'), scrollToNextNewsFeedItem ))(page)
Do While
doWhile()()() is a BotAction that works like a functional do while loop. It first runs the assembled BotAction's of the second call. Then begins a loop of running the assembled BotAction's if the ConditionalBotAction passed in the first call resolves true
before each loop iteration.
One use-case for this BotAction is "liking" posts in a social media feed. The BotAction first likes the post in the window, then scrolls down a bit. Then in a loop, likes each post while it scrolls down to the bottom of the feed, exiting the loop once the end of the page is found. So with doWhile()()
you would write a ConditionalBotAction that checks for the end of the feed, to exit your looped BotAction's. This can be done with forAsLong()()
too.
Here is a concept:
await chain( // imaginary BotAction's for navigating a social media feed doWhile(notAtEndOfSocialFeed)( givenThat(postInWindowBelongsToFriend)( likePostInWindow() ), scrollWindowToNextPost ))(page)