coding using jscript test complete
TRANSCRIPT
![Page 1: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/1.jpg)
Test Complete CodingQuick guide to using jScript with Test Complete by SmartBear
![Page 2: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/2.jpg)
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();
![Page 3: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/3.jpg)
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();
![Page 4: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/4.jpg)
Read a file
var file=fso.OpenTextFile
(fileLocationAndNameGolden,
FileReadOnly);
while (!file.AtEndOfStream){
var line=file.ReadLine();
}
![Page 5: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/5.jpg)
Try/catch/finally
try{
}
catch(e){
Log.Message(e.description
);
}
Finally{
{
![Page 6: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/6.jpg)
Copy a file
aqFile.Copy
(FromFullPathINcludingFileName,ToFullPathIncludingFileNa
me, true)
![Page 7: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/7.jpg)
Iterate through an array
var myArray = [];
for(var k=0; k < myArray.length; k++){
outFile.WriteLine(myArray[k]);
}
![Page 8: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/8.jpg)
Switch statement
switch(){
case "":
{
//do something
}
case2 ""
{
//do something
}
default:
{
//do something
}
}
![Page 9: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/9.jpg)
Built in Parameters
// Built in Parameters
function ProcessCommandLine(config) {
for (i = 1; i<= BuiltIn.ParamCount();i++){
ProcessCommandLineArgument(BuiltIn.ParamStr(i),config);
}
}
![Page 10: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/10.jpg)
Test Complete aqString
aqString.Replace
aqString.Trim
aqString.Find
aqString.ToLower
aqString.ChangeListItem
aqString.GetListItem
aqString.FindLast
aqString.GetLength
aqString.SubSting(
aqString.GetListLength
![Page 11: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/11.jpg)
Do loop
do{
//some commands
}while( something is true)
![Page 12: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/12.jpg)
Delete /copy file
aqFileSystem.DeleteFile(outPath + newFileName + ".xml");
aqFileSystem.CopyFile(from, to)
aqFile.Copy(FromFullPathINcludingFileName,ToFullPathIncl
udingFileName, true)
![Page 13: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/13.jpg)
Throw error
//Check that the object isn't null
if (obj == null) throw "Null object passed to
generateXMLFromObjectFields";
![Page 14: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/14.jpg)
Push an item to an array
myArray = [];
myArray.push(item);
![Page 15: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/15.jpg)
Project variables
// access test complete project variables
var MyAppPath=Project.Variables.MyAppPath;
![Page 16: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/16.jpg)
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");
};
![Page 17: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/17.jpg)
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();
}
![Page 18: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/18.jpg)
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;
}
![Page 19: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/19.jpg)
Read xml
objXMLDoc =
Sys.OleObject("Msxml2.DOMDocument.6.0")
objXMLDoc.async = false ;
objXMLDoc.setProperty("SelectionLanguage","XPath");
result=objXMLDoc.load(xmlConfigFile);
xmlNode=xmlTestConfig.selectSingleNode("TestClass");
![Page 20: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/20.jpg)
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");
![Page 21: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/21.jpg)
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;
![Page 22: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/22.jpg)
Generate XML from object
var docSection = Storages.XML("");
var colFields = aqObject.GetFields(obj, false);
var sec = docSection.GetSubSection(root_name);
![Page 23: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/23.jpg)
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);
![Page 24: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/24.jpg)
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");
![Page 25: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/25.jpg)
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){ …
![Page 26: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/26.jpg)
Shorter if/then
Variable = (condition) ? True-value : false_value
![Page 27: Coding using jscript test complete](https://reader034.vdocuments.site/reader034/viewer/2022052413/55a5d3a41a28abd3148b45d8/html5/thumbnails/27.jpg)
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);