How To Write Selenium Test Cases? A Detailed Guide

May 20, 2026 · 6 min read · Tool Comparison

Blog / Insights /
How To Write Selenium Test Cases? A Detailed Guide

How To Write Selenium Test Cases? A Detailed Guide

Contributors Updated on

Learn with AI

Linkedin

Facebook

X (Twitter)

Mail

Learn with AI

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

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

Before You Write Your Selenium Test Case

Achieving 100 % trial reporting go awesome. However, that figure is almost impossible to reach, consider that most teams have resourcefulness and time constraint on their testing projects.

Testers should instead aim to test what their business take. From hither uprise another challenge: the tres amigos (three friends) in software development don ’ t invariably part the same perspective. The tres amigo are:

  • Product owner:Wants to solve a business job.
  • Developer:Builds a solvent to work the product owner 's problem.
  • Tester:Checks if the solution works and identify likely issue.

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 divided language: & nbsp;Gherkin. It is structure by three primary statements: Given, When, and Then, with each statement describing a specific panorama of the system:

  • The Givenstatement sets up the initial context for the behavior and specify the depart point of the scheme.
  • The Whenargument describes the trigger that brings about a change or demeanor in the scheme.
  • The Thenstatement defines the expected outcome that should be observe 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 program concepts such as variables, loops, conditionals, and function is essential.

Additionally, ensure you understand the basics of web technologies:

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

Prepare your integrated growing surround (IDE). Democratic IDEs for Selenium include:

  • Eclipse
  • IntelliJ IDEA
  • VS Code

After that, & nbsp;install Selenium from its official site. Make sure to also establish the WebDriver for the browser you want to test on. & nbsp;

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

Steps To Write a Selenium Test Case

Let ’ s start examine on the Facebook login page.
 


 

Your test cause 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.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Preconditions

- Chrome WebDriver is installed.

- Selenium is set up.

- Valid Facebook certificate 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 abode or profile page.

6. Close browser.

Expected Results

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

Actual Results

(To be filled after test execution.)

Status

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

Comments

(Any extra commentary or observation.)

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 fashion where you don ’ t need to charge the GUI for better performance (learn more aboutheadless browser testing here):

Java
& nbsp; & nbsp; & nbsp; & nbsp; try {
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; driver.get (`` https: //www.facebook.com '');

And we usedriver.get (url)to navigate to the URL you want. In our cause, it ishttps: //facebook.com.

After that, right-click on the Facebook e-mail field and choose Inspect. You should find the ID for it. Selenium will base on this ID to site the field and filling in the information.

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

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

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

 

Here ’ s our total code:

Java
Explain

|

Contributors
The Katalon Team is pen of a various group of dedicated professionals, including subject matter experts with deep domain knowledge, experienced proficient writers skilled, and QA specialiser who bring a virtual, real-world position. Together, they lend to the Katalon Blog, delivering high-quality, insightful clause that empower users to make the most of Katalon ’ s puppet and stay updated on the latest movement in test automation and package quality.

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free