Syntax.CreateIf Method

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

This method creates and returns a syntax element that corresponds to a conditional (if ... then or if ... then ... else) statement statement. For example:

JavaScript, JScript

if (! Success)
  Log.Error("Checkpoint failed.");

Python

if not Success:
  Log.Error("Checkpoint failed.")

VBScript

If (Not Success) Then
  Call Log.Error("Checkpoint failed.")
End If

DelphiScript

if not Success then
  Log.Error('Checkpoint failed.');

C++Script, C#Script

if (! Success)
  Log["Error"]("Checkpoint failed.");

After you have created the conditional syntax element, use its Condition property to specify the condition to be tested. To specify the statement(s) to be executed if the condition evaluates to True, use the TrueSyntax property. If you need to execute some statement(s) when the condition is False, specify the corresponding syntax element in the FalseSyntax property.

Declaration

Syntax.CreateIf()

Result An IfSyntax object

Applies To

The method is applied to the following object:

Result Value

An IfSyntax object that corresponds to a conditional statement.

Example

The following example generates the assignment statement given in the Description section and displays it in a message box:

JScript

function ShowIfThenStatement()
{
    var oVarName = Syntax.CreateInvoke();
    oVarName.InvokeName = "Success";
    oVarName.IsProperty = true;

    var oCondition = Syntax.CreateCondition();
    oCondition.OperatorType = oCondition.otNot;
    oCondition.Right = oVarName;

    var oMethodCall = Syntax.CreateInvoke();
    oMethodCall.ClassValue = "Log";
    oMethodCall.InvokeName = "Error";
    oMethodCall.IsProperty = false;
    oMethodCall.AddParameter("Checkpoint failed.");

    var oIf = Syntax.CreateIf();
    oIf.Condition = oCondition;
    oIf.TrueSyntax = oMethodCall;

    var strCode = Syntax.GenerateSource(oIf);
    aqDlg.ShowMessage(strCode);
}

VBScript

Sub ShowIfThenStatement
    Dim oVarName, oCondition, oMethodCall, oIf, strCode

    Set oVarName = Syntax.CreateInvoke
    oVarName.InvokeName = "Success"
    oVarName.IsProperty = True

    Set oCondition = Syntax.CreateCondition
    oCondition.OperatorType = oCondition.otNot
    oCondition.Right = oVarName

    Set oMethodCall = Syntax.CreateInvoke
    oMethodCall.ClassValue = "Log"
    oMethodCall.InvokeName = "Error"
    oMethodCall.IsProperty = False
    oMethodCall.AddParameter "Checkpoint failed."

    Set oIf = Syntax.CreateIf
    oIf.Condition = oCondition
    oIf.TrueSyntax = oMethodCall

    strCode = Syntax.GenerateSource(oIf)
    aqDlg.ShowMessage strCode
End Sub

See Also

IfSyntax Object
CreateAssign Method
CreateCollection Method
CreateCondition Method
CreateInvoke Method
CreateVarDef Method
AddSyntaxToScript Method

Highlight search results