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 heighten Test Cases

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

1) Test Case: Verify Internal too External Links inward Wikipedia.org
 

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

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

4) Test Case: Verify Customer Registration inward gcrShop spider web portal
 

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

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

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

8) Test Case: Verify Admin Functionality alongside valid too invalid inputs (Positive too Negative Testing)
 

9) Test Case: Check communication betwixt dissimilar browsers 

10) Data Driven Testing for Admin Login Functionality past times fetching exam information from an external file (Text file).

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

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

----------------------------------------------------------------------
1) Test Case: Verify Internal too External Links inward Wikipedia.org
Internal Link: It redirects to roughly other Page or place inward the same application.

External Link: It redirects to roughly other Page inward 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 dorsum 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 2d URL is an External Link or not?

Input Data:
NA
-------------------------------------------------------
Selenium WebDriver Test Case:
WebDriver driver = novel 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 roughly other page inward the Same Application - Passed");
}
else{
System.out.println("It is an External Link - Redirected to roughly other page inward 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 roughly other page inward Other Application - Passed");
}
else{
System.out.println("It is an Internal Link - Redirected to roughly other page inward the Same Application - Failed");
}
driver.close();
----------------------------------------------------------
2) Test Case: Verify "Gmail" Link beingness inward "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 beingness of Gmail Link

Input Data:
NA
----------------------------------
Selenium WebDriver Test Case:
WebDriver driver = novel 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 alongside Exception Handling

WebDriver driver = novel 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 = novel 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 later Login too Compare alongside expected URL.

Or

Check the beingness 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 = novel FirefoxDriver();
driver.get("https://www.irctc.co.in");
driver.findElement(By.id("usernameId")).sendKeys("gcreddy7");
driver.findElement(By.className("loginPassword")).sendKeys("gld938");

Scanner scan = novel 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 inward gcrShop spider 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 too compare alongside expected

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

WebDriver driver = novel 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 appointment = novel 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 = novel 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 inward 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 electrical current url too compare alongside http://www.gcrit.com/build3/index.php

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

WebDriver driver = novel 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 inward gcrShop spider 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 too compare alongside expected.

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

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

 
WebDriver driver = novel 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 too Invalid Input)

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

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

2) Capture the Error Message too compare alongside expected.

Expected: Error: Invalid administrator login attempt.

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

WebDriver driver = novel 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 too 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 v minutes.
----------------------------------------------
8) Test Case: Check Admin Functionality alongside valid too invalid inputs (Positive too 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 alongside Invalid User Name too / or Password

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

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

Test Data:
User elevate = admina
Password = admin@123a
(Invalid User elevate too Invalid Password)

Other Negative Scenarios:

1) Valid User elevate too Invalid Password
2) Invalid user Name too Valid Password
3) Blank User elevate too Valid Password/Invalid Password
4) Valid / Invalid User elevate too Blank password
5) Blank User elevate too 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 = novel 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 too Showing Correct Error Message-Failed");
}
driver.close();
}
}
}
----------------------------------------------------------------------
9) Test Case: Verify communication betwixt dissimilar browsers
 

Test Steps:
i) Create Mozilla Firefox driver, Google chrome driver too IE driver.
ii) Launch 3 dissimilar applications
iii) Interact from 1 application to another
iv) Close all browsers 1 past times one.
-------------------------------------------
Selenium Test Case:

WebDriver firefoxDriver = novel 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 = novel 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 = novel InternetExplorerDriver();
IEDriver.get("https://in.mail.yahoo.com/");

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

10) Data Driven Testing for Admin Login Functionality past times fetching exam information 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 = novel FileReader("C:/Users/gcreddy/Desktop/input.txt");
BufferedReader br = novel 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 = novel 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 too Showing right 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 = novel 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 too Showing Error Message - Passed");
}
else{
System.out.println("Test Case 1"+ " - Login Successful too Not Showing Error Message - Failed");
}
obj.closeBrowser();
//----------------------------------------------   
//Test Case 3: Redirect from Admin Interface to User Interface later 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 inward a Program using user defined methods/reusable components

public class Class3 extends Class2{

public static void main(String[] args) {
//Create Object
Class3 objNew = novel Class3();
objNew.launchBrowser();
//Test Case 1: Admin Login alongside 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 alongside 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 too Showing Error Message - Passed");
}
else{
System.out.println("Test Case 1"+ " - Login Successful too 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();
}
}
-------------------------------------------------------------------------------
 

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post