SetOption Method

Applies to TestComplete 14.40, last modified on April 22, 2021

Description

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

If the option you want does not exist, SetOption will create it and assign NewValue to it. Also, this will assign the data type of the new option. To check if an option exists, use the OptionExists method.

Declaration

ProgObj.SetOption(OptionNameNewValue)

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    
NewValue [in]    Required    Variant    
Result None

Applies To

The method is applied to the following objects:

Parameters

The method has the following parameters:

OptionName

Specifies the name of the option whose value you want to change.

NewValue

Specifies the new value of the specified option.

Result Value

None.

Remarks

Be careful when modifying the system registry using the Section object methods. Incorrect modifications made to the registry may affect the functionality of your operating system and the applications installed.

When working with the registry, the Section.SetOption method lets you modify or create 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

SetOptionByIndex Method
GetOption Method
GetOptionNames Method
OptionExists Method
Working With XML Files From Scripts

Highlight search results