The Basics of Automated UI Testing

Posted Sep 14th, 2022

UI testing helps to ensure that the UI of your website or mobile app behaves as expected and that a high-quality user experience is maintained. This guide covers the basics of automated UI testing and how it can help your organization perform full UI testing more efficiently and consistently.

What Is Automated UI Testing?

UI testing is the practice of testing user interactions with the UI (user interface) of an application. This type of testing can help to validate that the “look and feel” of an application is consistent with expectations. It can also verify that actions taken when using the UI result in the expected behaviors. UI testing plays a critical role in ensuring a high level of application quality.

When executing any type of application test, the first step is to develop test cases. Automated UI testing is the process of taking UI test cases and scripting them so that they can be run in an automated fashion. While UI test automation can be very beneficial to a development organization, it is not without its challenges.

The Benefits of Automated UI Testing

UI test automation can greatly improve application quality. Let’s take a closer look at a few of the reasons why this is the case.

Automated UI testing saves time

In all areas of application testing, automated testing is inherently faster than manual testing. By developing scripts to automate UI testing, teams will see significant improvements in the time it takes to test their user interface. Therefore, they will be able to perform full UI testing more frequently, which will enable them to discover bugs more quickly and implement fixes more efficiently.

UI test automation results in increased test coverage

When testing manually, there are only so many test cases that manual testers will be able to get through in a reasonable amount of time. Testing all features and use cases would be extremely time-consuming to do consistently, and doing so consistently is the only way to ensure that changes to the application aren’t negatively impacting existing functionality.

Test automation provides teams with a starting point for efficiently executing tests for a great number of use cases without manual effort. Therefore, teams can develop test scripts to cover a greater percentage of the overall use cases supported by the application to be executed with greater regularity.

Automated UI testing limits the potential for human error

Let’s face it, no matter how careful a tester may be while walking step-by-step through a test script, there is the possibility that he or she may miss a step or take an incorrect action and not notice the mistake. Automated testing, however, is not subject to such errors. Assuming that the test script is written properly, the test will run in the exact same fashion every time it is executed. In this way, the results of the test can be relied upon as an accurate representation of the current state of application functionality.

Automated UI Testing and Its Challenges

While UI test automation can be highly beneficial, it can also be complex to implement and maintain. Below, we’ll analyze a few of the challenges that are experienced when moving towards an automated UI testing strategy.

User interfaces experience frequent changes

The UI is one aspect of the application that can experience frequent changes. This presents an obstacle in the maintenance of UI tests and a challenge in ensuring full (or close to it) test coverage.

For instance, consider a web application with user registration functionality. While the actual underlying functionality for registering a user (saving user information to the database) may remain relatively unchanged, it is likely that the UI will evolve over time to make it easier to register or to entice users to follow through with the registration process. Any change to the UI component could result in the need to alter the UI tests that ensure everything is functioning as designed. This means that the team will need to be aware of the need to validate and maintain these tests as even the smallest UI changes are made, adding overhead to the process for developing and delivering the product to end users.

Comprehensive automated UI testing is difficult in a highly fragmented landscape

A thorough and valuable automated UI testing strategy is a complex venture which is further complicated by the diverse environments leveraged by end users to access the application. More specifically, the number of devices, operating systems, and browsers (not to mention the seemingly infinite number of device/OS/browser combinations) makes it a challenge to put a successful testing strategy in place.

This requires that organizations remain aware of how users are accessing their application. They must also have the testing infrastructure needed to accommodate the appropriate use cases and adapt to the landscape as it evolves.

Tools to Implement Automated UI Testing

Building out an extensive and complete automated UI testing implementation requires the use of one or more UI testing frameworks and technologies. And choosing the correct tools requires an understanding of the application being tested, in addition to insight into the skill set of the team that will be developing the test scripts. Below is a list of a few popular tools in use today and some details as to when they are most useful.

Selenium

Selenium represents a variety of projects that assist in browser automation. Selenium WebDriver, for instance, can be used to automate user interactions in all major browsers. Furthermore, language bindings for Java, Python, C#, JavaScript, and Ruby are available so that the developer can write their test scripts in the language with which they are most comfortable.

Appium

For automating UI tests for mobile web, native, and hybrid applications on iOS and Android, Appium is one popular option. Major benefits of Appium include cross-platform support and the ability to leverage popular programming languages to write your tests.

Cypress

For the modern developer with extensive JavaScript experience, Cypress has become an increasingly popular option for developing automated UI tests. Cypress supports cross-browser testing, is relatively easy to get started with, and has a growing support community.

Getting Started with Automated UI Testing

So what does it take for an organization to get started with UI test automation? Below, we’ll take a look at some of the more critical and necessary steps for ensuring success in automated UI testing.

  • Analyze your application to determine what is required to thoroughly test your application’s UI. Identify all necessary use cases to help ensure a high level of quality and reliability for your end users. Document these use cases to give yourself a starting point for automating.

  • Determine which technologies and tools are most likely to meet your needs. As referenced above, some libraries and technologies are a better fit for specific applications and teams. Choose those that provide you with the functionality needed to build a high level of test coverage, and lean towards those that align with the skill set of the people who will be doing the heavy lifting of automating the tests.

  • Write your test scripts and develop a strategy for executing them in a consistent, efficient, and effective manner. These tests exist to help the team validate functionality, identify problems within the application more easily, and get fixes out the door sooner. Make sure that your strategy for executing these tests provides these benefits.

Developing an Automated UI Test with Selenium

So, with all of this in mind, let’s take a look at what it takes to start developing automated UI tests. For this example, we will use the official Sauce Labs website as the application we are testing. We will identify a test case, then develop a concise test script that will validate this case using Selenium.

Choosing a Test Case

From the Sauce Labs website, we can click the Contact sales link in the middle of the page, bringing us to a form used for contacting the sales team at Sauce Labs. This form contains a field for First Name.

So, for the purposes of this tutorial, let’s develop a test script to ensure that the First Name field is present on the page when leveraging Firefox as our browser.

Developing the test script

To test this particular case, we’ll be leveraging Selenium WebDriver and writing our test using the Java programming language.

After setting up a Maven project and importing the proper dependencies, we’ll create a class called SauceContactSalesTest.java. First, we’ll set up an init() method annotated with JUnit’s @BeforeEach in order to ensure that this will run prior to each test that’s executed.

This method sets up the Firefox driver and instantiates our webDriver class variable:

@BeforeEach
public void init() {
	WebDriverManager.firefoxdriver().setup();
	webDriver = new FirefoxDriver();
	webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}

Next, we’ll write our teardown method annotated with @AfterEach. This method uses the WebDriver quit method to close the browser window and terminate the WebDriver session.

@AfterEach
public void teardown() {
	webDriver.quit();
}

Now, we’re on to developing our actual test script. This method will be called test() and will be annotated with the @Test annotation. The steps automated by the test are as follows:

  1. Navigate to http://www.saucelabs.com.

  2. Click the Contact sales link.

  3. Check for the existence of an element with the name attribute FirstName. Use a Boolean variable to hold the value indicating whether or not this field exists.

  4. Leverage the JUnit assertTrue(boolean) method, passing the aforementioned variable as a parameter. If the FirstName field exists, it will indicate a passed test; if the FirstName field does not exist, on the other hand, it will indicate a failed test.

@Test
public void test() {
	webDriver.get("http://www.saucelabs.com");
	webDriver.findElement(By.linkText("Contact sales")).click();
boolean firstNameElementExists = true;
try {
        webDriver.findElement(By.name("FirstName"));
} catch (NoSuchElementException e) {
        firstNameElementExists = false;
}
        assertTrue(firstNameElementExists);
}

In the interest of completeness, here is the full SauceContactSalesTest class:

package demo.selenium.test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class SauceContactSalesTest {
        public WebDriver webDriver;
	@BeforeEach
	public void init() {
		WebDriverManager.firefoxdriver().setup();
		webDriver = new FirefoxDriver();
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
	}
	@AfterEach
	public void teardown() {
		webDriver.quit();
	}
	
	@Test
public void test() {
	webDriver.get("http://www.saucelabs.com");
	webDriver.findElement(By.linkText("Contact sales")).click();
        boolean firstNameElementExists = true;
try {
        webDriver.findElement(By.name("FirstName"));
} catch (NoSuchElementException e) {
        firstNameElementExists = false;
}       
        assertTrue(firstNameElementExists);
     }
}

Get Started Today with Automated UI Testing on Sauce Labs

UI testing is an important aspect of testing an application to the fullest extent. This type of testing helps to ensure that the UI behaves as expected and that a high-quality user experience is maintained.

Automated UI testing enables development organizations to get the most from their UI tests, allowing them to perform full UI testing more efficiently and with greater regularity. Sign up for a Sauce Labs free trial and start testing today!

About the Author

Scott Fitzpatrick is a Fixate IO Contributor with over nine years of experience as a software developer. During this time he has worked with numerous languages and frameworks, but finds that he most enjoys coding in Java. As a Fixate IO Contributor, Scott savors the opportunities to explore new technologies and learn from fellow contributors.



Topics

Automated TestingVisual Testing

Categories


    Try it free