The testTemplate is meant to be copied for each new test. Each test should be placed directly in the Sahi "scripts" folder.
The Files in testTemplate
Here is a description of the files in the testTemplate:
- launch.sah
- This is used to launch the test. Place your function calls in a special section.
- constants.sah
- For the defining of constants.
- testFunctions.sah
- Place your test function code here.
- fileList.sah
- This lists the files to be included in the code of the test.
- library.sah
- Various test functions you may want to use in your test. See the list of functions.
- data.xlsx
- Optional data source, such as for url paths, etc.
- writeData.xlsx
- Optional, for the ability to write data.
Using the testObj
The testObj object is useful for creating tests when you want to create urls from lists that contain parts of a url. For example, you may want to visit several url paths (like the shop/buy-iphone/special-edition-iphone-7 below) and for several locales (like the "uk" and "mx" below):
https://www.apple.com/mx/shop/buy-iphone/special-edition-iphone-7
https://www.apple.com/uk/shop/buy-iphone/special-edition-iphone-7
Suppose you have many paths and many locales.
Use the testObj to create those urls dynamically like this:
https://www.apple.com/mx/shop/buy-iphone/special-edition-iphone-7
https://www.apple.com/uk/shop/buy-iphone/special-edition-iphone-7
Suppose you have many paths and many locales.
Use the testObj to create those urls dynamically like this:
var $testName = new testObj($DOMAIN, $locales, $urlPaths);
Examine the code in the testSample for a working case.
The $DOMAIN could be set on the launch.sah file, as it typically is convenient to set there. You can make several launch files, one for each domain, if that helps your ability to to launch tests easily (one or a test environment, etc.).
The $locales and $urlPaths could be arrays containing the values you want.
For example, here is the code in testSample:
var $locales = ["/uk","/mx",""]; var $pages = [ "", "shop/buy-iphone/iphone-7" ]; var $checkContact = new testObj($DOMAIN,$locales,$pages);
var $DOMAIN = "http://apple.com" is set on the launch.sah file for testSample. You may want to review the code in the testSample folder as an example.
Properties of testObj
Some of the properties available in the test object are:
.domains
.locales
.testFunction
.testParameters
The first 2, .domains and .locales are examples of how to update the properties of the domains and locales. For example:
.domains
.locales
.testFunction
.testParameters
The first 2, .domains and .locales are examples of how to update the properties of the domains and locales. For example:
var $checkContact = new testObj(); $checkContact.domains = "http://apple.com"; $checkContact.locales = ["/uk","/mx"];
In fact, it turns out that each property can either be a string or an array. It will iterate over what you give to it.
$checkContact.domains = ["http://apple.com","http://test.env.apple.com"]; $checkContact.locales = "/mx";
The .testFunction properties is REQUIRED. This is the function that will be performed for every url visited. When storing a function as a value, do not use the () parentheses.
The very first parameter passed to this function is special value called $urlInfo. Its properties are the components of the URL:
$urlInfo.url (full url)
$urlInfo.domain
$urlInfo.locale
$urlInfo.urlPath
$urlInfo.urlParameter
So writing a test Function may look like this:
function testAllTheThings($urlInfo, $param1, $param2, param3){
test1($param1);
test2($param2);
specialFunction();
_assertEqual(etc...
}
Setting the test Function would look like this:
$checkContact.testFunction = testAllTheThings; //Note: no parameters, no parenthesis
To give the additional test parameters, use an array in .testParameters:
$checkContact.testParameters = [$param1,$param2,$param3];
A code example could be
var $checkAllTheThings = new testObj(); $checkAllTheThings.domains = "http://apple.com"; $checkAllTheThings.locales = ["/uk","/mx",""] //no url path is necessary, just check the front page function validateTheMarquee($urlInfo,$length,$height){ //test code here } $checkAllTheThings.testFunction = validateTheMarquee; $checkAllTheThings.testParameters = [$length,$height];
All testObj Properties
Here is a list of all available testObj properties:
.domains
.locales
.urlPathsOrFunction
.urlParametersOrFunction
.rotateUrlParameters
.clearCookieSetting
.testFunction
.testParameters
.restartValueDomain
.restartValueLocale
.restartValueUrlPath
.restartValueParameter
Note: the .urlPathsOrFunction property can be either an array or a function where the first parameter passed to it is $locale. The function would look like this:
.domains
.locales
.urlPathsOrFunction
.urlParametersOrFunction
.rotateUrlParameters
.clearCookieSetting
.testFunction
.testParameters
.restartValueDomain
.restartValueLocale
.restartValueUrlPath
.restartValueParameter
Note: the .urlPathsOrFunction property can be either an array or a function where the first parameter passed to it is $locale. The function would look like this:
[object Object]
That way, you can update your testObj property with either option:
$checkContact.urlPathsOrFunction = urlPathBasedOnLocale;
or
$checkContact.urlPathsOrFunction = ["/path1.html","/path2.html","/etc.html"];
$checkContact.urlPathsOrFunction = urlPathBasedOnLocale;
or
$checkContact.urlPathsOrFunction = ["/path1.html","/path2.html","/etc.html"];