August 24, 2011

QA Wizard Pro How To: Automating Onto Virtual Machines

Helix ALM
Virtual machines offer a great way to run automated scripts in a separate, customized environment of your choosing. The virtual environment can exist without interfering with your normal computer. You can configure the virtual machine to ensure the environment remains constant on each script run. One way to make better use of actual machine resources is to have QA Wizard Pro start up and shut down the virtual machine on its own. This way the machine is only running when it is being used for automation. To do so, I started by creating the following Windows .bat file:
@echo off
set QAW_VirtualMachinePath="C:UserspetervDocumentsVirtual MachinesWindows XP VMWareWindows XP VMWare.vmx"
 set QAW_RunScriptPath="C:Program Files (x86)SeapineQA Wizard ProQAWRunScript.exe"
 set QAW_WorkspacePath="C:UserspetervDocumentsQA Wizard Pro WorkspacesVMTestVMTest.qawwspace"
 set QAW_TestScriptPath="C:UserspetervDocumentsQA Wizard Pro WorkspacesVMTestSample Windows Script.qawscript"
 set QAW_ShutdownScriptPath="C:UserspetervDocumentsQA Wizard Pro WorkspacesVMTestShutdown Computer.qawscript"
 set QAW_ReportDir="C:UserspetervDocumentsQA Wizard Pro WorkspacesVMTestVM Results"
 set QAW_RemoteMachineName=
 set QAW_CleanSnapshot="Clean State Snapshot"
 set QAW_SnapshotName="Script Error %DATE% %TIME%"
REM **********************************************
echo Running VM.
 start "VM" "C:Program Files (x86)VMwareVMware Workstationvmware.exe" -x -q %QAW_VirtualMachinePath%
echo Waiting 2 minutes for the VM to start up.
 ping -n 120 >NUL 2>&1
echo Starting QAW Test Script
 set QAW_TestScriptSuccess=yes
 %QAW_RunScriptPath% %QAW_WorkspacePath% %QAW_TestScriptPath% /reportdir %QAW_ReportDir% /remote %QAW_RemoteMachineName%
 IF ERRORLEVEL 1 set QAW_TestScriptSuccess=no
echo Shutting down remote machine.
 %QAW_RunScriptPath% %QAW_WorkspacePath% %QAW_ShutdownScriptPath% /noreport /remote %QAW_RemoteMachineName%
echo Waiting 2 minutes for the VM to shutdown.
 ping -n 120 >NUL 2>&1
IF %QAW_TestScriptSuccess%=yes GOTO SkipTakingSnapshot
 "C:Program Files (x86)VMwareVMware Workstationvmrun.exe" snapshot %QAW_VirtualMachinePath% %QAW_SnapshotName%
echo Reverting to base snapshot.
 "C:Program Files (x86)VMwareVMware Workstationvmrun.exe" revertToSnapshot %QAW_VirtualMachinePath% %QAW_CleanSnapshot%
This example batch file uses VMware, but you can configure it to use a virtual environment of your choosing. You can also select the script you want to run in the virtual environment. The sample script and shutdown script that I use in the following example can be found in this VMTest QAWizard Pro Workspace. You need to set up your virtual environment to ensure QA Wizard Pro will be able to run scripts on it. To do so you will need to:
  • Set up the virtual machine to automatically log in to Windows. I used Windows XP in this example.
  • Install QA Wizard Pro and the appropriate licenses.
  • Have QAWRemote start when the virtual machine starts up. You can find QAWRemote.exe in the "Program FilesSeapineQA Wizard Pro" folder. To have it start automatically, you can create a shortcut to it in your "Startup" folder in the Windows Start Menu.
After I completed the set up tasks, I made a VMWare snapshot of the machine with the name "Clean State Snapshot". This allows me to reset the virtual machine to a known starting state each time I run a test. Running the above batch file causes the virtual machine to start up: Once started, the script is sent remotely to the virtual machine. It runs to completion and then sends back a report file with the results of running the test. After the script completion, I run another script to force close any open programs and shut down the remote machine. In the case of a previous error, the host machine then takes a snapshot of the virtual machine so I can view the state of the machine that the error occurred in. It then sets the virtual machine to the "Clean State Snapshot" that I saved when setting up the virtual machine, which ensures the machine is in a known good state the next time this script runs. The resulting report from the script run is saved to the host machine. It can be copied to a different destination or emailed to others that are interested in the results. You can modify the above batch file to work with different virtual machine environments. Also, since the scripts are running in an environment separate from their host machine, you can use Windows Task Scheduler to run the batch file overnight, even while the host computer is locked.