logo

NJP

Reduce your Automated Testing maintenance with ATF Custom Steps

Import · Jul 10, 2020 · article
  • Column name "u_template" with Label "Template"

To make sure the newly created record can be used in subsequent test steps, we need to pass on at least the sysid.Additionally, returning the table makes the setup of later tests steps easier.

Let's therefore create two Output Variables of type "String":

  • Column name "u_sys_id" with Label "Sys ID"
  • Column name "u_table" with Label "Table"

The in- and outputs setup should then look similar to this:

image

Description generation script

The Description generation script defines how the Test Step is described in the Step list when it's added to a Test Case. The text should allow Test Designers who are creating Test Cases to quickly understand what will happen in the individual step.

As an example we could populate the description with the name of the template and the table in which the record will be created:

function generateDescription() {
    var description = "";
    var grTemplate = new GlideRecord('sys_template');
    if (grTemplate.get(step.inputs.u_template)) {
        description = 'Create new Record in table ' + grTemplate.getValue('table') + ' using template ' + grTemplate.getDisplayValue('name');
    }
    return description;
}
generateDescription();

Step execution script

The execution script is the heart and soul of a custom step configuration. Like the description script, we have the full freedom of the scripting engine here to perform any operations required.

In this example we are creating a new record in a table using the GlideRecord and GlideTemplate APIs by applying the template record provided via the input variable:

(function executeStep(inputs, outputs, stepResult, timeout) {

    var templateId = inputs.u_template;
    if (gs.nil(templateId)) {
        stepResult.setOutputMessage(gs.getMessage("The '{0}' input variable was not specified", 'templateId'));
        stepResult.setFailed();
        return;
    }

    // create new GlideRecord and apply the template
    var template = new GlideRecord('sys_template');
    if (template.get(templateId)) {
        var testTemplate = new GlideTemplate.get(templateId);

        var grToBeCreated = new GlideRecord(template.getValue('table'));
        grToBeCreated.initialize();
        testTemplate.apply(grToBeCreated);
        outputs.u_sys_id = grToBeCreated.insert();
        outputs.u_table = template.getValue('table');

        stepResult.setSuccess();
    } else {
        stepResult.setFailed();
    }

}(inputs, outputs, stepResult, timeout));

Using the Step Config in an Example Test

For this example we want to test that a Case can be proposed as a Major Case via the UI and that the Case record is then successfully updated.In Step 1 a new Case record is created and all required fields are populated using the "ATF: New Case not assigned" template.Then in Steps 2- 6 a Test User is impersonated and the previously created Case opened and proposed as a Major Case.

In Step 7 we finally verify if the Test Case was successful by checking the Major case state attribute of the record created in Step 1.

image

In case a new mandatory field would now be added to the Case form, we could now simply update the "ATF: New Case not assigned" template without having to update the ATF Test Cases at all.

The above example was just one approach to use Custom Steps.

If you spend a significant amount of time on maintaining ATF Test Cases, it may be worth to investigate if a Custom Step could reduce some of these efforts.

Do you have any other examples for scenarios you addressed with Custom Steps in your testing setup?

Labels:

View original source

https://www.servicenow.com/community/now-platform-blog/reduce-your-automated-testing-maintenance-with-atf-custom-steps/ba-p/2289227