The Katalon Blog

How To Write Selenium Test Cases? A Detailed Guide

Written by Katalon Team | Jul 18, 2024 8:00:00 AM

Selenium is widely loved by web testers around the world thanks to its versatility and simplicity. Testing with Selenium is quite straightforward, which is why it is commonly used for people who want to go from manual to automation testing.

In this article, we will show you how to write Selenium test cases the right way.

Before You Write Your Selenium Test Case

Achieving 100% test coverage sounds amazing. However, that figure is almost impossible to reach, considering that most teams have resource and time constraints on their testing projects.

Testers should instead aim to test what their business requires. From here arises another challenge: the tres amigos (three friends) in software development don’t always share the same perspective. The tres amigos are:

  • Product owner: Wants to solve a business problem.
  • Developer: Builds a solution to solve the product owner's problem.
  • Tester: Checks if the solution works and identifies potential issues.

Now we need to establish a common ground between them. Instead of going the traditional way where they discuss their plans in three different languages, the tres amigos use a shared language: Gherkin. It is structured by three primary statements: Given, When, and Then, with each statement describing a specific aspect of the system:

  • The Given statement sets up the initial context for the behavior and defines the starting point of the system.
  • The When statement describes the trigger that brings about a change or behavior in the system.
  • The Then statement defines the expected outcome that should be observed after the event mentioned in the When statement.

What To Prepare To Write a Selenium Test Case

To effectively use Selenium, you should have a basic understanding of at least one programming language. Popular choices include Java, Python, C#, and Ruby. Familiarity with programming concepts such as variables, loops, conditionals, and functions is essential.

Additionally, ensure you understand the basics of web technologies:

  • HTML
  • CSS
  • JavaScript
  • Web elements (forms, buttons, links, etc.)

Prepare your integrated development environment (IDE). Popular IDEs for Selenium include:

  • Eclipse
  • IntelliJ IDEA
  • VS Code

After that, install Selenium from its official website. Make sure to also install the WebDriver for the browser you want to test on. 

Now you’re ready to write your Selenium test case.

Steps To Write a Selenium Test Case

Let’s start testing on the Facebook login page.
 


 

Your test case should look like this:
 

Test Case ID

TC_FB_001

Title

Facebook Login with Valid Credentials

Objective

Verify that a user can log in to Facebook with valid credentials.

Preconditions

- Chrome WebDriver is installed.

- Selenium is set up.

- Valid Facebook credentials are available.

Test Steps

1. Launch Chrome using ChromeDriver.

2. Go to the Facebook login page.

3. Type valid email/phone and password into the login fields.

4. Click Log in.

5. Verify that the user is on the Facebook home or profile page.

6. Close browser.

Expected Results

Users should be redirected to the Facebook home or profile page and see login-related elements (e.g., profile picture).

Actual Results

(To be filled after test execution.)

Status

(To be filled after test execution, e.g., Passed/Failed.)

Comments

(Any additional comments or observations.)

Let’s start testing!

First, import the necessary libraries. Here we need ChromeDriver to control Google Chrome. The ChromeOptions statement is needed to run in headless mode where you don’t need to load the GUI for better performance (learn more about headless browser testing here):

Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

Next, set up the path to the ChromeDriver executable in your machine:

Java
public class FacebookLoginTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

Now we can configure ChromeOptions to run in headless mode:

Java
        ChromeOptions options = new ChromeOptions();
      options.addArguments("--headless"); // Run in headless mode (no GUI)

Then we initialize the WebDriver:

Java
        WebDriver driver = new ChromeDriver(options);

And we use driver.get(url) to navigate to the URL you want. In our case, it is https://facebook.com:

Java
        try {
            driver.get("https://www.facebook.com");

And we use driver.get(url) to navigate to the URL you want. In our case, it is https://facebook.com.

After that, right-click on the Facebook email field and choose Inspect. You should find the ID for it. Selenium will base on this ID to locate the field and fill in the information.

Similarly, you can do the same for the password field:

Now locate the elements and use sendKeys to add your email and password to it:

Java
            // Locate email field example:
            WebElement emailField = new WebDriverWait(driver, Duration.ofSeconds(10))
                    .until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
            emailField.sendKeys("test@example.com");
        } finally {
            driver.quit();
        }
    }
}

 

Here’s our full code:

Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class FacebookLoginTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless"); // Run in headless mode (no GUI)

        WebDriver driver = new ChromeDriver(options);

        try {
            driver.get("https://www.facebook.com");
            // Locate email field example:
            WebElement emailField = new WebDriverWait(driver, Duration.ofSeconds(10))
                    .until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
            emailField.sendKeys("test@example.com");
        } finally {
            driver.quit();
        }
    }
}

 

How Katalon helps with Selenium testing

If Selenium testing is quite too technical for you, Katalon helps you overcome it. As an all-in-one test automation tool, Katalon makes automation accessible with built-in frameworks and a wide range of features designed for speed, clarity, and scale.

With a Katalon license, you can:

  • Write UI and API test cases for web, desktop, and mobile apps using Katalon's ready-to-use frameworks
  • Access the Object Repository to store and manage UI elements easily
  • Use the Object Capture tool to record and reuse web element locators
  • Manage test data, apply data-driven testing, and use BDD workflows
  • Run tests locally, remotely, on cloud, or through private environments
  • Trigger test runs from the CLI using Katalon Runtime Engine
  • Connect with top CI/CD tools to run tests after every code update
  • Schedule tests, track progress, and generate visual test reports

Katalon is especially helpful for teams comparing manual vs automated testing. It builds on your existing QA process and enables faster feedback. You can begin with manual test scripts and gradually automate repeatable flows without disrupting current workflows. Download Katalon and start testing today.

The platform supports real-world conditions and runs across 3000 plus combinations of browsers and devices. This makes Katalon an ideal bridge between manual QA testing and scalable quality assurance methods.

📝 Want to move from browser testing manually to automation? Request a demo and explore how Katalon fits your team’s testing strategy.