How to Write VBScript using XML?

Create Vbscript in XML
Write VBScript using XML
Write VBScript using XML

How to Write VBScript using XML?

WSH supports XML ( a case sensitive extended markup language to create a script and pass test data. XML tags can be used in Windows Script file are as follows:

TagsMeaning
<?xml?>It specifies windows script file’s XML level or version.
<?job?>It specifies to enable or disable error handling and debugging for a job.
<comment></comment>It enables us to embed comments within the script.
<Script></script>It defines a new script is being created.
<job></job>It defines a job .
<package></package>It configures multiple jobs to be defined in a single script file.
<resource></resource>It defines a static data that can be referenced by the script.

 

Ads code goes here

Let’s see the elements one by one

<?xml?>

<?xml?> is an optional tag that specifies XML version. This needs to be the first statement in the WSF if used. It does not need a closing tag.

Syntax

<?xml version="x.y" standalone="dfdflag"?>
  • Standalone refers to the boolean value which indicates if an external DTD (Document Type Definition) should be included or not.

Example

<?xml version="1.01" standalone="yes"?>

<?job?>

<?job?> is also an optional tag. It enables or disables error reporting and debugging. It does not need a closing tag.

Syntax

<?job error="errorflag" debug="defugflag"?>

errorflag and debugflag are boolean values.

If set true- error reporting will be enabled, false will disable error reporting. debugflag controls the presence of windows script debugger on an error. If set true, it will open Windows Script Debugger for the error. If set false it will not open Windows Script Debugger.

READ  Regular Expressions in VBScript or QTP or UFT

Example

<?job error="true" debug="true"?>

<comment></comment>

<comment></comment> tag enables us to put a comment in the script file. We can also use multi line comment with this tag.

Syntax

<comment>Our comment</comment>

Example

<comment>Our First comment</comment>

<script></script>

<script></script> enables us to write scripts. The scripts may be written in multiple languages(WSH supported languages).

Syntax

<script language="ScriptLanguage",[Src ="pathOfScript"]>
....
....
....
</script>

Where

  • ScriptLanguage is WSH approved language.
  • Src is an optional parameter. It just tells WSH where the script is located.

Example

<script language="VBScript">
msgbox "hello"
</script>
or
<script language="VBScript" Src="D:\scriptName.vbs">
</script>

<job></job>

WSF(Window Script File) may contain more than one job. And each job may contain more than one script. The jobs are defined inside <job></job> tag

Syntax

<job [id=myjobID]>
....
....
</job>

Where myjobID is the unique identifier to identify jobs in a WSF. If this parameter is omitted, it signifies that there is only one job in the script. JobID also helps us to specify which job we want to run.

Example

<job>
<script language="VBScript">
msgbox "hello"
</script>
<script language="jsScript">
Wscript.Echo("hello")
</script>
<script language="VBScript" Src="D:\hello.vbs">
</script>
</job>

<package></package>

<package></package> is useful when we want to place more than one jobs in the WSF.

Syntax

<package>
.....
.....
</package>

Example

<package>
<job id="myjob-1">
<script language="VBScript">
msgbox "hello"
</script>
</job>
<job id="myjob-2">
<script language="jsScript">
Wscript.Echo("hello")
</script>
</job>
<job id="myjob-3">
<script language="VBScript" Src="D:\hello.vbs">
</script>
</job>
</package>

<resource></resource>

<resource></resource> tag allows us to define constants that our script can access and use during execution time. This tag supports one or more constants. These constants can be defined once and can be used over and over.

<resource></resource> tag needs to be placed within <job></job> tag.

Syntax

<resource id="resourceID">
....
....
</resource>

resourceID is used to name the constant. The value of the constant is assigned in the tag itself.

READ  Frequently Asked Coding Questions During Interview on UFT

Example

<resource id="oneDollar">70</resource>
<script language="VBscript"
msgbox "5$="& 5*getResource("oneDollar")
</script></job>

How to execute a WSF file created in XML?

To execute all jobs we need to fire the command

Wscript myScript.wsf // for all jobs

To execute the selected job we need to fire the below command

Wscript myScript.wsf //job:myJob-2

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *