OptionExists Method

Applies to TestComplete 15.40, last modified on July 05, 2022

Description

This method returns True if an option belongs to the option list of the current section. Else, it returns False. The option is identified by its name. To get a list of names for all options in the section, use the GetOptionNames method.

Declaration

ProgObj.OptionExists(OptionName)

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    
Result Boolean

Applies To

The method is applied to the following objects:

Parameters

The method has the following parameter:

OptionName

Specifies the name of the option whose existence you want to obtain.

Result Value

If the specified option belongs to the option list of the current section, the method returns True. Else, it returns False.

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

GetOptionName Method
GetOptionNames Method
OptionCount Property
SubSectionExists Method

Highlight search results