Writing Selenium Test Cases

Writing Selenium Test Cases
-------------------------------------------------------------------------
Writing Selenium Test Case Part-1 Video

Writing Selenium Test Case Part-2 Video

Writing Selenium Test Case Part-3 Video
-------------------------------------------------------------------------
Prerequisites for writing Selenium WebDriver Test Cases
i) Test Scenario or Manual Test Case

ii) Element Locators - To Locate/Indentify/Recognize Elements

iii) Selenium WebDriver Commands or Methods - To perform operations on Elements

iv) Programming features - To enhance Test Cases

v) JUnit / TestNG Testing Framework Annotations - To group Test Cases, Batch Testing, and generating Test Reports.
----------------------------------------------------------------------
Selenium Test Cases

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

2) Test Case: Verify "Gmail" Link existence in "Google" Home page (Verify Element existence)
 

3) Test Case: Verify Login Functionality in Indian Railways Web Portal
 

4) Test Case: Verify Customer Registration in gcrShop web portal
 

5) Test Case: Verify Customer Login in gcrShop Web portal
 

6) Test Case: Verify Admin Login Functionality in gcrShop web portal (Verification Point for Valid Input)
 

7) Test Case: Verify Admin Login Functionality (Verification Points for Valid input and Invalid Input)
 

8) Test Case: Verify Admin Functionality with valid and invalid inputs (Positive and Negative Testing)
 

9) Test Case: Check communication between different browsers 

10) Data Driven Testing for Admin Login Functionality by fetching test data from an external file (Text file).

11) Writing Selenium WebDriver Test Cases using User defined methods/reusable components.

12) Writing multiple Test Cases in a Program using user defined methods/reusable components.

----------------------------------------------------------------------
1) Test Case: Verify Internal and External Links in Wikipedia.org
Internal Link: It redirects to another Page or location in the same application.

External Link: It redirects to another Page in other application.
------------------------------------
Test Steps:
i) Launch the Browser
ii) Navigate to https://en.wikipedia.org/wiki/Selenium_%28software%29
iii) Click "Create Account" Link
iv) Capture Current URL
v) Navigate back to Selenium Page
vi) Click "selenium.org" Link
vii) Capture Current URL
viii) Close Browser

Verification points:
i) Check if the 1st URL is an Internal Link or not?
ii) Check if the 2nd URL is an External Link or not?

Input Data:
NA
-------------------------------------------------------
Selenium WebDriver Test Case:
WebDriver driver = new FirefoxDriver();
driver.get("https://en.wikipedia.org/wiki/Selenium_%28software%29");
driver.findElement(By.linkText("Create account")).click();
String URL1 = driver.getCurrentUrl();

if (URL1.contains("wikipedia.org")){
System.out.println("It is an Internal Link - Redirected to another page in the Same Application - Passed");
}
else{
System.out.println("It is an External Link - Redirected to another page in Other Application - Failed");
}

driver.navigate().back();
driver.findElement(By.partialLinkText("seleniumhq.org")).click();

String URL2 = driver.getCurrentUrl();

if (! URL2.contains("wikipedia.org")){
System.out.println("It is an External Link - Redirected to another page in Other Application - Passed");
}
else{
System.out.println("It is an Internal Link - Redirected to another page in the Same Application - Failed");
}
driver.close();
----------------------------------------------------------
2) Test Case: Verify "Gmail" Link existence in "Google" Home page (Verify Element existence)

Test Steps:
i) Launch the Browser
ii) Navigate to Google.com (Google Home Page)

Verification Point
i) Check the existence of Gmail Link

Input Data:
NA
----------------------------------
Selenium WebDriver Test Case:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
boolean linkPresent = driver.findElement(By.linkText("Gmail")).isDisplayed();

if (linkPresent == true){
System.out.println("Gmail Link Exists - Passed");
}
else {
System.out.println("Gmail Link Not Exists -Failed");
}
driver.close();
}
----------------------------------------------
Selenium Test Case with Exception Handling

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");

try
{
boolean linkPresent = driver.findElement(By.linkText("xyz")).isDisplayed();
if (linkPresent == true){
System.out.println("xyz Link Exists - Passed");
}
}
catch (NoSuchElementException e){
System.out.println("xyz Link Not Exists -Failed");
}
driver.close();
-----------------------------------------------------
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
//boolean linkPresent = driver.findElement(By.linkText("xyz")).isDisplayed();
try

{
if (driver.findElement(By.linkText("xyz")).isDisplayed()){
System.out.println("xyz Link Exists - Passed");
}
}
catch (NoSuchElementException e){
System.out.println("xyz Link Not Exists -Failed");
}
driver.close();
-------------------------------------------------------
3) Test Case: Login to Indian Railways Web Portal
Test Steps:
i) Launch the Browser
ii) Navigate to https://www.irctc.co.in
iii) Enter User ID
iv) Enter Password
v) Enter Captcha (Verification Code)
vi) Click "Login" Button"

Verification Point
Capture URL after Login and Compare with expected URL.

Or

Check the existence of Signout Link
-------------------------------------------------
Input Data:

User ID: gcreddy7  - Static Input
Password: gld938 - Static Input

Captcha: (Dynamic value) -Dynamic Input
---------------------------------------------------------
Selenium WebDriver Test Case:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.irctc.co.in");
driver.findElement(By.id("usernameId")).sendKeys("gcreddy7");
driver.findElement(By.className("loginPassword")).sendKeys("gld938");

Scanner scan = new Scanner(System.in);
System.out.println("Enter Captcha");
String captcha = scan.nextLine();

driver.findElement(By.className("loginCaptcha")).sendKeys(captcha);
driver.findElement(By.id("loginbutton")).click();

try
{
if (driver.findElement(By.xpath(".//*[@id='topnav']/li[7]/ul/li[5]/a/span")).isDisplayed()) {
System.out.println("Login Successful -Passed");
}
}
catch (NoSuchElementException x){
System.out.println("Login Unuccessful -Failed");   
}
driver.close();
-----------------------------------------------------------------------
4) Test Case: Verify Customer Registration in gcrShop web portal

Test Steps:
i) Launch the Browser
ii) Navigate to http://www.gcrit.com/build3/
iii) Click "create an account" Link
iv) Enter all Mandatory fields
v) Click "Continue" Button

Verification Point:
Capture the conformation message and compare with expected

Expected Message: Your Account Has Been Created!
-------------------------------------------
Selenium WebDriver Test Case:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/");
driver.findElement(By.linkText("create an account")).click();
driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).click();
driver.findElement(By.name("firstname")).sendKeys("Niladri");
driver.findElement(By.name("lastname")).sendKeys("abcde");
driver.findElement(By.name("dob")).sendKeys("10/10/1990");
Date date = new Date();
int x = date.getSeconds();
String Email = "abcderty1"+x+"@yahoo.com";
driver.findElement(By.name("email_address")).sendKeys(Email);
driver.findElement(By.name("street_address")).sendKeys("wrr ft etrtyty");
driver.findElement(By.name("postcode")).sendKeys("12345");
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("9876787678");
driver.findElement(By.name("password")).sendKeys("abcd123");
driver.findElement(By.name("confirmation")).sendKeys("abcd123");
driver.findElement(By.id("tdb4")).click();

String Message = driver.findElement(By.xpath(".//*[@id='bodyContent']/h1")).getText();

if (Message.equals("Your Account Has Been Created!")){
System.out.println("Customer Registration Successful - Passed");
}
else{
System.out.println("Customer Registration Unsuccessful - Failed");   
}
driver.close();
---------------------------------------------------------
5) Test Case: Verify Customer Login in gcrShop Web portal

Test Steps:

i) Launch the Browser
ii) Navigate to http://www.gcrit.com/build3/
iii) Click "login" Link
iv) Enter Email Address
v) Enter Password
vi) Click "Sign In" Button
-----------------------
Verification Point:
Capture current url and compare with http://www.gcrit.com/build3/index.php

Input Data:
Email Address: rahman1237@gmail.com
Password: abcd123
--------------------------
Selenium Test Case:

WebDriver driver = new FirefoxDriver();
driver.get("http://gcrit.com/build3/");
driver.findElement(By.linkText("login")).click();
driver.findElement(By.name("email_address")).sendKeys("rahman1237@gmail.com");
driver.findElement(By.name("password")).sendKeys("abcd123");
driver.findElement(By.id("tdb5")).click();
String url = driver.getCurrentUrl();
//System.out.println(url);

if (url.contains("http://www.gcrit.com/build3/index.php")){
System.out.println("Login Successful - Passed");
}
else{
System.out.println("Login Unsuccessful - Failed");
}
driver.close();
-------------------------------------------------
6) Test Case: Verify Admin Login Functionality in gcrShop web portal (Verification Point for Valid Input)

Test Steps:
i) Launch the Browser
ii) Navigate to gcrShop Admin Interface (http://www.gcrit.com/build3/admin/)
iii) Enter Valid User name
iv) Enter Valid Password
v) Click "Login" Button
-------------------------
Verification Point:
Capture the url and compare with expected.

Expected url:http://www.gcrit.com/build3/admin/index.php

Actual:
Test Data:
User name = admin
Password = admin@123
-----------------------------------
Selenium Test Case:

 
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admin1");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();

String URL = driver.getCurrentUrl();

if (URL.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Admin Login Successful -Passed");   
}   
else {
System.out.println("Admin Login Unsuccessful -Failed");
}
driver.close();
--------------------------------------
7) Test Case: Verify Admin Login Functionality (Verification Points for Valid input and Invalid Input)

Test Steps:
i) Launch the Browser
ii) Navigate to gcrShop Admin Interface (http://www.gcrit.com/build3/admin/)
iii) Enter Invalid User name and / or Password
iv) Click "Login" Button
----------------------------
Verification points:
1) Capture the url and compare with expected.

Expected url:http://www.gcrit.com/build3/admin/index.php

2) Capture the Error Message and compare with expected.

Expected: Error: Invalid administrator login attempt.

Test Data:
User Name: admina
Password: admin@123
----------------------------------------------------
Selenium Test Case:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admin1");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();

String URL = driver.getCurrentUrl();
if (! URL.equals("http://www.gcrit.com/build3/admin/index.php")){
Error_Message = driver.findElement(By.className("messageStackError")).getText();
}

if (URL.equals("http://www.gcrit.com/build3/admin/index.php")) {
System.out.println("Admin Login Successful -Passed");   
}   
else if ((! URL.equals("http://www.gcrit.com/build3/admin/index.php")&& (Error_Message.contains("Error: Invalid administrator login attempt.")))){
System.out.println("Admin Login Unsuccessful and Showing Correct Error Message-Failed");
}
driver.close();
----------------------------------
Assignment:

Verify the maximum Login attempts (For invalid inputs only)
Verification: After 3 attempts it blocks the Login Functionality for 5 minutes.
----------------------------------------------
8) Test Case: Check Admin Functionality with valid and invalid inputs (Positive and Negative Testing)

Test Steps:
i) Launch the Browser
ii) Navigate to gcrShop Admin Interface (http://www.gcrit.com/build3/admin/)
iii) Enter valid "User name"
iv) Enter Valid "Password"
v) Click "Login" Button
---------------
* Repeat the navigation with Invalid User Name and / or Password

Verification points:
i) Capture the url and compare with expected.
Expected: http://www.gcrit.com/build3/admin/index.php

Test Data:
User name = admin
Password = admin@123
--------------
ii) Capture the Error message and compare with expected:
Expected =Error: Invalid administrator login attempt.

Test Data:
User name = admina
Password = admin@123a
(Invalid User name and Invalid Password)

Other Negative Scenarios:

1) Valid User name and Invalid Password
2) Invalid user Name and Valid Password
3) Blank User name and Valid Password/Invalid Password
4) Valid / Invalid User name and Blank password
5) Blank User name and Blank password
-----------------------------------
Selenium Test Case:

public class VerifyAdminlogin {
public static String Error_Message, username, password, iteration;

public static void main(String[] args) {
for (int i =1; i <=2; i++){
if (i == 1){
username ="admin";
password="admin@123";
iteration ="Iteration 1";
}
else if (i == 2){
username ="admin1";
password="admin@123";
iteration ="Iteration 2";   
}

WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("tdb1")).click();

String URL = driver.getCurrentUrl();
if (! URL.equals("http://www.gcrit.com/build3/admin/index.php")){
Error_Message = driver.findElement(By.className("messageStackError")).getText();
}

if (URL.equals("http://www.gcrit.com/build3/admin/index.php")) {
System.out.println(iteration+" - Admin Login Successful -Passed");   
}   
else if ((! URL.equals("http://www.gcrit.com/build3/admin/index.php")&& (Error_Message.contains("Error: Invalid administrator login attempt.")))){
System.out.println(iteration+" -Admin Login Unsuccessful and Showing Correct Error Message-Failed");
}
driver.close();
}
}
}
----------------------------------------------------------------------
9) Test Case: Verify communication between different browsers
 

Test Steps:
i) Create Mozilla Firefox driver, Google chrome driver and IE driver.
ii) Launch three different applications
iii) Interact from one application to another
iv) Close all browsers one by one.
-------------------------------------------
Selenium Test Case:

WebDriver firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.google.com");
firefoxDriver.findElement(By.linkText("Gmail")).click();
String text = firefoxDriver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/h2")).getText();

System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver chromeDriver = new ChromeDriver();
chromeDriver.get("http://www.gcrit.com/build3/create_account.php?osCsid=1vbg1oj32ole3qrcv4b6mr7m24");
chromeDriver.findElement(By.name("firstname")).sendKeys(text);
Thread.sleep(3000);

System.setProperty("webdriver.ie.driver", "E:\\IEDriverServer.exe");
WebDriver IEDriver = new InternetExplorerDriver();
IEDriver.get("https://in.mail.yahoo.com/");

firefoxDriver.close();
chromeDriver.close();
IEDriver.close();
-----------------------------------------------------------

10) Data Driven Testing for Admin Login Functionality by fetching test data from an external file (Text file).

public class Class1 {
public static WebDriver driver;
public static String error_Message;
public static void main(String[] args) throws IOException {

FileReader file = new FileReader("C:/Users/gcreddy/Desktop/input.txt");
BufferedReader br = new BufferedReader(file);

int Count =0;
int Iteration =0;
String line;
while ((line= br.readLine())!=null){
Count = Count+1;
Iteration = Iteration + 1;
if (Count > 1){
String [] inputData = line.split(" ", 2);   
   
driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys(inputData[0]);
driver.findElement(By.name("password")).sendKeys(inputData[1]);
driver.findElement(By.id("tdb1")).click();

String url = driver.getCurrentUrl();

if (! url.equals("http://www.gcrit.com/build3/admin/index.php")){
error_Message = driver.findElement(By.className("messageStackError")).getText();
}

if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println(Iteration+" - Admin Login Successful - Passed");   
}
else if ((! url.equals("http://www.gcrit.com/build3/admin/index.php")) && (error_Message.contains("Error: Invalid administrator login attempt."))){
System.out.println(Iteration+" - Admin Login Unsuccessful and Showing correct Error Message - Failed");   
}
driver.close();
}
}
br.close();
file.close();
}
}
-------------------------------------------------------
11) Writing Selenium WebDriver Test Cases using User defined methods/reusable components.
public static WebDriver driver;

//Launch Browser
public void launchBrowser(){
driver=new FirefoxDriver();
}
//Admin Login without Parameters
public void adminLogin(){
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
}
//Admin Login without Parameters
public void adminLogin(String Username, String Password){
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys(Username);
driver.findElement(By.name("password")).sendKeys(Password);
driver.findElement(By.id("tdb1")).click();
}
//Close Browser
public void closeBrowser(){
if (! driver.toString().contains("null")){
driver.close();   
}
}
public static void main(String[] args) {
Class2    obj = new Class2();
//Test Case 1: Admin Login Test Case (Positive Test Case)
//----------------------------------------------
obj.launchBrowser();
obj.adminLogin();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Test Case 1"+" - Admin Login Successful - Passed");
}
else {
System.out.println("Test Case 1"+" - Admin Login Unsuccessful - Failed");
}
obj.closeBrowser();
//----------------------------------------------   
//Test Case 2: Admin Login (Invalid Input-Negative Testing)   
obj.launchBrowser();
obj.adminLogin("Hyderabad", "admin@123");
String error_Message = driver.findElement(By.className("messageStackError")).getText();
if (error_Message.contains("Error: Invalid administrator login attempt.")){
System.out.println("Test Case 2"+" - Login Successful and Showing Error Message - Passed");
}
else{
System.out.println("Test Case 1"+ " - Login Successful and Not Showing Error Message - Failed");
}
obj.closeBrowser();
//----------------------------------------------   
//Test Case 3: Redirect from Admin Interface to User Interface after Admin Login
obj.launchBrowser();
obj.adminLogin();
driver.findElement(By.linkText("Online Catalog")).click();
String url2 = driver.getCurrentUrl();

if (url2.equals("http://www.gcrit.com/build3/")){
System.out.println("Test Case 3" + " - Redirected to user Interface - Passed");
}
else {
System.out.println("Test Case 3"+" - Not Redirected to user Interface - Failed");
}
obj.closeBrowser();
//----------------------------------------------   
}
}
------------------------------------------------------------------------------
12) Writing multiple Test Cases in a Program using user defined methods/reusable components

public class Class3 extends Class2{

public static void main(String[] args) {
//Create Object
Class3 objNew = new Class3();
objNew.launchBrowser();
//Test Case 1: Admin Login with Valid Inputs
objNew.launchBrowser();
objNew.adminLogin();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Test Case 1"+" - Admin Login Successful - Passed");
}
else {
System.out.println("Test Case 1"+" - Admin Login Unsuccessful - Failed");
}
objNew.closeBrowser();
//Test Case 2: Admin Login with Invalid Inputs
objNew.launchBrowser();
objNew.adminLogin("Hyderabad", "admin@123");
String error_Message = driver.findElement(By.className("messageStackError")).getText();
if (error_Message.contains("Error: Invalid administrator login attempt.")){
System.out.println("Test Case 2"+" - Login Unsuccessful and Showing Error Message - Passed");
}
else{
System.out.println("Test Case 1"+ " - Login Successful and Not Showing Error Message - Failed");
}
objNew.closeBrowser();
//Test Case 3: Redirect from Admin Interface to User Interface
objNew.launchBrowser();
objNew.adminLogin();
driver.findElement(By.linkText("Online Catalog")).click();
String url2 = driver.getCurrentUrl();

if (url2.equals("http://www.gcrit.com/build3/")){
System.out.println("Test Case 3" + " - Redirected to user Interface - Passed");
}
else {
System.out.println("Test Case 3"+" - Not Redirected to user Interface - Failed");
}
objNew.closeBrowser();
}
}
-------------------------------------------------------------------------------
 

1 comments:

Thanks gcreddy for sharing such a wonderful artical

Post a Comment