Create Test Step Scripts

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

To automate a BDD scenario in TestComplete, you create script functions and link these functions with the scenario’s test steps. When you command TestComplete to run the scenario, it will run the script functions bound to the test steps. You can create these functions by recording them or by writing their code manually.

Generate step definitions

The way you start creating script functions depends on how you create BDD scenarios:

  • If you import scenarios from CucumberStudio, TestComplete will automatically generate script functions for the test steps of the imported scenarios and will generate expressions that link these test steps to the script functions. So, you can continue with creating script code for function bodies. See below for more information on this.

  • If you import BDD scenarios from a file on your hard drive,
    – or –
    If you write BDD scenarios manually in TestComplete’s Gherkin editor, then
    you need to write script functions and linking expressions manually, or command TestComplete to generate them. To do this, right-click within the Gherkin editor and select Generate Step Definitions from the context menu, or click this item on the editor toolbar:

    The Generate Step Definitions item

    Click the image to enlarge it.

The item is disabled if you already have script functions for all the test steps in your feature file.

For more information on the syntax of linking expressions, see Binding Script Functions to Test Steps.

Create script code

You can create code for step functions by writing script code in the Code Editor or by recording script.

Record script

Recording is helpful if you want to create script code for test steps faster, or if you have little scripting background to start writing scripts from scratch.

Option 1

To record script code for some script function:

  • Right-click within the function in the Code Editor and select Record This Routine from the context menu:

    Record code for a script function

    If the script function already has some code, TestComplete will ask if you want to re-record the routine or append new statement to the existing code. It will not ask for your confirmation if the function contains only auto-generated script code.

  • To record test commands, simply work with your tested application or website in the same way as an end-user would: select menu items, click buttons, enter text, and so on. TestComplete will record your actions as script statements. You will see them later, after the recording process is over.

  • The Recording toolbar displays the name of the test step for which you are recording the script:

    Record BDD tests - Info on recorded test step

    When you are done with recording for this step, click Stop to stop recording and to view the recorded script statements. Alternatively, you can click Next Step to record script for the next step of your BDD test that has no test commands yet. TestComplete will save the recoded script statements and will continue recording.

Option 2

One more option to start script recording is to select Test > Record > Record Script from the main menu, or to click Record Script on the toolbar:

Start script recording

Click the image to enlarge it.

In this case, TestComplete will record a new script function and you will have to copy script statements from it to the script function that matches your test step.

It might be easier to record a script test for the entire BDD scenario, than to do this for individual test steps. After you record a script for your scenario, you will have to copy individual lines from it to script functions that match individual test steps.

Note: Most likely, the recorded script will use variables to store references to test objects. It is quite possible that some variables will be initialized in one test step, but then they will be used in some other test steps. So, you might need to declare these variables outside any function to make them global variables that are available in different script functions. For more information on global variables, see the documentation on the scripting language you use.

Write script manually

Write script code as you normally would do this for other tests. Below are links to some topics that might be helpful –

Test results

To mark a test step as failed, simply post an error message to the test log during the test step execution:

JavaScript, JScript

Log.Error("An error occurred."); // Error

Python

Log.Error("An error occurred.") # Error

VBScript

Log.Error "An error occurred." ' Error

DelphiScript

// DelphiScript does not support BDD.

C++Script, C#Script

Log["Error"]("An error occurred."); // Error

If the test step’s log has at least one error message, TestComplete will consider the test run failed. Log messages of other types (warnings, images, informative and event messages) are considered as success.

Hooks

To perform some actions before or after execution of a scenario or feature file, you can use hooks. See Using Hooks for details.

Notes

  • In most cases, each test step will have a special script function. To pass information between them, you will have to use global variables. For instance, instead of getting some object multiple times, you can get it once and store it to a global variable that is accessible from all the script functions. For information on how to refer to variables and functions declared in another script unit, see Calling Routines and Variables Declared in Another Unit.

  • Be careful with opening files and allocating resources on one test step, and closing files and deallocating resources on some other steps. It is possible that later the steps will not be executed. This happens, for instance, if the Playback > On error property of your test project is set to Stop project or Stop current item and an error is logged during the test run.

    To work around the issue, we recommend that you change the property value to Continue running and/or create an OnLogError event handler, where you can close open files and free allocated resources.

  • If you prefer using keyword tests for automation, you can record a keyword test for test steps, and then call these keyword steps from script functions.

See Also

Behavior-Driven Development (BDD) With TestComplete
Binding Script Functions to Test Steps

Highlight search results