For any corporate training requirements Please mail To sudhakar@qtpsudhakar.com
Course Name Date Time Duration Type Location
Qtp / UFT withVbscript 26th March 2020 9.00 PM IST to 10:30 PM IST 30 Days WeekDays Online

A book "Cracking the QTP interview" authored by Sudhakar.Read More. Buy From FlipKart

Wednesday, April 28, 2010

Selecting an Environment to Execute Scripts

When we run a script, we would like to have a msg - ' Which Environment you would like to run the script? Also need a drop-down list of Dev, IT and UAT. So that user can select whatever test environment they want to run the script. once user selects it, the script should open particular test environment in browser. Help me to write the script.
(One of my group member posted this question in my group)
Script to Select Environment by Creating .Net Windows Form using DotnetFactory Utility Object

Set SelectEnvForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms")
Set EnvComboBox= DotNetFactory.CreateInstance("System.Windows.Forms.ComboBox", "System.Windows.Forms")
Set btnSubmit = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
Set objPositions = DotNetFactory.CreateInstance("System.Drawing.Point","System.Drawing",x,y)
With objPositions
.X = 80
.Y = 80
End With
With EnvComboBox
.Location = objPositions
.DropDownWidth = 100
.Items.Add "DEV"
.Items.Add "IT"
.Items.Add "UAT"
End With
With objPositions
.X = 100
.Y = 125
End With
With btnSubmit
.Location = objPositions
.Text = "Submit"
End With
With SelectEnvForm
.Controls.Add EnvComboBox
.Controls.Add btnSubmit
.CancelButton = btnSubmit
.Text = "Select Environment Form"
End With
Msgbox EnvComboBox.SelectedItem

http://msdn.microsoft.com/en-us/library/k50ex0x9(v=VS.100).aspx Click this link to know how to create objects in a .net windows form using QTP DotnetFactory utility object.

Script to Select Environment by Creating HTML Form

Save the below script into a library file and associate to QTP Test. When we run QTP test it automatically displays a dialog to select environment. Select the environment and click on submit button.
In script you can use a variable ExecutionEnvironment to know which environment is selected.

Dim htmlCode
Dim ResPath
Dim fso
Dim FlPath
Dim fl
Dim wshShell
Dim wshSystemEnv
Dim ExecutionEnvironment
htmlCode="<html><hta:application id=""oHTA"" CAPTION=""yes"" windowstate=""normal"" sysmenu=""yes"" />"
htmlCode=htmlCode&vbnewline&"<title>Select Environment</title> <h3>Select Environment</h3><body>"
htmlCode=htmlCode&vbnewline&"<SCRIPT LANGUAGE=""VBScript"">"
htmlCode=htmlCode&vbnewline&"Sub SpecifyEnvironment()"
htmlCode=htmlCode&vbnewline&"Set lst=document.getElementById(""EnvList"")"
htmlCode=htmlCode&vbnewline&"Set wshShell = CreateObject( ""WScript.Shell"" )"
htmlCode=htmlCode&vbnewline&"Set wshSystemEnv = wshShell.Environment( ""SYSTEM"" )"
htmlCode=htmlCode&vbnewline&"wshSystemEnv( ""ExecutionEnv"" ) = lst.value"
htmlCode=htmlCode&vbnewline&"End Sub"
htmlCode=htmlCode&vbnewline&"Sub Window_onLoad"
htmlCode=htmlCode&vbnewline&"window.resizeTo 300,200"
htmlCode=htmlCode&vbnewline&"window.moveto centerX,centerY"
htmlCode=htmlCode&vbnewline&"End Sub"
htmlCode=htmlCode&vbnewline&"<select id='EnvList'><option value=""DEV"">DEV</option> <option value=""IT"">IT</option>  <option value=""UAT"">UAT</option></select><button type=""button"" onclick='SpecifyEnvironment()'>Submit</button></body></html>"
Set fso=CreateObject("scripting.filesystemobject")
Set fl=fso.CreateTextFile(FlPath,True)
SystemUtil.Run FlPath
if Window("text:=Select Environment").Exist(10) then
While Window("text:=Select Environment").Exist(0)=true
End If
fso.DeleteFile FlPath
Set wshShell = CreateObject("WScript.Shell")
Set wshSystemEnv = wshShell.Environment("SYSTEM")
ExecutionEnvironment = wshSystemEnv("ExecutionEnv")
Using Environment Name in Script
Msgbox ExecutionEnvironment

Please send your Suggestions and Doubts to my yahoo group http://in.groups.yahoo.com/group/qtpsudhakar

1 comment :

  1. HI,

    Its really very good article. For coming of this in automation framework, .Net Needs to be installed?