How to Create Test Cases for Automated Tests? With Example

The purpose of this tutorial is to teach you how to write test cases for use in automated testing.

  • insight
  • /
  • blog
  • /
  • how-to-create-test-cases-for-automated-tests

We know that one of the most common questions our clients have is about how to build test suites correctly. Their top priorities are performance, ease of management, and scalability. Because of this, we decided to share this article to share how to write test cases for automated tests.

We also suggest you read our guide: Test Automation Strategy: Importance, Benefits & Example.

A system's functioning can only be tested if it follows a specific set of steps, which are defined in test cases. There are test stages, preconditions, expected outcomes, and actual results that make up a typical test case. In most situations, QA departments will build test cases based on preexisting test scenarios. These test cases outline the broad strokes of the user flows and end-to-end functionality that QA should check to ensure the system meets customer needs. 

The purpose of this tutorial is to teach you how to write test cases for use in automated testing. Let's get started.

how-to-create-test-cases-for-automated-blog.jpg

Which Tests Should Be Automated?

You need a strategy that optimizes your automated tests if you want to see results from your test automation efforts. Because not all tests can be automated, picking the right test cases to automate early on is crucial.

You need not begin from scratch to choose which test cases should be automated. When it comes to automated testing, there are established guidelines for everything from setup to the decision of which tests to run automatically. Here is a quick rundown of the many categories of tests that might benefit most from being automated. You should be on the lookout for the following:

  • Continually repeating tests across different versions.
  • Evaluations whereby the possibility of human mistake is high
  • Analyses that use a plethora of data sets
  • Functionality that is often utilized yet creates potentially dangerous situations.
  • Manually infeasible testing
  • Examinations that are carried out on a wide variety of deployed hardware and software systems
  • Tasks that require a lot of time and effort when manually testing

Here are the different kinds of tests that should be automated to make the testing process faster and more effective:

  • Unit Tests
  • Functional Tests
  • Regression Tests
  • Smoke Tests
  • Data-driven Tests
  • Performance Tests

To learn more about test types, read our blog post: What is Automation Testing?" Types, Examples, Process.

How to Write Test Cases for Automation?

Unlike its manual version, creating an automated test case is a difficult and time-consuming endeavor that calls for a unique approach. Test cases that are automated should further deconstruct workflows than human test cases. Read our article to learn about the Top 20 Automation Testing Tools. The automation tools you choose will determine the specifics of your test case templates, but they should all have the following elements.

Creating a Test Case for Automated Test

Test Scenario: Successfully logging into ronwelldigital.com. 

Test Steps: 

  • Chrome browser opens
  • Navigate to ronwelldigital.com,
  • gets its URL
  • prints its URL
  • takes the title and
  • Prints the title
  • Closes the page

Browser: Chrome v 108

Test Data: URL of the Ronwell Digital 

Expected Result: When Chrome browser opens, it goes to ronwelldigital.com page, gets its URL, prints its URL, gets its header, prints its header and closes the page.

Actual Result: As Expected 

Test Status: Pass/Fail: Pass 

Converting a Test Case to Test Script for Automated Tests 

Selenium is used mostly for automating browser testing. Using Selenium WebDriver, you can simply automate browser testing across browsers including Chrome, Mozilla Firefox, Safari, and Internet Explorer. We thus incorporate selenium dependency into our project. When adding dependencies we are selecting the most using one by people of new versions vulnerability are more than old version.

<!--https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java→-->
   <dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>3.141.59</version>
</dependency>

Using TestNG annotations, start a Chrome browser instance and load the ronwelldigital.com page to write the test script for the preceding test case example. The @Test annotation contains the actual test case.

TestNG annotations require the following configuration in your pom.xml dependencies: either import the necessary external libraries or add the appropriate maven dependency.

<!-- https://mvnrepository.com/artifact/org.testng/testng -->

<dependency>

   <groupId>org.testng</groupId>

   <artifactId>testng</artifactId>

   <version>6.14.3</version>

   <scope>test</scope>

</dependency>

You need to create the test file under src/main/java/directory/javaClass

test-file.png

package org.example;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

public class RonwellPageTest {

   // set Page URL

   public String baseUrl = "https://ronwelldigital.com/";

   // set driver Path

String driverPath = "C:\\Users\\sonmezmi\\IdeaProjects\\Test\\driver\\chromedriver.exe";

   // set WebDriver

   public WebDriver driver ;

   @Test

   public void test() {

       // get the current URL of the page

       String URL= driver.getCurrentUrl();

       // write page in console

       System.out.print(URL);

       // get the title of the page

       String title = driver.getTitle();

       // write page title in console

       System.out.println(title);

   }

   @BeforeTest

   public void beforeTest() {

       // set the system property for Chrome driver

       System.setProperty("webdriver.chrome.driver", driverPath);

       // create driver object for Chrome browser

       driver = new ChromeDriver();

       // wait for opening Chrome browser

       driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

       // maximize Chrome browser

       driver.manage().window().maximize();

       // open page in Chrome browser

       driver.get(baseUrl);

       System.out.println("Open Page Before Test");

   }

   @AfterTest

   public void afterTest() {

       // quit page

       driver.quit();

       System.out.println("Quit Page After Test");

   }

}

On executing the test case as TestNG test, it will launch the browser, navigate to ronwelldigital.com and give the following output on your IntelliJ IDEA Console as seen below:

test-result.png

This test case was prepared by Mikail Sönmez, Software Test Automation Engineer at Ronwell Digital. 

If you have a LinkedIn account, you can find Mikail directly from this link.

References

Devops - 10 Rules for Writing Automated Tests

Techtarget - How to Write Test Cases One Component at a Time 

BrowserStack - Test Cases for Automated Tests 

 

We run all kinds of IT services that vow your success

facebooklinkedininstagramtwitter

2000-2022 © Ronwell Digital