Logo-English

GR

 

 

QA AUTOMATION ENGINEER

 

Filling a form using a CSV on iMacros part 2 (Loops)

On the previous post we already learn how to call a CSV document using the command

SET !DATASOURCE file.csv

 And we also learn that once "declared" the document, we can extract from it, the data of each column, calling each of those columns the following way

{{!COL#}}

 where # is the column number, using this variable in a TAG command. For example:

TAG POS=1 TYPE=TAGTYPE ATTR=SOMEATTR:someattrvalue CONTENT={{!COL1}}

 When we did this on the previous post, we realized that when clicking on Play button, it only fills the form using just the first line of the CSV document. In this post we will learn how to call each one of the lines of the CSV document creating a "loop" in that way we can use all the document to perform the same task several times.

 To accomplish that, we are going to add two more lines to our script.

SET !LOOP X
SET !DATASOURCE_LINE X

 Where X can be a number or a variable. In our example, we will use both, a number and a variable.

When we go to use our test in a "loop" we are not going to use the Play button. We need to set first the total number of loops by using the field Max: on our iMacros.

iMacrosEng

And after that clicking on Play (loop) button.

!LOOP

This variable holds the value of the current loop number. Therefore when we start the script this variable is equal to 1 by default. As soon as it ends the first pass of all our script, immediately when starting the second loop the value of this variable is the previous one + 1. For example:

Starting a loop 

!LOOP =1

On the second loop

!LOOP = 2

However, we can tell it which is the first loop we want to use to start. This is useful when we have a title row in our CSV document and we don't want to start with the first row of the document in our script. Something like the following example:

Name, Lastname, Address, City, State, Country, ZIP, Phone
Charles,Sexton,P.O. Box 701, 883 Tortor. Av.,Camaçari,Bahia,Lesotho,12345,640-890-8662 
Flynn,Green,P.O. Box 269, 2190 Amet, Road,St. Veit an der Glan,Kt,Albania,89234,936-182-5814 
Baker,Strickland,P.O. Box 464, 2785 Curabitur Av.,Tacoma,WA,Mali,18456,729-741-7444 
Beck,Adkins,9622 Aliquam Rd.,Ajaccio,Corse,Mauritius,09234,547-381-7344 
Nita,Turner,P.O. Box 161, 8181 Senectus St.,Middelburg,Zl,Niger,12584,872-429-6004 
Kiara,Bruce,P.O. Box 619, 4942 Risus. Ave,Leeuwarden,Friesland,Saint Pierre and Miquelon,23394,429-848-4599

 Or when our test stopped at row 50 and we planned to run 100 rows, we can tell it to start from the row 50 using this variable using:

!LOOP 50 

and combining it with !DATASOURCE_LINE

!DATASOURCE_LINE

This variable allows us to tell wich line of our document we want to use. It can take numerical values or another variables, like !LOOP  variable. For example, if we want to use the row 3 on our previous example:

Flynn,Green,P.O. Box 269, 2190 Amet, Road,St. Veit an der Glan,Kt,Albania,89234,936-182-5814

We just need to type the following:

SET !DATASOURCE_LINE 3

Creating the Loop

In our previous post we had a form that we had to fill with a CSV file that looks as the example above, our code until then was:

VERSION BUILD=8510617 RECORDER=FX
TAB T=1
SET !DATASOURCE ejemplo.csv
URL GOTO=https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/viewform
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.757433527 CONTENT={{!COL1}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.414022470 CONTENT={{!COL2}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.1960753282 CONTENT={{!COL3}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.549681952 CONTENT={{!COL4}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.436972228 CONTENT={{!COL5}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.1842450612 CONTENT={{!COL6}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.926019437 CONTENT={{!COL7}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.1850440789 CONTENT={{!COL8}}
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:submit

If we run that, with the Max: field equal to 3 clicking on Play (loop) this will make the script to take the values of the first row and it will use it the 3 times to fill our form. If our fist row, as in our CSV file example above, is used for titles it will fill the form with those incorrect values. To avoid that, first we are going to add the following line.

VERSION BUILD=8510617 RECORDER=FX
TAB T=1
SET !DATASOURCE ejemplo.csv
SET !LOOP 2
URL GOTO=https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/viewform

This line tells our script "start with loop 2" this alone doesn't help too much. If we run our script like that, it will pretty much do the same as before, but instead it will run it just twice. The first time !LOOP will be equal to 2 and the second time will be equal to 3 and it will end. To make it loop thru our document we are going to add the following line.

VERSION BUILD=8510617 RECORDER=FX
TAB T=1
SET !DATASOURCE ejemplo.csv
SET !LOOP 2
SET !DATASOURCE_LINE {{!LOOP}}
URL GOTO=https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/viewform

As I explained before, we use the !DATASOURCE_LINE variable to tell our script which line of our document we want to use. Therefore, in this case we are telling it to take the value of the !LOOP variable. Then, because the line above is assigning the value 2 to the variable !LOOP the !DATASOURCE_LINE variable takes the same value.

When we run that with a Max: 3 we will have the following:

First loop:

LOOP = 2 therefore

DATASOURCE_LINE = 2

In this case, it will use the values of the row 2 of our document

Second Loop:

LOOP = 3 therefore 

DATASOURCE_LINE = 3

Here, it will use the values of the row 3. That's how our script will be completed.

VERSION BUILD=8510617 RECORDER=FX
TAB T=1
SET !DATASOURCE ejemplo.csv
SET !LOOP 2
SET !DATASOURCE_LINE {{!LOOP}}
URL GOTO=https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/viewform
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.757433527 CONTENT={{!COL1}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.414022470 CONTENT={{!COL2}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.1960753282 CONTENT={{!COL3}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.549681952 CONTENT={{!COL4}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.436972228 CONTENT={{!COL5}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.1842450612 CONTENT={{!COL6}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.926019437 CONTENT={{!COL7}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:entry.1850440789 CONTENT={{!COL8}}
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:https://docs.google.com/forms/d/1TeWc2RoeHyCX2DOAXBTzl_uYIS2QrBnfGosuN41d1Mw/formResponse ATTR=NAME:submit

We only need to set the value of the field Max: to the total number of rows in our document and run it with the Play (loop)  button.