There are many cases in automated testing where you need to
write multiple tests which are basically the same, except for some minor
changes.
For example, you need to write a test which inserts a value
to a filed, and the value should change every time. If you write the whole
thing in one test, a simple loop will solve the issue. But if you want a
separate test for each value, you’ll need to create many test files that will
include the test code and will differ only in the desired value.
#!/usr/bin/python
import json
import sys
import getopt
import time
import os.path
#######################################
## Declaring default values
#######################################
## This is the default template to generate test files.
testTemplate = """
StartTest(function(t)
{{
t.chain(
//here
we click on the button which will open a window
function
(next) {{
var
fieldToClick = Ext.ComponentQuery.query('element')[0];
t.click(fieldToClick,next);
}},
//wait
for the window to open
{{
waitFor : 'componentQuery', args :
"windowToOpen" }},
//insert
to the text field in the window a value
function
(next) {{
var
fieldToType = Ext.ComponentQuery.query('textfield')[0];
t.type
(fieldToType, '{0}', next);
}}
);
}})
"""
## Default JSON input file name
inputJSONfile = '/home/user/values.json'
## The default template for test's file names
testFileNameTemplate =
"Insert_Value_{0}_To_Textfield.t.js"
#######################################
## This is the script's actual code (The MAIN function)
#######################################
## Printing the final configuration
print
("#######################################\nConfiguration\n#######################################")
print 'Input JSON file is:', inputJSONfile
print 'Template for test''s file names:' ,
testFileNameTemplate
print 'Test template used for generating test files:',
testTemplate
print
("#######################################\n")
## Open the attributes json file
with open(inputJSONfile) as data_file:
##
Parsing the JSON data
data =
json.load(data_file)
# Loop on all the
keys in the json
for k in
data.keys():
##
Extract the attribute name for each record in the json
valueToInsert
= data[k]
## Generating the file name
fileName =
testFileNameTemplate.format(valueToInsert)
## Creating the test file (This will
OVERWRITE EXISTING FILES)
## Injecting the values into the
test template
with open(fileName,
"w") as fo:
fo.write(testTemplate.format(valueToInsert))
print (fileName + " created.")
So what exactly happens in this script?
Each test clicks on an element, and insert a value into the textbox which opens.
The python scripts creates all the test files in the same
directory from which it runs, and you can supply the JSON file path in the
script itself.
The testTemplate is the code which you will be
included in each test file.
The inputJSONfile is the path of the JSON file
The testFileNameTemplate is the file name for
each test (and can include the value from the JSON, as you can see on this
example).
You can manipulate the script and add more code and more
parameters to it; the purpose of this post is to give you the main idea for
creating such an infrastructure.