Selenium WebDriver Quick Tutorial Part 2

Selenium WebDriver Quick Tutorial Part 2

i) Writing Selenium Test Cases

ii) Cross Browser Testing

iii) Batch Testing

iv) Data Driven Testing
---------------------------------------------
Prerequisites for Writing Selenium WebDriver Test Cases

1) Test Scenario or Manual Test Case

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

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

4) Programming features - To enhance Test Cases

5) JUnit / TestNG Testing Framework Annotations - To group Test Cases, Batch Testing, and generating Test Reports.
---------------------------------------------
ii) Cross Browser Testing

Manual Test Case:

Test Case Name: Verify Launch Application (Google)

Test Steps:

i) Launch the Browser
ii) Navigate to https://www.google.com

Verification Point:
Capture the Page Title (Actual) and compare with Expected.

Expected: Google

Result: 
---------------------------------------------
a) Selenium Test Case for Mozilla Firefox Browser

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

String PageTitle = driver.getTitle();

if (PageTitle.equals("Google")){
System.out.println("Google Application Launched - Passed");
}
else {
System.out.println("Google Application Not Launched - Failed");
}
driver.close();
}
---------------------------------------------
b) Selenium Test Case for Google Chrome Browser

System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");

String PageTitle = driver.getTitle();

if (PageTitle.equals("Google")){
System.out.println("Google Application Launched - Passed");
}
else {
System.out.println("Google Application Not Launched - Failed");
}
driver.close();
}
---------------------------------------------
c) Selenium Test case for IE Browser

System.setProperty("webdriver.ie.driver", "E:/IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://www.google.com");

String PageTitle = driver.getTitle();

if (PageTitle.equals("Google")){
System.out.println("Google Application Launched - Passed");
}
else {
System.out.println("Google Application Not Launched - Failed");
}
driver.close();
}
---------------------------------------------
iii) Batch Testing

Test Case 1: Verify Admin Login in gcrShop Admin Interface

Test Case 2: Verify Redirect Functionality in gcrShop (From Admin Interface to user Interface)

Test Case 3: Verify the "Manufacturers" Link existence in gcrShop web portal after Admin Login
---------------------------------------------
Selenium Test Batch:

public class BatchTesting {
public static WebDriver driver;
//Launch Browser
public void launchBrowser(){
driver = new FirefoxDriver();
}
//Admin Login
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(){
driver.close();
}
public static void main(String[] args) {
BatchTesting obj = new BatchTesting();
//Test Case 1: Verify Admin Login in gcrShop Admin Interface
obj.launchBrowser();
obj.adminLogin("admin", "admin@123");

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: Verify Redirect Functionality in gcrShop (From Admin Interface to user Interface)
obj.launchBrowser();
obj.adminLogin("admin", "admin@123");
driver.findElement(By.linkText("Online Catalog")).click();
String url2 = driver.getCurrentUrl();

if (url2.equals("http://www.gcrit.com/build3/")){
System.out.println("Test Case 2: Redirected from Admin Interface to User Interface - Passed");
}
else {
System.out.println("Test Case 2: Not Redirected from Admin Interface to User Interface - Failed");
}
obj.closeBrowser();
//---------------------------------------------
//Test Case 3: Verify the "Manufacturers" Link existence in gcrShop web portal after Admin Login
obj.launchBrowser();
obj.adminLogin("admin", "admin@123");

try {
boolean linkExistence = driver.findElement(By.linkText("Manufacturers")).isDisplayed();

if (linkExistence == true){
System.out.println("Test Case 3: Manufacturers Link Exists on Index Page - Passed");
}
}
catch (NoSuchElementException e){
System.out.println("Link not Exists");
}
obj.closeBrowser();
//---------------------------------------------
}
}
---------------------------------------------
iv) Data Driven Testing

What is Data Driven Testing?

Testing the Same Functionality with multiple sets of Test Data

Why Data Driven Testing?

Possitive and Negative Testing

How to conduct Data driven Testing?

1) By Dynamic submission of Test Data

2) By Reading Test Data from a Text File

3) By Reading Test Data from an Excel file
Etc...
---------------------------------------------
Data Driven Test Case: Admin Login Functionality with valid and Invalid inputs

Test Steps:

1) Launch the Browser
2) Navigate to "http://www.gcrit.com/build3/admin" URL
3) Enter Username
4) enter Password
5) Click Login Button

Test Data: Read test data from a Text File (Input.txt)

Verification Point:
Capture the URL after Login and Compare with expected

Expected: http://www.gcrit.com/build3/admin/index.php
---------------------------------------------
Selenium Data Driven Test Case:

public class DataDriven {
public static String line;
public static int Iteration;
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;
Iteration =0;
while ((line = br.readLine()) != null){
count = count +1;
if (count > 1) {
Iteration = Iteration + 1;
String [] InputData = line.split(", ", 2);

WebDriver 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")){
System.out.println("Iteration-"+Iteration+ " - Admin Login Successful - Passed");
}
else {
System.out.println("Iteration-"+Iteration+ " - Admin Login Unsuccessful - Failed");
}
driver.close();
}
}
file.close();
br.close();
}
}
---------------------------------------------

1 comments:

Post a Comment