How To Automate ServiceNow With Selenium

Faisal Khatri

Posted On: February 16, 2023

view count205260 Views

Read time22 Min Read

How To Automate ServiceNow With Selenium

As businesses grow, there is a need to digitize the processes as manual work becomes tedious. For example, a company with 5000+ employees must digitize its IT department for different works like Asset management, IT Operations, queries related to software, managing the Software licenses, etc.

ServiceNow, a cloud platform, provides service, operations, and business management solutions that organizations use to manage their digital workflows and streamline their business processes.

When it comes to testing ServiceNow, Selenium is a popular choice among organizations. Selenium is a widely used open-source automation testing framework that can be used to test ServiceNow workflows and ensure a high-quality user experience.

In this blog, we will learn how to test ServiceNow using Selenium WebDriver to ensure the workflows and applications are working as expected, ultimately improving the overall efficiency and quality of the testing process.

So, let’s get started!

What is ServiceNow?

ServiceNow offers a platform for a fast‑changing world. It is a cloud-based platform that helps to manage your day-to-day operations and plays an important role in enhancing the digital experience for users. The platform provides a wide range of tools and capabilities that can help organizations to improve their digital workflows, automate and optimize processes, and enhance communication and collaboration. ServiceNow helps digitize and unify organizations to find smarter, faster, better ways to make work flow.

Here are some of the figures from the QA 2022 and FY 2022 regarding ServiceNow business:

QA 2022 and FY 2022

Now Platforms

Now Platform helps digitize your entire business by optimizing the processes and creating values on a single unifying platform. Check out the different Now Platforms provided below:

now platform salesforce

Source

Tokyo Release

ServiceNow releases are scheduled two times per year. That latest Tokyo release was done in Q4 of 2022. ServiceNow’s next release, Utah, is planned for Q2 2023. Check out the features from Tokyo General Availability release highlights.

As per the report, ServiceNow Inc improved its market share to approximately 0.79 % due to strong revenue growth of 20.12% overall.

What is ATF (Automated Test Framework)?

ServiceNow testing is performed on supported ServiceNow applications to ensure that the product is free from bugs and all the desired functionalities are working as per the business requirements.

ServiceNow has an Automated Test Framework for testing the ServiceNow applications. Automated Test Framework is an out-of-the-box test framework developed by ServiceNow. It helps test the functionalities in ServiceNow, allowing you to create and run automated tests on your ServiceNow instances (Production & Non-Production).

This is done using the special ATF plugin that aids in accessing the application. It is intended to test the changes you make to your instance and not the ServiceNow functionality.

Using ATF, we can verify and validate application functionalities, test end-to-end scenarios, server-side scripting tests, configurations, customizations, and many more.

Shortcomings of ATF

While the ATF of ServiceNow provides many benefits to organizations to test their digital workflows and applications, there are some potential drawbacks to consider, including:

  • ATF tests ServiceNow workflows in local browsers, but advanced testing requires external automation testing tools. ATF is primarily designed for form testing and might not be suitable for testing other modules, such as testing notifications or service portals.
  • ATF also requires monitoring of the tests that are run.
  • ATF may require manual intervention to run the next tests in succession instead of running the tests automatically once the current test is completed. This is where open-source frameworks like Selenium can be extremely useful for extensively testing the front-end components.

How to test ServiceNow using Selenium WebDriver?

After discussing ATF and its drawback, let’s consider Selenium WebDriver for testing ServiceNow’s Demo Developer instance website, as Selenium has all the required features for website testing.

Application Under Test (ServiceNow Instance)

ServiceNow’s Demo Developer instance is used for executing the tests (Tokyo Release). You can create the instance by following the below steps:

  1. Sign up on the ServiceNow website.
  2. Sign up on the ServiceNow

  3. Once you fill out the form, select the required options displayed after signing up. You should receive an email with the instance link, username, and password.
  4. servicenow password

  5. You can log in and perform the testing using this link and its credentials.

ServiceNow’s Demo Developer Instance – Login Page

servicenow login

Incident Creation Page

Incident Creation Page

Incident List Page

Incident List Page

As per the ServiceNow documentation, the instance hibernates after 10 hours, and refreshing it is required by logging into your ServiceNow account.

Getting Started with ServiceNow testing

Following tools/language has been used in writing and running the tests:

  • Programming Language: Java
  • Web Automation Tool: Selenium WebDriver (Version 4.8.0)
  • Test Runner: TestNG
  • Build Tool: Maven
  • Cloud Platform to run the tests: LambdaTest

Note: In this blog, we are using Selenium 4, the latest version of Selenium. It offers a number of new features and improvements over previous versions, including improved relative locators, integrated support for Chrome DevTools protocol, enhanced WebDriver APIs, and more.

Test Scenarios

We will target the New Incident Creation page and the Incident List page for automation. However, before we move on to those screens, we need to login into the developer instance. Hence our first scenario will be to automate the Login Screen.

The developer instance link will be different for different users as it is assigned by ServiceNow. It is not a common demo website that can be used by all.

Instance URL: https://dev21190.service-now.com/ was assigned to me after signup on ServiceNow.

Test Scenario 1:

Automate the login page by entering the username and password and clicking the Log in button.

servicenow login1

Test Scenario 2:

Navigate to the New Incident page, and verify the header on the page to validate that we are on the right page before we start creating a new incident. Enter valid values in the Caller Name, Channel, select Service and finally, click on the Submit button to create a new incident.

 select Service

Once the new incident is created, it is displayed on the Incident List page.

Test Scenario 3:

The third scenario will be to navigate to the Incident List page and verify the values displayed on the first row, checking that whatever values were entered at the time of the creation of the new incident are displayed correctly here.

In case you are wondering how to write effective test cases for your test scenarios, you can refer to this tutorial on the ServiceNow test case template.

test scenario 3

github

Implementation

As discussed earlier, this project has been created using Maven.

Project Structure and Packages

project

TestNG is used as a test runner. Once the project is created, we need to add the dependency for Selenium WebDriver and TestNG in the pom.xml file.

Dependencies in pom.xml

dependencies in pom

Versions of the dependencies are set in a separate properties block. This is done for easy maintainability. So, if you need to update the versions, you can do it easily without searching throughout the pom.xml file.

Properties block in pom.xml

properties file

The first step is to create a DriverManager class, which will be used to start the specific driver for the required browser.

Configuration (DriverManager class)

Code Walkthrough

Static Variables in the DriverManager class

ThreadLocal class is used here for setting the drivers as it is thread-safe and works very well when tests are run in parallel. The main reason behind using ThreadLocal is that two threads run in isolated ways.ThreadLocal variables, even if two threads set different values on the same ThreadLocal object.

Next is the GRID_URL, which must be set for running tests on the LambdaTest platform. Similarly, LT_USERNAME and LT_ACCESS_KEY are required to authenticate and start new sessions on the LambdaTest platform. You can set these capabilities from the LambdaTest Capabilities Generator.

Cloud testing platforms like LambdaTest allow you to perform cross browser testing at scale over an online browser farm of 3000+ browsers and operating systems. It offers a cloud Selenium grid, using which you can automate testing ServiceNow workflows to ensure a high-quality user experience.

Subscribe to the LambdaTest YouTube Channel and stay updated with the latest tutorial around Selenium testing, Appium, Playwright automation testing, and more.

Coming back to the LambdaTest Capabilities Generator, it helps you to generate desired capabilities for your Selenium tests in a set of key-value pairs that define the desired behavior of the browser or device under tests, such as browser name, version, platform, screen resolution, and more.

The parameter Browsers used in the createDriver() method is an enum (An enum type is a special data type that enables a variable to be a set of predefined constants), which is used to declare the Browser names on which we are planning to run the tests.

Configuration (Browsers(enum)

Notice the @Getter annotation above enum is imported from Lombok dependency and has to set the Getters for the variables set inside the enum on runtime, helping us to save time and effort by not writing the code for Getter methods inside this class.

Setting the Chrome and Firefox drivers in Cloud

Next important methods to note are setupChromeInCloud() and setupFirefoxInCloud(). These methods help set up Chrome and Firefox browsers in the LamdaTest cloud platform and help run the tests.

setupChromeInCloud() method

setupChromeInCloud

setupFirefoxInCloud() method

setupFirefoxInCloud

These methods have one thing in common – ltOptions, which are required to set the Configurations related to LambdaTest platforms in W3C format since Selenium 4 is W3C compliant. As both methods for setting up browsers need these ltOptions, a common method ltOptions() is created to set the respective configs.

ltOptions() method

ltOptions

How to write automated tests for ServiceNow workflows?

First, we write the base tests where the common configuration, like starting and quitting the drivers will be set up, and this base test will be extended to the Test Class, where actual tests will be written.

BaseTest class

@Parameters annotation in TestNG is used to set the browser parameter from testng.xml. So if we need to run the tests on different browsers, we don’t have to change the code every time. The browser name could be updated in the testng.xml and the test could be run accordingly.

CreateDriver() method from the DriverManager class is called here to set the Drivers as per the Browser value passed.

createDriver() method (DriverManager class)

createDriver

setupBrowserTimeouts() method is called for setting the browser timeouts after the driver is set.

setupBrowserTimeouts() method

setupBrowserTimeouts

All our configurations are now set, and we are ready to write the tests. Let’s jump into the Scenarios that we discussed earlier and write tests.

Implementation

Here is the test that covers all three scenarios discussed in the test automation strategy.

ServiceNowTests class

Page Object Model is followed while writing the tests, and since we have three different pages to test; namely, Login Page, Incident Page, and IncidentList Page. Hence, three Page Object classes are created.

Implementation (Test Scenario 1)

As discussed in the automation testing strategy, let’s implement the Login Scenario, where the valid user will log in in the instance.

LoginPage class

Login Page has all the locators for the Login Screen, which has fields like User name, Password, and the Log in button.

Locating the User name field

Locating the User name field

Username field has the ID locator in the DOM, which can be used to locate the field.

Similarly, the Password Field and Login button can be located using the appropriate locator strategies (e.g., Name, id, linkText, etc.)

Locating the Password field

Locating the Password field

Locating the Log in button

Locating the Log in button

the Log in button

performLogin() method helps to login into the website. Here, we are not passing the username and password values in the method parameter. Instead, we get it using the System.getProperty() method.

This is done to ensure that we don’t expose the Username and Password secret values while testing. The values in the fields are entered using sendKeys() in Selenium WebDriver.

performLogin() method

performLogin() method

Once the login is successful, we will navigate to the new incident creation page using its respective URL and return a new instance of NewIncidentPage.

Implementation (Test Scenario 2)

In Scenario 2, we discussed creating a new Incident by checking the page header first and then moving ahead to create a new incident.

New Incident Creation Page

New Incident Creation Page

Test for creating a new Incident

Test for creating a new Incident

NewIncidentPage class is created for locating the fields required to create a new incident. createNewIncident() method performs the action required to create a new Incident.

NewIncidentPage class

Lets deep dive into the createNewIncident() method. This method accepts three parameters, namely, callerName, channelName, and shortDesc, which will help us to generate a new entry for the incident.

createNewIncident() method

The Caller Name field is an input text field. Lets locate this field using Chrome DevTools.

Caller Name field

If you look closely, the id to locate the field in the DOM is “sys_display.incident.caller_id“.
However, when it comes to locating the field, the value for id is given as “#sys_display\.incident\.caller_id(Notice the “\” in between the names before the “.”)

“\” is used as an escape character here, as in CSS Selector, “.” is used for class names. However, since we need to pass on dots for full id value, we must pass the “\” as an escape character.

In Java, “ \ ” is used for escape characters to pass the actual “\” for the String value. Hence for the CSS Selector value “ \\ ” is passed.

CSS Selector value

The Channel field is a single select dropdown list. In the DOM, we can see that it has the < Select > tag, which makes it easier to locate the field and use Selenium’s Select class to select the desired option and handle drop-down in Selenium.

handle drop-down in Selenium

Code to select the value from the Channel field

Code to select the value

First, the Channel field is located using the CSS Selector, and a new instance of the Select class is returned from the channelField() method. selectChannelByVisibleText() method accepts the channelName as a parameter and selects the desired channel as provided in the text.

This passing of the parameter allows the tester to pass any visible text available in the field rather than hard coding any available value in the dropdown list.

The next field we need to locate and automate is the Service field.

locate and automate is the Service field

To enter the value in this field, there are two ways, either type in the valid service name in the field or open the Service Search window and select the required service.

open the Service Search window

and select the required service

We will use the second way, i.e., open the Search Service window and choose the service of it. The steps that we need to perform are to click on the Search icon.

Here is the code snippet from the NewIncidentPage class, which will locate the Service field Help button and click on it.

 code snippet from the NewIncidentPage class

Next, move the focus to the Search Service window and click on the Service name to select it.

 Search Service window

selectServices() method performs all the major actions here. It will switch to the new window and select the service (The Second Service name from the Services list in the window is selected).

Here, for switching between windows, we need window handles of all the windows opened. A window handle is a unique identifier that holds the address of all the windows. It is a pointer that returns a String value.

The getWindowHandles() method helps us store the window handles and switch to the next open window.

Once the Service name selection is done, the Search Service window gets closed, and the focus needs to be shifted to the New Incident window. The following code line will ensure that the focus is shifted back to the original window. To learn more about it, you can go through this blog on handling Windows in Selenium.

Next, the Short Description field is located, and text is entered in it as it is a mandatory field. After that, the Submit button will be clicked and using the below statement, we will navigate to the IncidentList Page.

Assertions are the core of the test. With TestNG assertions, we will ensure that whatever data we have entered in the system using the New Incident Page is displayed correctly on the Incident List Page.

IncidentListPage

The record entered from the New Incident Page is reflected as the first on the Incident List Page.

Implementation (Test Scenario 3)

The IncidentListPage class is created to locate all the elements on this page and return the text of the first row.

IncidentListPage class

If you observe the Incident List Page Screen, it can be seen that it is a table, and values are respectively presented in rows and columns.

Incident List Page Screen with locators

Incident List Page Screen with locators

You will find that most of the locators are located using the CSS Selectors, and the CSS Selectors are almost the same; only the respective child is changed at the end.

respective child is changed at the end

For example, CSS Selector for Opened Column and Short Description column are #incident_table > tbody > tr:nth-child(1) > td:nth-child(4) for opened Column.

 CSS Selector for Opened Column and Short Description

#incident_table > tbody > tr:nth-child(1) > td:nth-child(5) for Short Description Column

Short Description Column

nth-child matches every element that is the nth child of its parent. And since the Incident List Page has a table, we can just update the child reference, locate the required column, and get its value.

Now, coming to assertions in the test, in the test, the respective column value will be compared with the expected value that we used while creating a new incident.

expected value that we used while creating a new incident

With the assertions, we have completed our tests and covered all the scenarios we discussed as a test automation strategy.

Let’s now run the tests and check the result.

Test Execution

There are two ways to run the tests for verifying functionalities on the ServiceNow dashboard:

  1. From the IDE using TestNG.
  2. From the CLI using Maven.

Using TestNG

TestNG is used as a test runner; hence testng.xml has been created. Tests can be run by right-clicking on the file and selecting the option Run ‘…\testng.xml’.

The following four values need to be supplied as per the Test Configurations otherwise the tests won’t run:

  • LambdaTest Username
  • LambdaTest Access Key
  • ServiceNow’s Demo Developer Website Username
  • ServiceNow’s Demo Developer WebSite Password

These values can be passed as “-Dwebsite_username=< servicenow_username > -Dwebsite_password=< servicenow_password > -DLT_USERNAME=< LambdaTest Username > -DLT_ACCESS_KEY=< LambdaTest Access Key >”

Screenshot of the test run locally using Intellij IDE

Screenshot of the test run locally using Intellij IDE

Parallel Execution of the Tests:

The tests we just ran using TestNG were executed in parallel. In sequential execution, we have to wait for the first test to finish, and then only the next test gets executed.
Parallel execution saves time in test execution as all the tests are run in parallel at the same time.

For performing parallel testing in TestNG, we need to mention parallel=”tests” in testng.xml. Here, all the test blocks updated in testng.xml will start executing in parallel.

We have already used the ThreadLocal class in our code. Hence our execution will be thread-safe, so we don’t have to worry about things like test sessions overlapping, etc.

ThreadLocal class

Using Maven

To run the tests using maven, the following steps need to be run:

  1. Open command Prompt/Terminal.
  2. Navigate to the root folder of the project.
  3. Type the command on the terminal:
  4. mvn clean test -Dwebsite_username=< servicenow_username > -Dwebsite_password=< servicenow_password > -DLT_USERNAME=< LambdaTest Username > -DLT_ACCESS_KEY=< LambdaTest Access Key >

Screenshot of the test results run using Maven

Screenshot of the test results run using Maven

Once the tests are run successfully, we can check out the LambdaTest Dashboard and view all the video recordings, screenshots, device logs, and step-by-step granular details of the test run in the LambdaTest Analytics.

Check out the screenshots below, which will give you a fair idea of the dashboard for automated app tests.

LambdaTest Dashboard

LambdaTest Dashboard

The following screenshots show the details of the build and the tests that were run. Again, the test name, browser name, browser version, OS name, respective OS version, and screen resolution are all correctly visible for each test.

It also has the video of the test that was run, giving a better idea about how tests were run on the device.

LambdaTest Build Details – Chrome Browser

LambdaTest Build Details - Chrome Browser

ServiceNow Developer’s Demo Instance Test execution on Chrome browser on LambdaTest platform:

LambdaTest Build Details – Firefox Browser

LambdaTest Build Details - Firefox Browser

ServiceNow Developer’s Demo Instance Test execution on Firefox browser on LambdaTest platform:

Conclusion

In this blog, we discussed what ServiceNow is. We looked into the Now Platform and Automated Test Framework(ATF) created by ServiceNow.

Selenium WebDriver, as an external tool, was used to demonstrate how automated testing of the ServiceNow application can be performed over the Chrome and Firefox browsers using a ServiceNow Cloud Testing Platform like LambdaTest. I hope you enjoyed reading this blog and learned something from it.

Happy Testing!

Frequently Asked Questions (FAQs)

Can we automate ServiceNow using Selenium?

Yes, you can automate ServiceNow using Selenium. ServiceNow is a web-based platform, and Selenium is a web automation tool, so you can use Selenium to automate various tasks in ServiceNow, such as filling out forms, clicking on links, and validating data.

What is automation in ServiceNow?

Applications for ServiceNow Process Automation digitize, visualize, and manage cross-enterprise workflows, offering advantages including managing process compliance and ongoing process improvement.

What type of tool is ServiceNow?

Through the automation and simplification of routine processes, the cloud-native platform ServiceNow enables large-scale businesses to improve operational efficiencies.

Is ServiceNow a CRM tool?

Enterprises can use ServiceNow and Salesforce’s feature-rich offerings as CRM and sales platforms. These systems give businesses the ability to streamline internal processes, generate cost savings across several departments, and increase team efficiency.

What is workflow in ServiceNow?

ServiceNow workflows define a set of operations to automate multi-step procedures. Each action has instructions for running the script, logging it, getting approvals, processing records, setting timers, and waiting.

Author Profile Author Profile Author Profile

Author’s Profile

Faisal Khatri

Faisal is a Software Testing Professional having 14+ years of experience in automation as well as manual testing. He is a QA, freelancer, blogger and open source contributor. He loves learning new tools and technologies and sharing his experience by writing blogs.

Blogs: 21



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free