How to create Selenium test cases

Related Product On This Page How to extract trial cases from Business RequirementsMarch 17, 2026 · 8 min read · Tool Comparison

Related Product

How to make Selenium test cases

Selenium is one of the most wide secondhand tools for automating web application examine across browsers and platforms.

Writing effective Selenium test cases involves place the right exploiter flows, selecting locators, implementing test logic, and ensuring maintainability through best practices. Whether you ’ re screen a login page or a complete checkout stream, structured Selenium tests assist get bugs early and improve test coverage in CI/CD grapevine.

This guidebook walk you through the step-by-step process of penning, organizing, and executing Selenium tests to ensure honest and scalable browser automation.

How to pull test cause from Business Requirements

All software must undergo before being released. This method tests the application workflow from beginning to end by replicating different user scenarios. The first step in performing an End to End test is to analyze business requirements. A tester take the different user personas involved, aims for, and considers apply automation to accomplish it.

The usual process get with the tester canvas the concern requirements to isolate a set of user stories. These user narrative assist represent the business requirements & # 8211; what a someone utilise the product would like to be able to do. This, in twist, helps cater to a serial of user personas, and create a figure of.

Once the exam scenario are framed, a set of are followed to frame these scenario into a succession of actions that can help verify a particular functionality. This is what is known as a.

Writing a test cause using Selenium

To write the first test case, deal a sample business prerequisite & # 8211;Ensure secure user unveiling to a Account.

Given that a exploiter attempt to gain introduction to a Browserstack Account could either be a new or an existing user, trial scenarios can be framed around both theRegister and Login pages.

For the intention of this article, consider the exploiter persona to be that of a registered exploiter.

On view encounter the Browserstack Login page, ask the following questions:

  1. Can a user login with the correct exploiter ID and password?
  2. What happens when an invalid e-mail id and invalid password are entered into the signifier?
  3. What happens when a valid email id and invalid password are entered into the form?
  4. What happen when an invalid email id and valid password are entered into the form?

All of these test scenario can now be expanded into a set of positive and negative test case.

Read More:

Positive test cases ensure that the ‘ happy-path ’ or expected user journey when the correct data is entered, works as intended. Negative trial cases focalise on invalid actions, often by replicating the input of invalid data or seeking access to an invalid component.

In this case, the first test scenario can lead to the development of a positive test case, whereas the rest lead to negative tryout cases.

Starting out with Selenium essay? Try BrowserStack Test University and get access to existent devices for a hands-on learning experience..

Test Management Reimagined with AI

Join our unrecorded webinar to discover how top QA teams boost test creation swiftness by 90 % using AI.

Example of a Test Case in Selenium

Build a Selenium test case example based on the first exam scenario.

1. Test Scenario:To authenticate a successful user login on

Test Steps:

  • The user navigates to the BrowserStack sign-in page.
  • In the ’ e-mail ’ field, the exploiter enters their registered email address.
  • The user enters the registered password.
  • The exploiter detent ‘ Sign Me In. ’

2. Prerequisites:A registered email ID with a unique username and parole.

3. Browser: Chrome v 86.

4. Test Data: Legitimate username and word.

5. Expected/Intended Results:Once username and password are entered, the web page redirects to the exploiter ’ s dashboard.

6. Existent Results:As Expected

7. Test Status:Pass/Fail: Pass

Read More:

Converting a Selenium Test Case to a Test Script

After configuring the scheme to fulfil exam scripts (as discussed in a previous article on), convert the manual test event into an executable test script. For that, carry out the following steps:

  1. Create a Selenium WebDriver instance.
  2. Perform browser optimization if necessary.
  3. Write a sequence of commands to execute the test measure.
  4. Validate the action performed.

Explore each step in point:

Step 1 & # 8211;To launch the website in a browser of your choosing, set the scheme properties to the path of the needed driver for the browser. Since this example habituate Google Chrome, it should be set to the. The codification for the same is as follows & # 8211;

Webdriver driver = new ChromeDriver (); System.setProperty (`` webdriver.chrome.driver '', `` Path of the chrome driver '');

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

Step 2 & # 8211;Proceed to maximize the browser for a open picture of test cases be executed using the following command.

driver.manage.window.maximize ();

Step 3 & # 8211;Once the basic job are consummate, execute each step of the manual test case articulated in the previous subdivision.

Implement test steps

Here is a step-by-step procedure to implement a Selenium test script:

Step 1 The exploiter voyage to Gmail.com

driver.get (`` https: //www.browserstack.com/users/sign_in '');

Step 2 In the ’ email ’ field, the exploiter recruit their registered e-mail reference.

To perform this step, locate the email field on the Google Sign In variety, and enter the requirement data.

Use the ID in Chrome DevTools to identify the necessary elements for the Email field, and store them in a webelement variable.

driver.findElement (By.id (`` user_email_login '')); WebElement username=driver.findElement (By.id (`` user_email_login ''));

Once the element has be located, perform the requisite action:

username.sendKeys (`` abc @ gmail.com '');

Read More:

Step 3 & # 8211; The user enters the registered password

Repeat the same summons for the password field as well

driver.findElement (By.id (`` user_password '')); WebElement password=driver.findElement (By.id (`` user_password '')); password.sendKeys (`` your_password '');

Step 4 & # 8211; The exploiter clicks ‘ Sign In ’

Once the e-mail and password web constituent have been located and the actions have been perform, the tester can perform the concluding trust action. In this case, the exploiter clicks ‘Sign In.’

login.click ();

Now, validate the actions performed using affirmation. Assertions assist testers compare the expected and actual results of the actions performed. If these are in sync, the test case is said to have passed. Otherwise, the test case is see to have failed.

Read More:

The assertion, in this case, is of the general form:

Assert.assertEquals (String existent, String expected);

The String actual variable throw the post-login value, which is:

String actualUrl= '' https: //live.browserstack.com/dashboard '';

The method below can help get the expected URL:

String expectedUrl= driver.getCurrentUrl ();

Thus, the final code will appear something like this:

Assert.assertEquals (actualUrl, expectedUrl);

If the examination case surpass, it will retrieve the same else will return as having failed.

The last code will be as follows:

import org.openqa.selenium.By; meaning org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; importation org.openqa.selenium.chrome.ChromeDriver; importation org.testng.Assert; import org.testng.annotations.Test; public class LoginAutomation {@ Test public void login () {System.setProperty (`` webdriver.chrome.driver '', `` path of driver ''); WebDriver driver=new ChromeDriver (); driver.manage () .window () .maximize (); driver.get (`` https: //www.browserstack.com/users/sign_in ''); WebElement username=driver.findElement (By.id (`` user_email_Login '')); WebElement password=driver.findElement (By.id (`` user_password '')); WebElement login=driver.findElement (By.name (`` commit '')); username.sendKeys (`` abc @ gmail.com ''); password.sendKeys (`` your_password ''); login.click (); String actualUrl= '' https: //live.browserstack.com/dashboard ''; String expectedUrl= driver.getCurrentUrl (); Assert.assertEquals (expectedUrl, actualUrl);}}

How to Select Test Cases for Selenium Automation

Selenium and other test automation frameworks help reduce ontogenesis cycle timeframes, avoid repetitive tasks and achieve in a quick and effective manner. But, to get the well-nigh out of Selenium tests, it is necessary to concenter on some best practices when selecting test cases for automation.

1. Prize test suites which offer the highest Return on Investment in damage of cost-saving, increase efficiency, and improving test quality.

2. Identify on what they are meant to do. postulate to be automated. Based on business goals and requirement, automatize the followers:

  • Test cases to be executed with different datum sets
  • Test cases to be executed on different environments
  • Test cases to be executed for different user type
  • Test cause that have multiple dependencies, etc

3. Consider the executing time and prove frequency of these test instance. If one or both of these parameters are high, then it is likely a test automation fabric is required to pen these test cases.

Teams can leverage based platforms like BrowserStack that offer a of 3000+ real browsers and device. It empowers teams to run concurrent Selenium tests on coveted existent device-browser combination online. Without the limit of, testers can verify website functionality in. They can accelerate testing time with, and get results faster without compromising on truth.

Read More:

On BrowserStack, examiner can leverage with a legion of CI/CD tools like Jenkins, Travis, Circle CI, etc. They can also utilize multiple features for more comprehensive testing scenario & # 8211;,,.

Talk to an Expert

Best Practices for Writing Selenium Test Cases

Following best praxis ensures your Selenium trial cases are reliable, maintainable, and efficient. Here are the key guidelines:

  1. Use Clear and Consistent Naming Conventions:Name your test methods and variable clearly so they contemplate the use of the test, e.g.,testLoginWithValidCredentials.
  2. Keep Tests Independent:Ensure each trial instance can run independently without relying on the outcome of others. This avoids cascading failures.
  3. Avoid Hardcoded Values:Use form files or environment variables for test data, URLs, certificate, and timeouts.
  4. UseInstead of Thread.sleep ():Implement WebDriver ’ s expressed waits to await for ingredient, reducing craziness and improving executing speed.
  5. Centralize Locators with the:POM promotes reuse and simplifies maintenance by storing element locater and methods in separate page classes.
  6. Validate Outcomes with Assertions:Use potent, meaningful assertion to control application demeanour after each user action.
  7. Log Test Steps and Results Clearly:Add logging at key steps to aid in debugging and traceability during trial failures.
  8. Gracefully:Use try-catch cube or trial model ’ built-in exclusion handling to log mistake and keep the test rooms run.
  9. Run Tests Across Browsers and Devices:Test on real browsers and devices using instrument like BrowserStack Automate to ascertain true cross-platform compatibility.
  10. Integrate with Early: Automate test run as part of your deployment pipeline to catch regression quickly.

Conclusion

Running Selenium tests effectively is key to achieving reliable, scalable, and repeatable exam mechanization for your web applications. By setting up the right environment, choosing suitable browsers and platforms, and integrating with CI/CD line, squad can guarantee quicker feedback and high tryout coverage.

Start little, automate critical exploiter flow first, and scale confidently using tool like for real gimmick and cross-browser testing.

Tags
26,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

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