Getting Started with Tricentis Test Automation for ServiceNow

Introduction

Getting Started

Tricentis Test Automation for ServiceNow provides a testing environment for ServiceNow applications. It includes the following components.

  • Application – A central location for test scripts, reports and dashoards.

  • Server – Executes unattended tests against multiple browsers.

  • Recorder – Enables point-and-click recording. Captures test steps and generates test cases.

  • Mini – Provides instantaneous results and screenshots during test creation.

This guide describes how to create your first test cases with Tricentis Test Automation for ServiceNow.

Tricentis Test Automation for ServiceNow mini

It is recommended that you work through the examples in this guide in on your own, using Tricentis Test Automation for Service Now mini. Tricentis Test Automation for Service Now mini provides instant feedback on your test scripts, and you will be able to watch them execute.

For instructions on how to install Tricentis Test Automation for ServiceNow mini, see the Installation Guide.

Starting Tricentis Test Automation for ServiceNow mini

To start the application select CapIO mini from the Windows Start menu. The CapIO Mini Settings screen is displayed.

Tricentis Test Automation for ServiceNow mini will create an instance of the selected browser which it can issue commands to. If you ever need to close the browser, or if the session times out, you may re-open the browser by opening and closing the configuration panel, using the 'gear' icon on the left menu.

In the Choose a browser to test in field, select the browser in which your ServiceNow tests will run (Chrome is recommended). Tricentis Test Automation for ServiceNow mini will create an instance of the selected browser to which it will send commands. If you ever need to close the browser, or if the session times out, you may re-open the browser by opening and closing the configuration panel, using the icon in the toolbar.

Select SERVICE NOW to display the following screen.

Complete the screen fields, specifying the hostname and version of your ServiceNow instance, and the username and password to be used to connect to ServiceNow. Click X to close the screen and save your changes.

Running Commands

The Tricentis Test Automation for Service Now mini main screen looks like this.

To run commands write or copy code into the Code pane (the upper portion of the window), and click the command in the toolbar. Tricentis Test Automation for ServiceNow mini will first validate the syntax of the script, then it will start executing the commands in sequence in the active browser.

Tricentis Test Automation for ServiceNow mini will first validate the syntax of the script, then it will start executing commands in the active browser.

Your First Test Case

Case Description

The first test case we wish to create is one that creates a new incident in ServiceNow. This involves logging in to ServiceNow, accessing the Create New Incident form, filling in values and submitting the record.

It is better to work from a written plan rather than creating steps in an add-hoc manner. This will keep your test focused and succinct.

Because the ServiceNow commands run by Tricentis Test Automation for ServiceNow match business language very closely, we can specify the steps at a very high level.

  • Log in to ServiceNow.

  • Navigate to the Incident > Create New module.

  • Set the Short Description, Caller and Impact fields.

  • Save the form.

These steps are very general and it will be up to the developer to fill in the details. In practice it never hurts to be very detailed in your case steps.

Logging In

The login command shown below will navigate to the ServiceNow login.do page directly, and log in with the credentials provided in the Service Now settings screen. Note that a login command is required in Tricentis Test Automation for ServiceNow mini; however when executing tests from the Server, a a login command is automatically run at the start of each suite.

Note It is only necessary to log in once with Tricentis Test Automation for ServiceNow mini when a new instance of the browser opens.

client.login(capioConfig.url, capioConfig.username, capioConfig.password);

Navigating To A Module

Once logged in to ServiceNow, you will typically find yourself at the home page. From here you need to select the Create New module under the Incident application.

The state of the menu should always be treated as if the application and section are closed. Trying to select a module which is not visible will result in a failure.

To navigate to the Incident New record form:

  1. Expand the Incident application in the menu.

  2. Select the Create New module.

client.expandApplication('Incident');
client.selectModule('Incident', 'Create New');

Note The selectModule command requires the application as well, since there may be many modules of the same name.

Filling In Field Values

Once we are on the Incident New record form, the next step is to fill in field values. In Tricentis Test Automation for ServiceNow this has been greatly simplified by having a single command which can fill all field types in ServiceNow.

In order to fill in a field, you must use the column name, not the label. This is to ensure consistency on all forms, lists and other areas, and in circumstances where the label has been modified.

The steps to fill in the values for our example are as follows:

  1. Set the Short Description field to 'Lorem ipsum dolor sit...

  2. Set the Impact field to '2 - Medium'

  3. Set the Caller field to 'Abel Tuter'

Note You must be very specific in setting values for Reference Fields and Choice lists. The value specified must match what the system has exactly, including case and spaces.

client.setFieldValue('short_description', 'Lorem ipsum dolor sit...');
client.setFieldValue('impact', '2 - Medium');
client.setFieldValue('caller_id', 'Abel Tuter');

Saving the Incident

The last step is to save the record. How you do this may depend on what the next step would be. You may want to save and stay (Save), or save and return to the previous screen (Submit/Update).

In this case we can use the save and stay command, so we can remain on the form. This action may be either a context menu item or a form button, depending upon how the ServiceNow instance is configured. You will need to select the command which matches your system configuration.

// This is the most likely
client.clickFormContextMenuItem('Save');
// but this may work too
client.clickFormButton('Save');

Your First Test Case

Congratulations, you have created your first test case. From our original steps we have ended up with the script shown below. At this point we may continue on to more steps or we might stop and add validations.

At this point, you would most likely take the working code out of Tricentis Test Automation for ServiceNow mini and put it in a Tricentis Test Automation for ServiceNow Case.

Be sure to remove any login commands from the script before pasting the code in a Tricentis Test Automation for ServiceNow case, as the login command will cause a failure on the Tricentis Test Automation for ServiceNow server.

// Login
// Only run this once, then comment this line out
// client.login(capioConfig.url, capioConfig.username, capioConfig.password);

// Navigate to Create New Incident
client.expandApplication('Incident');
client.selectModule('Incident', 'Create New');

// Set field values
client.setFieldValue('short_description', 'Lorem ipsum dolor sit...');
client.setFieldValue('impact', '2 - Medium');
client.setFieldValue('caller_id', 'Abel Tuter');

// Save the form
client.clickFormContextMenuItem('Save');

Extending the Case

Saving Variables

In many test cases, it is necessary to save information in a case so that it may be reused in later cases. In Tricentis Test Automation for ServiceNow this is done via the shared context object. This is an object which persists for the entire execution of a Suite, and to which the user has complete read and write access.

To get and set properties of the shared context you must use the ctx prefix.

To create a new property, simply assign it a value. The property name must be a valid JavaScript variable name and the value can be any type.

ctx.myStringVariable = 'foobar';
ctx.myBoolVariable = true;
ctx.myObjectVariable = {'subProperty': 'foobar'};

To access a previously stored value simply refer to it anywhere in a case.

client.setFieldValue('short_description', ctx.myStringVariable);

To change a previously stored value you can assign a new value. This value will persist during the execution from this point forward. There is no way to retrieve the previous value, so if you think you might need both it's best to create two variables.

ctx.myStringVariable = 'a new value';

Preserving the Incident Number

At the end of our first test case, and in any case where we need to navigate back to a record, it is good practice to save the number or Sys Id of the record so we can easily get back to the record.

Let's add this to the previous Create Incident test case.

// Login
// Only run this once, then comment this line out
// client.login(capioConfig.url, capioConfig.username, capioConfig.password);

// Navigate to Create New Incident
client.expandApplication('Incident');
client.selectModule('Incident', 'Create New');

// Set field values
client.setFieldValue('short_description', 'Lorem ipsum dolor sit...');
client.setFieldValue('impact', '2 - Medium');
client.setFieldValue('caller_id', 'Abel Tuter');

// Save the form
client.clickFormContextMenuItem('Save');

// Save the incident number
ctx.incidentNumber = client.getFieldValue('number');

Note We are able to add this code here because when we saved, the system navigated us back to the same record. If we had intended to do a submit, we can save the variable BEFORE clicking the button.

Note When executing in Tricentis Test Automation for ServiceNow mini, if you would like to preserve the context, you may manually set values in the application’s Data tab.

Adding Validations

Validation Basics

So far we have looked at performing different actions in Tricentis Test Automation for ServiceNow, but most test cases will have us validate some state of the system. This includes, but is not limited to, field values and attributes, titles and text on the screen and visible and hidden elements.

Tricentis Test Automation for ServiceNow uses a Business Driven Development style of defining validations in its scripts that is made up of two parts: the expression and the assertion.

The expression can be a variable or a function call, and so almost anything will work. The assertion is a combination of statements which starts with .should and describes the assertion. The assertion may contain additional chained words which further qualify the assertion. Finally, the assertion will end in a command which performs the assertion.

Here is a basic validation in Tricentis Test Automation for ServiceNow:

// client.getFieldDisplayValue('caller_id').should.equal('Abel Tuter');
// ----------- Expression ---------------//------ Assertion --------/
// Gets the display value of the Caller    Validates the value equals
// field                                   'Abel Tuter'

Most validations will do a simple check that the expression equals a certain value, but they can be more complex, checking substrings or other properties, and can be inverted to check the opposite case.

// Validate a value contains an expected result
client.getFieldValue('number').should.contain('INC');

// Validate a value has a property that equals an expected result
client.getUniqueValue().should.have.lengthOf(32);

// Negate an assertion by using additional chained words
client.getFieldValue('number').should.not.equal('INC010001');

More information on the types of assertions are available can be found here.

Validations in a Test Plan

When a validation fails, Tricentis Test Automation for ServiceNow will halt the execution of any remaining steps in the script. In the Tricentis Test Automation for ServiceNow Application, validations have their own table so you may separate validations from the test case script. These validations will always run after the case, and their failure will not affect the script or other sibling validations.

As of Tricentis Test Automation for ServiceNow 2.3.1 you may have include validations within the test case itself, any validations that fail in the case will prevent the rest of the case from executing.

Adding a Validation To The Case

Let's add some validations to our script:

  1. Validate the Number field begins with INC.

  2. Validate the Caller field is set to Abel Tuter.

  3. Validate the Sys ID for the record has a length of 32.

  4. Validate the Priority field is set to 3 - Moderate.

Note In Tricentis Test Automation for ServiceNow mini, these validations may be placed at the bottom of the script.

// Validate field values
client.getFieldValue('number').should.contain('INC');
client.getFieldDisplayValue('priority').should.equal('3 - Moderate');
client.getFieldDisplayValue('caller_id').should.equal('Abel Tuter');

// Validate the current records Sys Id
client.getUniqueValue().should.have.lengthOf(32);

The Complete Test Case

Our complete test case, including actions, a saved variable and assertions should look like this.

// Login
// Only run this once, then comment this line out
// client.login(capioConfig.url, capioConfig.username, capioConfig.password);

// Navigate to Create New Incident
client.expandApplication('Incident');
client.selectModule('Incident', 'Create New');

// Set field values
client.setFieldValue('short_description', 'Lorem ipsum dolor sit...');
client.setFieldValue('impact', '2 - Medium');
client.setFieldValue('caller_id', 'Abel Tuter');

// Save the form
client.clickFormContextMenuItem('Save');

// Save the incident number
ctx.incidentNumber = client.getFieldValue('number');

// Validate field values
client.getFieldValue('number').should.contain('INC');
client.getFieldDisplayValue('priority').should.equal('3 - Moderate');
client.getFieldDisplayValue('caller_id').should.equal('Abel Tuter');

// Validate the current records Sys Id
client.getUniqueValue().should.have.lengthOf(32);

Importing Into the Tricentis Test Automation for ServiceNow Application

Now that the test case is complete and working in Tricentis Test Automation for ServiceNow mini, we can transfer it to the Tricentis Test Automation for ServiceNow application. The steps to take are as follows:

  1. Navigate to the correct suite.

  2. Divide the script into the case and the validations.

  3. Create the test case.

  4. Remove any mini only commands.

  5. Attach validations to the case.

For this script, we should end up with 5 total records. The case should contain:

Name: Create Incident

client.expandApplication('Incident');
client.selectModule('Incident', 'Create New');

client.setFieldValue('short_description', 'Lorem ipsum dolor sit...');
client.setFieldValue('impact', '2 - Medium');
client.setFieldValue('caller_id', 'Abel Tuter');

client.clickFormContextMenuItem('Save');

ctx.incidentNumber = client.getFieldValue('number');

And the case should have 4 validations. One record for each validation.

Name: Validate Number Contains 'INC'

client.getFieldValue('number').should.contain('INC');

Name: Validate Priority is '3 - Moderate'

client.getFieldDisplayValue('priority').should.equal('3 - Moderate');

Name: Validate Caller is 'Abel Tuter'

client.getFieldDisplayValue('caller_id').should.equal('Abel Tuter');

Name: Validate Sys ID has a length of 32

client.getUniqueValue().should.have.lengthOf(32);

To Be Continued

This document is a work in progress and additional topics may be added.