How To write Test Case is PASS or FAIL using Selenium

How To write Test Case is PASS or FAIL using Selenium,you can write Test case results PASS or FAIL in Excel using J Excel API and TesNG framework with Selenium WebDriver. Most of the time we are working with Excel,Test Cases in order to verify how many test cases are pass/fail in Excel and it is useful for sending the reports to respective managers.


How To write Test Case is PASS or FAIL using Selenium


Write Test Cases PASS or FAIL in SELENIUM

In this post you will learn clear idea about How To write Test Case is PASS or FAIL using Selenium with real time examples.I have created two classes one for ExcelUtility and another for Write the Excel file for each test cases.



Popular Posts

Selenium Xpath Tutorials
Read Data from Properties File
Selenium WebDriver Methods
TestNG Classpath error
Session ID is null using webdriver after calling quit
How to send extent reports in email with screenshots

Pre-Requisites

Step 1: Using JXL jar-You Can Download from Here
Step 2: Selenium WebDriver Configurations
Step 3: xls Test-Data file.
Step 4: addCell()
Step 5: Test Case Results
Step 6: Test Cases
Step 7: Read the xls file FileInputStream
Step 8: Read Workbook from FileINputStream
Step 9: Read Sheets from Workbook
Step 10: Column,rows

ExcelUtility Class

package com.selenium.Utility;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class Excelutility {

public String Testcase; 
public WritableSheet writablesh; 
public WritableWorkbook workbookcopy;

@BeforeTest
public void queryParameterization() throws BiffException,IOException,RowsExceededException,WriteException, InterruptedException{

FileInputStream testfile = new FileInputStream("E:\\AppiumTutorials\\Selenium_Practice\\Testdata\\TestData.xls");//Now get 
WorkbookWorkbook wbook = Workbook.getWorkbook(testfile);
//Now get Workbook 
SheetSheet sheets = wbook.getSheet("Query_data");
int Norows = sheets.getRows();
//Read rows and columns and save it in String Two dimensional array
String inputdata[][] = new String[sheets.getRows()][sheets.getColumns()];
System.out.println("Number of rows present in TestData xls file is -"+Norows);
//For writing the data into excel we will use 
FileoutputStream classFileOutputStream testoutput = new FileOutputStream("E:\\AppiumTutorials\\Selenium_Practice\\SeleniumYoutube\\Testdata\\TestData_results.xls");
System.out.println("creating file one");
//To Create writable workbook
workbookcopy = Workbook.createWorkbook(testoutput);
System.out.println("creating file 2");
//To Create Writable sheet in Writable workbook
writablesh = workbookcopy.createSheet("Query_data",0);
System.out.println("creating file 3");

//Using for loop to write all the data to new sheet 
for(int i=0;i<sheets.getRows();i++) { 
for(int k=0;k<sheets.getColumns();k++) {
 inputdata[i][k] = sheets.getCell(k, i).getContents(); 
Label l = new Label(k, i, inputdata[i][k]); 
Label l2 = new Label(4,0,"Results"); 
writablesh.addCell(l); 
writablesh.addCell(l2); 

            }

        }

}


@AfterTest
public void writeexcels(){ 
try { 
workbookcopy.write(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 

try { 
workbookcopy.close(); 
} catch (WriteException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
     } 

  }

}

WriteExcel Class

package com.selenium.elements;
import java.util.concurrent.TimeUnit;
import jxl.write.Label;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import com.selenium.Utility.Excelutility;


public class WriteexcelFile extends Excelutility {

 WebDriver driver;
 String baseUrl = "https:hploadrunnertutorial.blogspot.in/";

 @Test(priority=1,description="Test Case 1-Open Firfox Browser and open baseUrl")

 public void openBrowser() throws RowsExceededException, WriteException{
 driver = new FirefoxDriver();
 driver.manage().window().maximize();
 driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
 driver.get(baseUrl);
 System.out.println("baseUrl is open in Firefox Browser");
 String Title=driver.getTitle();
 System.out.println(Title);

 if(Title.equalsIgnoreCase("HP Loadrunner Tutorial")){

 Testcase="PASS";

 }else{

 Testcase = "FAIL";

 }

 Label l3=new Label(4,1,Testcase);
 writablesh.addCell(l3);
 }

 @Test(priority=2,description="Test Case 2-Submit Query in the System")
 public void submitQuery() throws InterruptedException, RowsExceededException, WriteException{
 String name = writablesh.getCell(1,3).getContents();
 System.out.println(name);
 driver.findElement(By.id("ContactForm1_contact-form-name")).sendKeys(name);
 System.out.println("System enters name field.");
 Thread.sleep(3000);
 driver.findElement(By.id("ContactForm1_contact-form-email")).clear();
 String emails = writablesh.getCell(2,3).getContents();
 driver.findElement(By.id("ContactForm1_contact-form-email")).sendKeys(emails);
 System.out.println("System clears the fields");
 driver.findElement(By.id("ContactForm1_contact-form-email-message")).clear();
 String message = writablesh.getCell(3,3).getContents();
 driver.findElement(By.id("ContactForm1_contact-form-email-message")).sendKeys(message);
 driver.findElement(By.id("ContactForm1_contact-form-submit")).click();
 Thread.sleep(3000);
 String nametext = driver.findElement(By.id("ContactForm1_contact-form-name")).getAttribute("value");
 System.out.println("Print nametext is -"+nametext);
 Thread.sleep(3000);

 if(nametext.isEmpty()){
 Testcase = "PASS";

 }else{

 Testcase = "FAIL";

 }

 Label l3=new Label(4,2,Testcase);
 writablesh.addCell(l3);

       }
}



Please watch VIDEO Tutorial:






Test Results:

How To write Test Case is PASS or FAIL using Selenium


TESTNG Results:



How To write Test Case is PASS or FAIL using Selenium



Please provide your valuable comments on this topic and Please try to practice this example and provide your feedback.For more real time examples,please Read Selenium Complete Tutorials.

Post a Comment

2 Comments

  1. Automated testing tools are capable of executing tests, reporting outcomes and comparing results with earlier test runs. Tests carried out with these tools can be run repeatedly, at any time of day. The method or process being used to implement automation is called a test automation framework.
    Selenium Training in Chennai
    Selenium Training in Velachery
    Selenium Training Course Fees

    ReplyDelete
  2. Tester should always focus in test data that he is going use for validating an application. Aim of this article is to provide importance of test data in testing as well as to provide the standard guidelines to prepare test data that can be used for testing.

    Test Data Introduction:


    Test data is nothing that the inputs that are planned by the tester to test the software/application. These planned data or inputs are added as column in the test case to showcase that these type of input should be given. This documentation of adding the inputs will be done during the test case preparation.

    Based on the input expected result of the application functionality will get differs that are all added as different scenarios during the test case preparation. Check out the top 10 difference between manual & automation testing before going further.
    Best Tips to Design your Test Data Properly

    ReplyDelete