Writing Selenium WebDriver Test Cases

Writing Selenium WebDriver Test Cases

Pre-requisites  to create Selenium Test Cases


i) Test Scenario

ii) Element Locators - To Locate / identify/recognize Elements.

iii) Selenium WebDriver Commands/Methods - To perform Operations on Elements.

iv) Programming Features - To enhance Test cases
------------------
v) JUnit/TestNG Annotations - To group Test cases, Batch Testing and generate Test Reports.
---------------------------------------------------------------
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 or location in other application
-----------
Test Steps:

i) Launch the Browser
ii) Navigate to Selenium page in Wikipedia.org
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 First URL is an Internal Link or not?
ii) Check if the second URL is an External Link or not?
---------------------------------------
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 url = driver.getCurrentUrl();
//System.out.println(url);
if (url.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 the Other Application -Failed");
}
driver.navigate().back();
driver.findElement(By.partialLinkText("seleniumhq.org")).click();
url = driver.getCurrentUrl();

if (! url.contains("wikipedia.org")){
System.out.println("It is an External Link - Redirected to another page in the 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 Element Existence (Gmail link existence in Google home page)

Test Steps:

i) Launch the browser
ii) Navigate to Google.com (Google Home page)
------------------
Verification point:

 
Check the existence of Gmail link.

Selenium Test Case:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
boolean existence = driver.findElement(By.linkText("Gmail")).isDisplayed();

if (existence == true){
System.out.println("Gmail Link Exists - Passed");
}
else {
System.out.println("Gmail Link Not Exists - Failed");
}
}
}
------------------------------------------
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");

try
{
if (driver.findElement(By.linkText("Gmailabc")).isDisplayed()){
System.out.println("Gmail Link Exists - Passed");
}
}

catch (NoSuchElementException e)
{
System.out.println("Gmail Link Not Exists - Failed");
}
driver.close();
}
}
--------------------------------------------------------------
3) Test Case: Login to Indian Railways Online web portal

Test Steps:
 

i) Launch the Browser
ii) Navigate to https://www.irctc.co.in (Indian Railways Online web portal)
iii) Enter User Id
iv) Enter Password
v) Enter Captcha (Verification Code)
vi) Click Login Button
-------------------------------
Verification Point:
 

Capture the URL and Compare with https://www.irctc.co.in/eticketing/home

Test Data:
 

User Id: gcreddy7 (Static Input)
Password: gld938 (Static Input)

Captcha: (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.in is an Input stream
System.out.println("Enter Captcha");
String captcha = scan.nextLine();

driver.findElement(By.className("loginCaptcha")).sendKeys(captcha);
driver.findElement(By.id("loginbutton")).click();
String url = driver.getCurrentUrl();

if (url.equals("https://www.irctc.co.in/eticketing/home")){
System.out.println("Login Successful - Passed");
}
else{
System.out.println("Login Unsuccessful - Failed");   
}
driver.close();
}
}
--------------------------------------------------------
4) Test Case: Verify Customer Registration in gcrShop Web portal
Test Steps:
 

i) Launch the Browser
ii) Navigate to http://gcrit.com/build3/
iii) Enter all Mandatory fields
iv) Click "Continue" Button
-----------------------
Verification point:
 

Capture conformation message and compare with expected.

WebDriver driver = new FirefoxDriver();
driver.get("http://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("Rahman");
driver.findElement(By.name("lastname")).sendKeys("Mohommed");
driver.findElement(By.name("dob")).sendKeys("10/20/1990");
driver.findElement(By.name("email_address")).sendKeys("rahman1237@gmail.com");
driver.findElement(By.name("street_address")).sendKeys("abcd xyz");
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("9234565453");
driver.findElement(By.name("password")).sendKeys("abcd123");
driver.findElement(By.name("confirmation")).sendKeys("abcd123");
driver.findElement(By.id("tdb4")).click();

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

if (ConformationMessage.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

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();
-------------------------------------------------

2 comments:

Very nice and informative videos for learners.
I am getiing org.openqa.selenium.firefox.NotConnectedException when i try to execute in firefox driver.Plz help me to handle this exception.

Post a Comment