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 heighten Test Cases
5) JUnit / TestNG Testing Framework Annotations - To grouping Test Cases, Batch Testing, in addition to 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) in addition to compare alongside Expected.
Expected: Google
Result:
---------------------------------------------
a) Selenium Test Case for Mozilla Firefox Browser
WebDriver driver = novel 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 = novel 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 illustration for IE Browser
System.setProperty("webdriver.ie.driver", "E:/IEDriverServer.exe");
WebDriver driver = novel 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 inwards gcrShop Admin Interface
Test Case 2: Verify Redirect Functionality inwards gcrShop (From Admin Interface to user Interface)
Test Case 3: Verify the "Manufacturers" Link beingness inwards gcrShop spider web portal afterwards Admin Login
---------------------------------------------
Selenium Test Batch:
public course of written report BatchTesting {
public static WebDriver driver;
//Launch Browser
public void launchBrowser(){
driver = novel 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 = novel BatchTesting();
//Test Case 1: Verify Admin Login inwards 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 inwards 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 beingness inwards gcrShop spider web portal afterwards 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 non Exists");
}
obj.closeBrowser();
//---------------------------------------------
}
}
---------------------------------------------
iv) Data Driven Testing
What is Data Driven Testing?
Testing the Same Functionality alongside multiple sets of Test Data
Why Data Driven Testing?
Possitive in addition to Negative Testing
How to deport 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 alongside valid in addition to Invalid inputs
Test Steps:
1) Launch the Browser
2) Navigate to "http://www.gcrit.com/build3/admin" URL
3) Enter Username
4) motion into Password
5) Click Login Button
Test Data: Read examination information from a Text File (Input.txt)
Verification Point:
Capture the URL afterwards Login in addition to Compare alongside expected
Expected: http://www.gcrit.com/build3/admin/index.php
---------------------------------------------
Selenium Data Driven Test Case:
public course of written report DataDriven {
public static String line;
public static int Iteration;
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;
Iteration =0;
while ((line = br.readLine()) != null){
count = count +1;
if (count > 1) {
Iteration = Iteration + 1;
String [] InputData = line.split(", ", 2);
WebDriver 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")){
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();
}
}
---------------------------------------------
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 heighten Test Cases
5) JUnit / TestNG Testing Framework Annotations - To grouping Test Cases, Batch Testing, in addition to 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) in addition to compare alongside Expected.
Expected: Google
Result:
---------------------------------------------
a) Selenium Test Case for Mozilla Firefox Browser
WebDriver driver = novel 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 = novel 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 illustration for IE Browser
System.setProperty("webdriver.ie.driver", "E:/IEDriverServer.exe");
WebDriver driver = novel 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 inwards gcrShop Admin Interface
Test Case 2: Verify Redirect Functionality inwards gcrShop (From Admin Interface to user Interface)
Test Case 3: Verify the "Manufacturers" Link beingness inwards gcrShop spider web portal afterwards Admin Login
---------------------------------------------
Selenium Test Batch:
public course of written report BatchTesting {
public static WebDriver driver;
//Launch Browser
public void launchBrowser(){
driver = novel 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 = novel BatchTesting();
//Test Case 1: Verify Admin Login inwards 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 inwards 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 beingness inwards gcrShop spider web portal afterwards 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 non Exists");
}
obj.closeBrowser();
//---------------------------------------------
}
}
---------------------------------------------
iv) Data Driven Testing
What is Data Driven Testing?
Testing the Same Functionality alongside multiple sets of Test Data
Why Data Driven Testing?
Possitive in addition to Negative Testing
How to deport 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 alongside valid in addition to Invalid inputs
Test Steps:
1) Launch the Browser
2) Navigate to "http://www.gcrit.com/build3/admin" URL
3) Enter Username
4) motion into Password
5) Click Login Button
Test Data: Read examination information from a Text File (Input.txt)
Verification Point:
Capture the URL afterwards Login in addition to Compare alongside expected
Expected: http://www.gcrit.com/build3/admin/index.php
---------------------------------------------
Selenium Data Driven Test Case:
public course of written report DataDriven {
public static String line;
public static int Iteration;
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;
Iteration =0;
while ((line = br.readLine()) != null){
count = count +1;
if (count > 1) {
Iteration = Iteration + 1;
String [] InputData = line.split(", ", 2);
WebDriver 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")){
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();
}
}
---------------------------------------------