Post on 15-Jul-2015
Test Complete CodingQuick guide to using jScript with Test Complete by SmartBear
Create an object/class
function myLogFileObject()
{
this.myPublicObjectArray = [];
var myPrivateObjectArray = [];
var myPrivateCounter = 0;
this.readFunction=function();
return true;
}
}
Why?
Nice way to organize
functions and variables
that represent that object.
How to use?
var myObject
myLogFileObject();
myObject.readFunction();
Write to a text file
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var outFile = objFSO.CreateTextFile(FileNameIncludingPath,
true,false);
outFile.WriteLine("Write me to the file");
outFile.Close();
Read a file
var file=fso.OpenTextFile
(fileLocationAndNameGolden,
FileReadOnly);
while (!file.AtEndOfStream){
var line=file.ReadLine();
}
Try/catch/finally
try{
}
catch(e){
Log.Message(e.description
);
}
Finally{
{
Copy a file
aqFile.Copy
(FromFullPathINcludingFileName,ToFullPathIncludingFileNa
me, true)
Iterate through an array
var myArray = [];
for(var k=0; k < myArray.length; k++){
outFile.WriteLine(myArray[k]);
}
Switch statement
switch(){
case "":
{
//do something
}
case2 ""
{
//do something
}
default:
{
//do something
}
}
Built in Parameters
// Built in Parameters
function ProcessCommandLine(config) {
for (i = 1; i<= BuiltIn.ParamCount();i++){
ProcessCommandLineArgument(BuiltIn.ParamStr(i),config);
}
}
Test Complete aqString
aqString.Replace
aqString.Trim
aqString.Find
aqString.ToLower
aqString.ChangeListItem
aqString.GetListItem
aqString.FindLast
aqString.GetLength
aqString.SubSting(
aqString.GetListLength
Do loop
do{
//some commands
}while( something is true)
Delete /copy file
aqFileSystem.DeleteFile(outPath + newFileName + ".xml");
aqFileSystem.CopyFile(from, to)
aqFile.Copy(FromFullPathINcludingFileName,ToFullPathIncl
udingFileName, true)
Throw error
//Check that the object isn't null
if (obj == null) throw "Null object passed to
generateXMLFromObjectFields";
Push an item to an array
myArray = [];
myArray.push(item);
Project variables
// access test complete project variables
var MyAppPath=Project.Variables.MyAppPath;
prototype
//prototype
var Dog=function(name) {
this.name = name;
var barkCount = 0;
this.bark = function() {
barkCount++;
Log.Message(this.name + " bark");
};
this.getBarkCount = function() {
Log.Message(this.name + " has barked " + barkCount + " times");
};
prototype
this.wagTail2= function() {
Log.Message(this.name + " wagging tail2");
}
};
Dog.prototype.wagTail = function() {
Log.Message(this.name + " wagging tail");
};
function dog_test(){
var dog = new Dog("Dave");
dog.bark();
dog.bark();
dog.getBarkCount();
dog.wagTail2();
dog.wagTail();
}
Extend Array - unique
//Extend Array to return unique numbers only
Array.prototype.unique = function()
{
var tmp = {}, out = [];
for(var i = 0, n = this.length; i < n; ++i)
{
if(!tmp[this[i]]) { tmp[this[i]] = true; out.push(this[i]); }
}
return out;
}
Read xml
objXMLDoc =
Sys.OleObject("Msxml2.DOMDocument.6.0")
objXMLDoc.async = false ;
objXMLDoc.setProperty("SelectionLanguage","XPath");
result=objXMLDoc.load(xmlConfigFile);
xmlNode=xmlTestConfig.selectSingleNode("TestClass");
Read DOM 6.0 xml
var objXMLDoc =
Sys.OleObject("Msxml2.DOMDocument.6.0");
objXMLDoc.async = false;
objXMLDoc.setProperty("SelectionLanguage","XPath");
var ns= "xmlns:a='http://smpte-
ra.org/schemas/2021/2008/BXF'";
objXMLDoc.setProperty("SelectionNamespaces", ns);
ar AsRunList=objXMLDoc.SelectNodes("//a:AsRun");
File functions
var fso = Sys.OleObject("Scripting.FileSystemObject");
var file = fso.GetFile(file_path);
var fol = fso.GetFolder(folder_path);
fol.size
var filesCount = fol.files.Count;
Generate XML from object
var docSection = Storages.XML("");
var colFields = aqObject.GetFields(obj, false);
var sec = docSection.GetSubSection(root_name);
Connect to UDP
socket = dotNET.System_Net_Sockets.Socket.zctor(
dotNET.System_Net_Sockets.AddressFamily.InterNetwork
,
dotNET.System_Net_Sockets.SocketType.Dgram,
dotNET.System_Net_Sockets.ProtocolType.Udp);
SQL Server DB
Conn = new ActiveXObject("ADODB.Connection");
var constr= "Provider=SQLOLEDB.1;Data
Source=SERVERN;Initial Catalog=cat1;User
ID=sa;Password=myPassword";
Conn.Open(constr);
var catalogue = new ActiveXObject('ADOX.Catalog');
rs = new ActiveXObject("ADODB.Recordset");
Literal Object
Create a simple constructor that contains the fields for that object
Function User(theParam1){
This.param1 = theParam1;
}
Extend that object using the Literal prototype feature
User.prototype = {
Constructor: User,
Functoin1: function(theParam2){ …
Shorter if/then
Variable = (condition) ? True-value : false_value
Object fields and methods
Var FieldsCol = aqObject.GetFields(Obj);
Iterate through the fields
while ( FieldsCol.HasNext() ){
Log.Message( FieldsCol.Next().Name );
}
Use this to get the methods:
aqObject.GetMethods(Obj);