Selenium: How to verify PDF content?

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.util.PDFTextStripper;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

public class PdfReadWrite {

public static void main(String args[]) throws InterruptedException,
IOException {

WebDriver driver = new FirefoxDriver();
driver.get(“http://www.nikon.co.in/tmp/IN/2419865273/3760176746/2586568015/286546384/3855120363/4043737636/4196861409/3724334581/3315196638.pdf”);
;
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// create poiter to URL
URL url = new URL(driver.getCurrentUrl());

// Buffer the file into temporary storage
BufferedInputStream sourceFile = new BufferedInputStream(
url.openStream());

PDFParser parser = new PDFParser(sourceFile);
parser.parse();

// put PDF text in String variable
String textPdf = new PDFTextStripper().getText(parser.getPDDocument());

// in case you want to see where the sub string is
System.out.println(textPdf.indexOf(“D3X Body only”));

// Apply Asserts
try {
Assert.assertTrue(textPdf.contains(“D309X Body only”),
“Text ‘D3X Body only’ is not found”);
} catch (AssertionError e) {

}
// close the document
parser.getPDDocument().close();
driver.quit();

}// main

}// class

//in case you find error, please check you have all the jars mentioned in the import section

Advertisements

How to Save Cost of Testing by Tests Reusability

Reduce Investment planned for Testing by Tests Reusability

In software test life cycle (STLC), among various phases, test case development is most critical and consumes considerable portion of the time, allocated for testing. While working on various projects, my target was to write optimized number of effective test cases.  During each product/project I tested, I used to feel that I have executed the similar tests in the earlier assignments. I am sure you might also have observed, that you are sometime writing the test cases, which you have already written for some other project/product.

Let me explain with an example of Login, whether you are in Finance, health care or CRM domain, test engineer has to write almost similar cases with few more addition or deletion. Also, as a Supervisor, you might have observed that on many occasions your team missed potential test cases; reason could be the time crunch, skill, experience ….. Usually Test case effectiveness is dependent on individual’s skills.  Supervisor faces bigger challenges on estimating the count of test cases to be written as it will provide the basis for test execution time. Also they face challenge to ensure their team is not missing potential test cases as well as spending effort to write quality test cases instead of redundant and exhaustive test cases. I experienced that my various teams used to find many potential test cases during the test execution and add them to the test suite later.

How is the idea if a Team

  • Can estimate testing efforts close to reality
  • Saves investments by shortening the Testing life cycle
  • Instead of investing efforts in writing test cases from scratch, choosing test cases from an organized test cases repository
  • Can verify the completeness of their Tests by checking them against predefined set of cases
  • Can shorten the Test cases Review cycle
  • Ensure the completeness of Test suite
  • deliver quality product by ensuring no missing test cases

Now, how to implement the Test reusability and ensure the completeness of test cases.  I am writing on these topic too. Will publish them soon.

Selenium-WebDriver Code to Write in an excel file

// Before executing this program, create an XLS file, testExcel.xls in D drive

 

import java.io.FileOutputStream;

import java.io.IOException;

import jxl.Workbook;

import jxl.read.biff.BiffException;

import jxl.write.WritableWorkbook;

import jxl.write.WritableSheet;

import jxl.write.Label;

import jxl.write.WriteException;

public class Sample{

public static void main(String[] args) throws IOException, WriteException, BiffException{

//Declare the name of the Excel file

FileOutputStream exlFileName= new FileOutputStream(“D:\\testExcel.xls”);

//making the instance of a Writable Excel workbook and giving it a Name

WritableWorkbook exlWorkBook = Workbook.createWorkbook(exlFileName);

//Creating Writable worksheets in the writable workbook

WritableSheet exlWorkSheet = exlWorkBook.createSheet(“sheet num 0”, 0);

//Creating content for a cell in the excel workbook

Label cellContent = new Label(0,0,”at 0,0 the Content is : good”);

//Adding a cell and inserting content in the cell.

exlWorkSheet.addCell(cellContent);

//Confirm the writing in the excel i.e. Write in the excel workbook.

exlWorkBook.write();

//Close the workbook

exlWorkBook.close();

}//eof main

}//eof class