Writing Selenium Test Cases

Writing Selenium Test Cases, Select Manual Test Cases, Selenium Test Environment Setup, and write & execute/run Selenium WebDriver Test cases.

To write these Selenium Test cases I used a sample application, You too can use this application for learning/practicing Selenium Test Cases.

Selenium Project

Admin Interface

http://gcreddy.com/project/admin/login.php

User Interface

http://gcreddy.com/project/

Selenium WebDriver Test Cases

Prerequisites for writing Selenium WebDriver Test Cases…

1. Selected Manual Test Case for Automation
2. Element Locators to Locate/Recognize/Identify Elements/Objects
3. Selenium WebDriver API Commands to perform operations on elements
4. Programming Concepts to enhance the Test Cases
5. Testing Framework (Junit/TestNG) to group Test cases, prioritize Test cases, execute Test batches and generate Test Reports.

Note: Selenium WebDriver Test cases in this tutorial were written by using Eclipse IDE, Selenium WebDriver, and Java.

Generally, we use Testing Framework (Ex: TestNG) for inserting verification points and generating Test Reports, but in this practice tutorial, I used Java control flow statements to insert verification points and generating Test Results. So these Test cases are only for learning purposes, in Real-time we use Testing Framework as the Test Runner.

First focus on Selenium Test Environment Setup:

Choose Selenium Tool/s and other third-party plugins & frameworks for testing

Eclipse IDE, Java, Selenium WebDriver, TestNG, Maven, Jenkins, Extend Reports.

Steps:

1. Download & Install Java (JDK) Software – Write / Create & Run / Execute programs / test cases
(We use programming syntax to write test cases, we use programming features called variables, operators, control flow, methods, exception handling, etc, to enhance test cases)

2. Set Java environment variable path (path variable) – to use Java software from any directory in your machine/computer.

3. Download Eclipse IDE & Extract – to write Java programs, syntax guidance, context help, auto compilation, and integrate several plug-ins & frameworks with Selenium.

4. Download Selenium WebDriver Java language binding (from www.selenium.dev) and add WebDriver jar file/s to Java project in Eclipse IDE.

5. Download Browser driver/s and set browser path in test cases (instantiate browser driver)

Writing Selenium Test Cases

Test Case 1: Verify Internal and External Links in wikipedia.org

Test Steps:
1. Launch the Browser
2. Navigate to wikipedia.org selenium page
3. Click the “Create account” link
4. Capture the URL
5. Navigate back to Selenium Page
6. Click the “selenium.dev” link
7. Capture the URL
8. Close the Browser

Verification Point/s:
1. Verify if the 1st URL is an Internal link
2. Verify if the 2nd URL is an External link

Input Data/Test Data:
NA

Expected Result/URL:
1. https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&returnto=Selenium+%28software%29
2. http://www.seleniumhq.org

Selenium WebDriver Test Steps:

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://en.wikipedia.org/wiki/Selenium_(software)#Selenium_WebDriver”);
driver.findElement(By.linkText(“Create account”)).click();
String url1=driver.getCurrentUrl();
//System.out.println(url1);

if (url1.contains(“wikipedia.org”)){
System.out.println(“1st Verification: “+ “It is an Internal Link – Passed”);
}
else{
System.out.println(“1st Verification: “+ “It is Not an Internal Link – Failed”);
}

driver.navigate().back();
//Thread.sleep(4000);

driver.findElement(By.partialLinkText(“selenium.dev”)).click();
//Thread.sleep(4000);
String url2=driver.getCurrentUrl();
//System.out.println(url2);

if (url2.contains(“selenium.dev”)) {
System.out.println(“2nd Verification: “+ “It is an External Link – Passed”);
}
else
{
System.out.println(“2nd Verification: “+ “It is Not an External Link – Failed”);
}
driver.close();
}
}

 

Test Case 2: Verify “Gmail” Link existence in Google Home page.

Test Steps:
1. Launch the Browser
2. Navigate to Google Home page (“https://www.google.co.in”)
3. Return the “Gmail” displayed status

Verification Point/s:
Verify the existence of “Gmail” Link

Input Data:
NA

Selenium WebDriver Test Case:

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com”);
try{
boolean linkExistence=driver.findElement(By.linkText(“abcd”)).isDisplayed();

if (linkExistence == true){
System.out.println(“Gmail Link Exists – Passed”);
}
}
catch(NoSuchElementException e){
System.out.println(“Gmail Link Not Exists – Failed”);
}
driver.close();

Test Case 3: Verify Admin Login Functionality in gcrShop application Admin Interface

Test Steps:
1. Launch Browser
2. Navigate / launch /load login page of admin interface of the gcrShop application (http://gcreddy.com/project/admin/login.php)
3. Enter valid “Username”
4. Enter valid “Password”
5. Click on “Login” Button
6. Capture the current URL
7. Close the Browser window

Verification Point
Compare the captured URL with Expected

Input / Test Data:
Username = gcreddy
Password = Temp@2020

Expected Result:
http://gcreddy.com/project/admin/index.php

Actual Result: * After Test Case Execution

Test Result: pass/fail (* After Test Case Execution)

Comments: * After Test Case Execution

Selenium WebDriver Test Case:

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Downloads\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“http://gcreddy.com/project/admin/login.php”);

driver.findElement(By.name(“username”)).sendKeys(“gcreddy”);
driver.findElement(By.name(“password”)).sendKeys(“Temp@2020”);
driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(2000);

try {
boolean linkExists = driver.findElement(By.linkText(“Logoff”)).isDisplayed();

if (linkExists == true) {
System.out.println(“Admin Login is Successful – Passed”);
}
}
catch (NoSuchElementException e1) {
System.out.println(“Admin Login is Unsuccessful – Failed”);
}
driver.close();

Test Case 4: Customer Registration in gcrShop application User Interface.

Test Steps:
1. Launch Browser
2. Launch Customer Registration Page in gcrShop application User Interface (https://gcreddy.com/project/create_account.php)
3. Enter all mandatory fields
4. Click the “Continue” Button

Verification Point:
Capture the Conformation message and compare it with the expected message

Expected result:
Your Account Has Been Created!

Selenium WebDriver test Case:

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Downloads\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/create_account.php”);

driver.findElement(By.name(“gender”)).click();
driver.findElement(By.name(“firstname”)).sendKeys(“abcd”);
driver.findElement(By.name(“lastname”)).sendKeys(“xyz”);
driver.findElement(By.id(“dob”)).sendKeys(“10/10/2000”);
String email = “sriniram123″+ Math.random()+ “@gmail.com”;
System.out.println(email);
driver.findElement(By.name(“email_address”)).sendKeys(email);

driver.findElement(By.name(“street_address”)).sendKeys(“abcd 2 xyz”);
driver.findElement(By.name(“postcode”)).sendKeys(“500072”);
driver.findElement(By.name(“city”)).sendKeys(“Hyderabad”);
driver.findElement(By.name(“state”)).sendKeys(“Telangana”);

Select dropDown = new Select (driver.findElement(By.name(“country”)));
dropDown.selectByVisibleText(“India”);

driver.findElement(By.name(“telephone”)).sendKeys(“9876767676”);
driver.findElement(By.name(“password”)).sendKeys(“abcd321”);
driver.findElement(By.name(“confirmation”)).sendKeys(“abcd321”);

driver.findElement(By.id(“tdb4”)).click();

driver.findElement(By.name(“password”)).sendKeys(“abcd321”);
driver.findElement(By.name(“confirmation”)).sendKeys(“abcd321”);
driver.findElement(By.id(“tdb4”)).click();

String confirmationMessage = driver.findElement(By.tagName(“h1”)).getText();

if (confirmationMessage.equals(“Your Account Has Been Created!”)) {
System.out.println(“Pass”);
}
else {
System.out.println(“Fail”);
}
driver.close();

Test Case 5: Verify Customer Login in gcrShop Application User Interface.

Test Steps:
1. Launch Browser
2. Navigate to gcrShop User Interface Login Page (https://gcreddy.com/project/login.php)
3. Enter E-Mail Address
4. Enter Password
5. Click “Sign In” Button

Input Data/Test Data:
E-mail Address: meghala1234S@gmail.com
Password: xyz4321

Verification Point:
Verify the Existence of the “Log Off” Link…

Selenium WebDriver Test Case:

System.setProperty(“webdriver.chrome.driver”, “E://chromedriver.exe”);
WebDriver driver = new ChromeDriver();

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

driver.get(“https://gcreddy.com/project/login.php”);

driver.findElement(By.name(“email_address”)).sendKeys(“sriniram123@gmail.com”);
driver.findElement(By.name(“password”)).sendKeys(“abcd321”);
driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(2000);

try {
boolean elementExistence = driver.findElement(By.linkText(“Log Off”)).isDisplayed();

if (elementExistence == true) {
System.out.println(“Customer Login is Successful – Passed”);
}
}
catch (NoSuchElementException e1) {
System.out.println(“Customer Login is Unsuccessful – Failed”);
}
driver.close();

Test Case 6: Verify page redirecting functionality (from Admin Interface to User Interface) in gcrShop application.

Business Rule: It has to redirect from Admin to the User interface, before admin login, and after admin login.

Test Steps:
1. Launch Browser
2. Navigate to the admin interface of the application (http://gcreddy.com/project/admin/login.php)
3. Click the “Online Catalog” link
4. Capture the current URL
5. Back to the previous page
6. Enter valid Username
7.Enter valid Password
8. Click the Login button
9. Capture the current URL
10. Click the “Online Catalog” link
11. Capture the current URL
12. Close Browser window

Verification Points:
1. Verify page redirecting functionality from admin to user interface before login
2. Verify page redirecting functionality from admin to user interface after login

Input / test data:
Username:
Password:

Expected:
1. http://gcreddy.com/project/
2. http://gcreddy.com/project/

Write Selenium WebDriver test steps

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Downloads\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“http://gcreddy.com/project/admin/login.php”);

driver.findElement(By.linkText(“Online Catalog”)).click();
Thread.sleep(2000);
String url1 = driver.getCurrentUrl();
//System.out.println(url1);

if (url1.equals(“http://gcreddy.com/project/”)) {
System.out.println(“Before Login – Page is redirecting from admin to user interface – Passed”);
}
else {
System.out.println(“Before Login – Page is Not redirecting from admin to user interface – Failed”);
}

driver.navigate().back();
driver.findElement(By.name(“username”)).sendKeys(“gcreddy”);
driver.findElement(By.name(“password”)).sendKeys(“Temp@2020”);
driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(2000);
String url2 = driver.getCurrentUrl();

if (url2.equals(“http://gcreddy.com/project/admin/index.php”)) {
driver.findElement(By.linkText(“Online Catalog”)).click();
}

String url3 = driver.getCurrentUrl();
//System.out.println(url3);

if (url3.equals(“http://gcreddy.com/project/”)) {
System.out.println(“After Login – Page is redirecting from admin to user interface – Passed”);
}
else {
System.out.println(“After Login – Page is Not redirecting from admin to user interface – Failed”);
}
driver.close();
}
}

Test Case 7: Verify User Registration in a Forum

Test Steps:
1. Launch Browser
2. Navigate to the User Registration page (https://www.gcreddy.com/discussiongroup/ucp.php?mode=register)
3. Click “I agree to these terms” Button
4. Enter Username
5. Enter Email Address
6. Enter password
7. Confirm password
8. Enter Captcha (verification code)
9. Click Submit button

Verification Point:
Capture the confirmation message and compare it with expected

Expected:
Your account has been created.

Input Data / Test Data:
Name: Indianew
Email: Indianew12@gmail.com
Password: Abcd123
Confirmation:Abcd123
Confirmation code: * Dynamic Data

Selenium WebDriver Test Case:

System.setProperty(“webdriver.chrome.driver”, “E://chromedriver.exe”);
WebDriver driver = new ChromeDriver();

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

driver.get(“https://www.gcreddy.com/discussiongroup/ucp.php?mode=register”);

driver.findElement(By.id(“agreed”)).click();

driver.findElement(By.id(“username”)).sendKeys(“Indianewad”);

String Email = “Indianew”+Math.random()+”@gmail.com”;
driver.findElement(By.id(“email”)).sendKeys(Email);

driver.findElement(By.id(“new_password”)).sendKeys(“Abcd123”);
driver.findElement(By.id(“password_confirm”)).sendKeys(“Abcd123”);

System.out.println(“Enter Verification Code”);
Scanner scan = new Scanner(System.in);
String captcha = scan.nextLine();

driver.findElement(By.id(“confirm_code”)).sendKeys(captcha);

driver.findElement(By.id(“submit”)).click();
Thread.sleep(3000);

String message = driver.findElement(By.xpath(“//*[@id=\”message\”]/div/p”)).getText();

if (message.contains(“Your account has been created.”)) {
System.out.println(“User Registration is Successful – Passed”);
}
else {
System.out.println(“User Registration is Unsuccessful – Failed”);
}
driver.close();

Test Case 8: Data-Driven Testing for “Admin Login” functionality by reading test data from a text file.

What is Data-Driven Testing?
Data-driven testing is the creation of test scripts where test data and/or output values are read from data files instead of using the hard-coded values each time the test runs.

Why Data-Driven Testing?
For conducting Positive and Negative Testing

Difference between Parameterization and Data-Driven Testing?
For passing a single value or multiple values we use Parameterization, we use multiple values only for Data-driven testing.

How to conduct Data-Driven Testing?
By using any resource (Test Data file) we can conduct Data-driven testing.

Steps for Data-Driven Testing:
1. Read Test Data from an external file
2. Create a Test case
3. Connect Test data with the Test case

Selenium Data-Driven Test Case

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Downloads\\chromedriver.exe”);

FileReader fileObject = new FileReader(“C:\\Users\\user\\Desktop\\input.txt”);
BufferedReader br = new BufferedReader(fileObject);
String line;
int lineCount=0;
int Iteration=0;

while ((line = br.readLine()) !=null) {
String inputData [] = line.split(“, “, 2);
lineCount=lineCount+1;

if (lineCount>1) {
Iteration=Iteration+1;

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“http://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(inputData[0]);
driver.findElement(By.name(“password”)).sendKeys(inputData[1]);
driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(2000);

String url= driver.getCurrentUrl();

if (url.equals(“http://gcreddy.com/project/admin/index.php”)) {
System.out.println(Iteration + ” Admin Login is Successful – Passed”);
}
else {
System.out.println(Iteration+” Admin Login is Unsuccessful – Failed”);
}
driver.close();
}
}
br.close();
fileObject.close();
}
}

Test Case 9: Write a Test case for Admin Login Locking Functionality in gcrShop application admin interface.

Business Rule: Admin Login will be locked for 5 minutes after 3 failed login attempts.

Test Steps:
1. Launch the Browser
2. Navigate to (Load) gcrShop admin interface login page /home page (http://gcreddy.com/project/admin/login.php)
3. Enter “Username”
4. Enter “Password”
5. Click the “Login” Button

Verification Point
Capture the Error message and compare it with expected for 4 times

Expected:
1st iteration: Error: Invalid administrator login attempt.
2nd iteration: Error: Invalid administrator login attempt.
3rd iteration: Error: Invalid administrator login attempt.
4th iteration: Error: The maximum number of login attempts has been reached. Please try again in 5 minutes.

Test Data:
Ref: input.txt

Locating Elements:
Username – edit box – name – username
Password – edit box – name – password
Login – Button – id – tdb1
Error Message – Text Area – className – messageStackError

Selenium WebDriver Test Script:

public class TestCase {
static WebDriver driver;
static String ErrorMessage;

public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty(“webdriver.chrome.driver”, “E://chromedriver.exe”);

FileReader file = new FileReader (“C:\\Users\\mahit\\Desktop\\input.txt”);
BufferedReader br = new BufferedReader(file);

int count=0;
int iteration=0;
String line;

while ((line=br.readLine()) !=null) {
count = count+1;

if (count>1) {
iteration = iteration+1;

String inputData[] = line.split(“, “, 2);

driver = new ChromeDriver();

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

driver.get(“http://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(inputData[0]);
driver.findElement(By.name(“password”)).sendKeys(inputData[1]);
driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(2000);

ErrorMessage = driver.findElement(By.className(“messageStackError”)).getText();

if ((iteration == 4) && (ErrorMessage.contains(“The maximum number of login attempts has been reached. Please try again in 5 minutes.”))) {
System.out.println(iteration+ ” Maximum invalid login attempts are over – Passed”);
}
else if ((iteration <= 3) && (ErrorMessage.contains(“Error: Invalid administrator login attempt.”))) {
System.out.println(iteration+ ” Maximum invalid login attempts are Not over – Passed”);
}
else
{
System.out.println(iteration+” Failed”);
}
driver.close();
}
}
br.close();
file.close();
}
}

Test Case 10: Batch Testing using Selenium

Write Selenium WebDriver Test Cases using User-defined Methods/Reusable Components.

Test Case 1: Verify Admin Login (Positive Test Case)
Test Case 2: Verify Error Message in Admin Login Functionality (Negative Test Case)
Test Case 3: Verify Redirect Functionality from Admin Interface to user Interface

Steps:
1. Select Test Cases…
2. Identify the common Functionality among Test Cases
3. Create Reusable Components / User-defined Methods for common Functionality
4. Create Test Cases using Reusable Components / User-defined Methods

Reusable Components:
1. Lunch Browser
2. Admin Login
3. Close Browser…

Selenium WebDriver Test Batch:

public class BatchTesting {
public static WebDriver driver;

//Launch Browser
public void launch(){
driver = new ChromeDriver();
driver.manage().window().maximize();
}
//Close Browser
public void closeBrowser(){
driver.close();
}
//Admin Login
public void login(String Username, String Password){
driver.get(“http://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(Username);
driver.findElement(By.name(“password”)).sendKeys(Password);
driver.findElement(By.id(“tdb1”)).click();
}
public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
BatchTesting obj=new BatchTesting();
//Test Case 1: Verify Admin Login (Positive Test Case)
obj.launch();
obj.login(“admin”, “admin@123”);
String url=driver.getCurrentUrl();

if (url.contains(“http://gcreddy.com/project/admin/index.php”)){
System.out.println(“Test Case 1: “+ “Admin Login is Successful – Passed”);
}
else{
System.out.println(“Test Case 1: “+ “Admin Login is unsuccessful – Failed”);
}
obj.closeBrowser();
//Verify Error Message in Admin Login Functionality (Negative Test Case)
obj.launch();
obj.login(“admin1”, “abcder”);

String ErrorMessage=driver.findElement(By.className(“messageStackError”)).getText();
//System.out.println(ErrorMessage);
if (ErrorMessage.contains(“Error: Invalid administrator login attempt.”)){
System.out.println(“Test Case 2: “+ “Admin Login is Unsuccessful & Showing Correct Error Message -Passed”);
}
else {
System.out.println(“Test Case 2: “+ “Admin Login is Unsuccessful & Not Showing Correct Error Message -Failed”);
}
obj.closeBrowser();
//Test Case 3: Verify Redirect Functionality from Admin Interface to user Interface
obj.launch();
obj.login(“admin”, “admin@123”);
driver.findElement(By.linkText(“Online Catalog”)).click();
String url2=driver.getCurrentUrl();

if (url2.equals(“http://gcreddy.com/project/”)){
System.out.println(“Test Case 3: “+ “Redirected from Admin Interface to User Interface – Passed”);
}
else {
System.out.println(“Test Case 3: “+ “Not Redirected from Admin Interface to User Interface – Passed”);
}
obj.closeBrowser();
}
}


Manual Testing Tutorial

Java Tutorial

Python Tutorial

SQL Tutorial

Selenium, Java, and Testing Videos

Follow me on social media: