GetOption Method

Applies to TestComplete 15.63, last modified on April 10, 2024

Description

Use this method to get the value of an option in the current section. The sought-for option is identified by its name. To get a list of names for all options in the section, use the GetOptionNames method.

If the specified option does not exist, GetOption returns DefaultValue. You can check if an option exists using the OptionExists method.

Declaration

ProgObj.GetOption(OptionName, DefaultValue)

ProgObj An expression, variable or parameter that specifies a reference to one of the objects listed in the Applies To section
OptionName [in]    Required    String    
DefaultValue [in]    Required    Variant    
Result Variant

Applies To

The method is applied to the following objects:

Parameters

The method has the following parameters:

OptionName

Specifies the name of the desired option. If you need to retrieve data from an option marked as (Default) in the Windows registry editor, use an empty string as the option’s name.

DefaultValue

Specifies the value to be returned by the method if the specified option does not exist.

Result Value

The value of the specified option.

Remarks

To determine the specified option’s type, TestComplete uses the specified default value’s type. For instance, if you specify a string in the DefaultValue parameter, TestComplete considers the option a string type.

When working with the system registry, the Section.GetOption method lets you get values of the REG_DWORD, REG_SZ and REG_EXPAND_SZ types only. The REG_BINARY and REG_MULTI_SZ value types are not supported.

Example

The following code snippet checks whether the registry key contains the specified value. If it does, the code posts the value to the test log. Otherwise, it adds the value to the key.

JavaScript, JScript

function Test()
{

  var Key, Value, ValueName;
  // Gets an object for the Windows system registry key
  Key = Storages.Registry("TestKey\\SubKey", HKEY_CURRENT_USER);
  // Specifies the name of the value you want to obtain
  ValueName = "NewValue";

  // Checks whether the value exists
  if (Key.OptionExists(ValueName))
    {
    // If the value exists, posts it to the test log
    Value = Key.GetOption(ValueName, "not specified");
    Log.Message(ValueName + ": " + Value);
    }
  else
    {
    // If the value does not exist, creates it
    Log.Warning("The " + ValueName + " value does not exist and will be created.");
    Value = "default value";
    Key.SetOption(ValueName, Value);
    }

}

Python

def Test():

  # Gets an object for the Windows system registry key
  Key = Storages.Registry("TestKey\\SubKey", HKEY_CURRENT_USER)
  # Specifies the name of the value you want to obtain
  ValueName = "NewValue"

  # Checks whether the value exists
  if (Key.OptionExists(ValueName)):
    # If the value exists, posts it to the test log
    Value = Key.GetOption(ValueName, "not specified")
    Log.Message(ValueName + ": " + str(Value))
  else:
    # If the value does not exist, creates it
    Log.Warning("The " + ValueName + " value does not exist and will be created.")
    Value = "default value"
    Key.SetOption(ValueName, Value)

VBScript

Sub Test

  Dim Key, Value, ValueName
  ' Gets an object for the Windows system registry key
  Set Key = Storages.Registry("TestKey\SubKey", HKEY_CURRENT_USER)
  ' Specifies the name of the value you want to obtain
  ValueName = "NewValue"

  ' Checks whether the value exists
  If Key.OptionExists(ValueName) Then
    ' If the value exists, posts it to the test log
    Value = Key.GetOption(ValueName, "not specified")
    Log.Message(ValueName & ": " & Value)
  Else
    ' If the value does not exist, creates it
    Log.Warning("The " & ValueName & " value does not exist and will be created.")
    Value = "default value"
    Call Key.SetOption(ValueName, Value)
  End If

End Sub

DelphiScript

procedure Test();
var Key, Value, ValueName;
begin

  // Gets an object for the Windows system registry key
  Key := Storages.Registry('TestKey\SubKey', HKEY_CURRENT_USER);
  // Specifies the name of the value you want to obtain
  ValueName := 'NewValue';

  // Checks whether the value exists
  if Key.OptionExists(ValueName) then
    begin
    // If the value exists, posts it to the test log
    Value := Key.GetOption(ValueName, 'not specified');
    Log.Message(ValueName + ': ' + Value);
    end
  else
    begin
    // If the value does not exist, creates it
    Log.Warning('The ' + ValueName + ' value does not exist and will be created.');
    Value := 'default value';
    Key.SetOption(ValueName, Value);
    end;

end;

C++Script, C#Script

function Test()
{

  var Key, Value, ValueName;
  // Gets an object for the Windows system registry key
  Key = Storages["Registry"]("TestKey\\SubKey", HKEY_CURRENT_USER);
  // Specifies the name of the value you want to obtain
  ValueName = "NewValue";

  // Checks whether the value exists
  if (Key["OptionExists"](ValueName))
    {
    // If the value exists, posts it to the test log
    Value = Key.GetOption(ValueName, "not specified");
    Log["Message"](ValueName + ": " + Value);
    }
  else
    {
    // If the value does not exist, creates it
    Log["Warning"]("The " + ValueName + " value does not exist and will be created.");
    Value = "default value";
    Key["SetOption"](ValueName, Value);
    }

}

See Also

GetOptionByIndex Method
GetOptionNames Method
GetSubSection Method
OptionExists Method
SetOption Method
Working With XML Files From Scripts

Highlight search results