27
Test Complete Coding Quick guide to using jScript with Test Complete by SmartBear

Coding using jscript test complete

Embed Size (px)

Citation preview

Page 1: Coding using jscript test complete

Test Complete CodingQuick guide to using jScript with Test Complete by SmartBear

Page 2: Coding using jscript test complete

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

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

Read a file

var file=fso.OpenTextFile

(fileLocationAndNameGolden,

FileReadOnly);

while (!file.AtEndOfStream){

var line=file.ReadLine();

}

Page 5: Coding using jscript test complete

Try/catch/finally

try{

}

catch(e){

Log.Message(e.description

);

}

Finally{

{

Page 6: Coding using jscript test complete

Copy a file

aqFile.Copy

(FromFullPathINcludingFileName,ToFullPathIncludingFileNa

me, true)

Page 7: Coding using jscript test complete

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

Switch statement

switch(){

case "":

{

//do something

}

case2 ""

{

//do something

}

default:

{

//do something

}

}

Page 9: Coding using jscript test complete

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

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

Do loop

do{

//some commands

}while( something is true)

Page 12: Coding using jscript test complete

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

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

Push an item to an array

myArray = [];

myArray.push(item);

Page 15: Coding using jscript test complete

Project variables

// access test complete project variables

var MyAppPath=Project.Variables.MyAppPath;

Page 16: Coding using jscript test complete

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

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

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

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

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

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

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

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

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

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

Shorter if/then

Variable = (condition) ? True-value : false_value

Page 27: Coding using jscript test complete

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);