Applies to TestComplete 14.60, last modified on September 18, 2020

Description

The WMI object provides access to the Windows Management Instrumentation (WMI) functionality. This object is a wrapper over the operating system’s intrinsic SWbemServices object, but with simpler access to the WMI services, so that their functionality is easier to use for inexperienced testers. For detailed information about the SWbemServices object and the WMI scripting API, see “Scripting API for WMI” in the MSDN Library (the on-line version is available at http://msdn.microsoft.com).

The WMI object has the following requirements:
  • WMI must be installed on the computer to which WMI queries will be addressed. It is a standard feature preinstalled on most Windows operating systems.

  • The Windows Management Instrumentation service be running on the target computer.

Additionally, the WMI.PostInstalledAppsInfo method requires that the WMI Windows Installer Provider component of the operating system be installed on the target computer. See the method description for details.

To learn how you can make use of WMI in your tests, see Working With WMI Objects in Scripts.

Note that the WMI object was created with the script extensions technology. It is only available if the WMI Object script extension is installed in TestComplete.

The WMI object demonstrates how you can use script extensions to add new objects to the TestComplete object model. You can find the object’s source code in the AQAScriptExtensions.tcx file that is located in the <TestComplete>\Bin\Extensions\ScriptExtensions folder. This file is a zip archive that has the .tcx extension. You can unpack the files from it with any zip archiver. The WMI object’s code is included into the wmiCode.vbs unit. The description.xml file contains information about the extension and the object’s methods and properties.

There is a tutorial that explains the creation of the object’s code:

Creating Runtime Objects Tutorial

Members

Example

Below is a sample script that demonstrates how you can use the WMI object to create a scheduled task that runs a specified TestComplete project at a defined time twice a week. To use the routine to create a scheduled task on a remote computer, specify the desired computer name using the WMI.ComputerName property.

JavaScript, JScript

function CreateScheduledTask()
{

  var TCPath, ProjectPath, ProjectName, CommandLine, DaysOfWeek, StartTime;
  var NewJob, JobID, JobCreateError;

  // Create a new job object through WMI
  WMI.ComputerName = ".";
  NewJob = WMI.Service.Get("Win32_ScheduledJob");

  TCPath = "C:\\TestCompleteFolder\\TestComplete.exe";
  ProjectPath = "C:\\Work\\SampleProjectSuite.pjs";
  ProjectName = "Sample_Project";

  // Specify parameters for a new scheduled job
  CommandLine = TCPath + " " + ProjectPath + " /r" + " /p:" + ProjectName + " /exit";
  DaysOfWeek = 2 + 8;
  StartTime = "********180000.000000-480";

  // Create a new scheduled job
  JobCreateError = NewJob.Create(CommandLine, StartTime, true, DaysOfWeek, null, true, JobID);
  if (JobCreateError != 0)
    Log.Error("An error (error code " + aqConvert.IntToStr(JobCreateError) + ") occurred during the job creation")
  else
    Log.Message("The new job has been created successfully");

}

Python

def CreateScheduledTask():

  # Create a new job object through WMI
  WMI.ComputerName = "."
  NewJob = WMI.Service.Get("Win32_ScheduledJob")

  TCPath = "C:\\TestCompleteFolder\\TestComplete.exe"
  ProjectPath = "C:\\Work\\SampleProjectSuite.pjs"
  ProjectName = "Sample_Project"

  # Specify parameters for a new scheduled job
  CommandLine = TCPath + " " + ProjectPath + " /r" + " /p:" + ProjectName + " /exit"
  DaysOfWeek = 2 + 8
  StartTime = "********180000.000000-480"

  # Create a new scheduled job
  JobCreateError = NewJob.Create(CommandLine, StartTime, True, DaysOfWeek, None, True)
  if (JobCreateError != 0):
    Log.Error("An error (error code " + aqConvert.IntToStr(JobCreateError) + ") occurred during the job creation")
  else:
    Log.Message("The new job has been created successfully")

VBScript

Sub CreateScheduledTask

  Dim TCPath, ProjectPath, ProjectName, CommandLine, DaysOfWeek, StartTime
  Dim NewJob, JobID, JobCreateError

  
  ' Create a new job object through WMI
  Set NewJob = WMI.Service.Get("Win32_ScheduledJob")

  TCPath = "C:\TestCompleteFolder\TestComplete.exe"
  ProjectPath = "C:\Work\SampleProjectSuite.pjs"
  ProjectName = "Sample_Project"

  ' Specify parameters for a new scheduled job
  CommandLine = TCPath & " " & ProjectPath & " /r" & " /p:" & ProjectName & " /exit"
  DaysOfWeek = 2 or 8
  StartTime = "********180000.000000-480"

  ' Create a new scheduled job
  JobCreateError = NewJob.Create(CommandLine, StartTime, True , DaysOfWeek, ,True, JobID)
  If JobCreateError <> 0 Then
    Log.Error "An error (error code " & aqConvert.IntToStr(JobCreateError) & ") occurred during the job creation"
  Else
    Log.Message "The new job has been created successfully"
  End If

End Sub

DelphiScript

procedure CreateScheduledTask();
var TCPath, ProjectPath, ProjectName, CommandLine, DaysOfWeek, StartTime;
var NewJob, JobID, JobCreateError;
begin

  // Create a new job object through WMI
  WMI.ComputerName := '.';
  NewJob := WMI.Service.Get('Win32_ScheduledJob');

  TCPath := 'C:\TestCompleteFolder\TestComplete.exe';
  ProjectPath := 'C:\Work\SampleProjectSuite.pjs';
  ProjectName := 'Sample_Project';

  // Specify parameters for a new scheduled job
  CommandLine := TCPath + ' ' + ProjectPath + ' /r' + ' /p:' + ProjectName + ' /exit';
  DaysOfWeek := 2 or 8;
  StartTime := '********180000.000000-480';

  // Create a new scheduled job
  JobCreateError := NewJob.Create(CommandLine, StartTime, true, DaysOfWeek, aqObject.EmptyVariant, true, JobID);
  if JobCreateError <> 0 then
    Log.Error('An error (error code ' + aqConvert.IntToStr(JobCreateError) + ') occurred during the job creation')
  else
    Log.Message('The new job has been created successfully');

end;

C++Script, C#Script

function CreateScheduledTask()
{

  var TCPath, ProjectPath, ProjectName, CommandLine, DaysOfWeek, StartTime;
  var NewJob, JobID, JobCreateError;

  // Create a new job object through WMI
  WMI["ComputerName"] = ".";
  NewJob = WMI["Service"]["Get"]("Win32_ScheduledJob");

  TCPath = "C:\\TestCompleteFolder\\TestComplete.exe";
  ProjectPath = "C:\\Work\\SampleProjectSuite.pjs";
  ProjectName = "Sample_Project";

  // Specify parameters for a new scheduled job
  CommandLine = TCPath + " " + ProjectPath + " /r" + " /p:" + ProjectName + " /exit";
  DaysOfWeek = 2 + 8;
  StartTime = "********180000.000000-480";

  // Create a new scheduled job
  JobCreateError = NewJob["Create"](CommandLine, StartTime, true, DaysOfWeek, null, true, JobID);
  if (JobCreateError != 0)
    Log["Error"]("An error (error code " + aqConvert["IntToStr"](JobCreateError) + ") occurred during the job creation")
  else
    Log["Message"]("The new job has been created successfully");

}

See Also

Working With WMI Objects in Scripts
Script Extensions
Pre-Installed Script Extensions
Creating Runtime Objects Tutorial
TestComplete Helper Objects

Highlight search results