Exploiting JXL using Selenium

17
OSSCube Exploiting JXL using Selenium 27 Feb 2015 Deepak Dua Anil Kumar

Transcript of Exploiting JXL using Selenium

OSSCube

Exploiting JXL using Selenium

27 Feb 2015

Deepak Dua

Anil Kumar

2

Objective

• UNDERSTAND WHAT IS JXL

• WHY JXL

• OTHER SIMILAR APPROACH

• UNDERSTAND BASIC CODE OF JXL

• APPROACH TO ADVANCE REPORTING

• LIVE EXAMPLE

• Q&A

What is JXL?

• JXL is powerful JAR for reading and writing Excel files.

• JXL was the first API to perform read and write Excel files.

• It is a very small and simple API, very easy to learn and can be used in many productive ways.

• Copying a Chart and image insertion in excel is possible.

Why JXL

• JXL Supports font, number and date formatting.

• JXL Supports shading and coloring of cells.

• It Preserves macros on copy & Supports image creation.

• Customizable reports depends on the user's technical ability.

• JXL is internationalized, enables processing in almost any locale, country, language, or character encoding.

Why JXL when we have POI?

• JXL and POI APIs have some same end functionality:

• Both are open source i.e. Free, No cost of using

• Cell styling: alignment, backgrounds (colors and patterns), borders (types and colors), font support (font names, colors, size, bold, italic, strikeout, underline)

• Data formatting: Numbers and Dates, Text wrapping within cells, Read/Write existing and new spreadsheets, Header/Footer support.

• After all these similarities there are some differences in both APIs and the strong difference is that by using the JXL API we can build of our own reports, and in actual reports are the only part which is the important thing from the user's point of view who doesn't know much about html reports and all.

Understanding Basic Code

READING EXCEL FILE

JExcelApi can read an Excel spreadsheet from a file stored on the local file-system or from some input stream.

The first step when reading a spreadsheet from a file or input stream is to create a Workbook.

Workbook wbook = Workbook.getWorkbook(new File("C:\\Users\\workspace\\JXL_webinar\\src\\Book1.xls"));

It will get the excel sheet you have created and stored on your system.

Sheet sheet = workbook.getSheet(0);

Once you have a sheet, you can then start accessing the cells. You can retrieve the cell's contents as a string by using the convenience method getContents().

Sheet.getCell(0, 0).getContents();

It will get the content from the excel file sheet 0 and 1st row 1st col.

Understanding Basic Code

WRITING EXCEL FILE

WritableWorkbook It is a class inherited from Object class used to write in the work sheets.

WritableWorkbook wwbCopy = Workbook.createWorkbook(new File("Book2.xls"), wbook);

This will create a new excel sheet same as “ Book1.xls” and name it as “ Book2.xls”.

WritableSheet shSheet = wwbCopy.getSheet(0);

Code is used to access the sheet 1 of "Book1" where test data resides.

Now when it comes to write in to excel in actual we are writing to the sheet so for that we need a writable sheet just like the WritableWorkbook,

WritableSheet wshTemp = wwbCopy.getSheet(strSheetName);

Understanding Basic Code

WRITING EXCEL FILE (CONTD.)

This creates Cells of different data types, i.e. Label (String), DateTime (Date), Boolean and Number (double):

Label labTemp = new Label(iColumnNumber, iRowNumber, strData);wshTemp.addCell(labTemp);

Once we have finished adding sheets and cells to the workbook, we call write() on the workbook, and then close the file. This final step generates the output file (Book2.xls in this case) which may be read by Excel. If we call close() without calling write() first, a completely empty file will be generated.

workbook.write(); workbook.close();

Now we are done with the writing part in the excel sheet.

Advanced Reporting

JFREECHART

• JfreeChart is open source library for Java. It allows user to create / generate graphs easily. What ever we do, we need to report in some or the other way.

• In Selenium, as we all know there is no reporting mechanism, we will depend on other frameworks like TestNG mainly for Reports.

• How can we generate reports without TestNG. Using JFreechart user can easily generate graphs with basic commands which JfreeChart provides.

• Before getting started, user should have JFreeChart jar files. You can download the lastest version of JfreeChart from internet.

• Consider a situation where we are developing an application and you need to show the data in the form of charts, where the data itself is populated dynamically. In such case, you can use JFreeChart to display the data in the form of charts using simple programming.

Application Level Architecture

The application level architecture explains where JFreeChart library sits inside a Java Application.

Few Classes for Reporting

ChartFactory Class

• ChartFactory is an abstract class under the org.jfree.chart package. It provides a collection of utility methods for generating standard charts.

• createPieChart(java.lang.String title, PieDataset dataset, boolean legend, boolean tooltips,

boolean urls) This method creates a pie chart with default settings. It returns JfreeChart type object.

ChartUtilities Class

• ChartUtilites class from the org.jfree.chart package provides a collection of utility methods of JFreeCharts including methods for converting charts into image file format such as PNG, JPEG and creating HTML image maps.

• saveChartAsPNG(java.io.File file, JfreeChart chart, int width, int height) This method

converts and saves a chart to the specified file in PNG format.

JXL Example

Lets take an example using an application, here we are trying to register and login to the application, with different set of valid and invalid data, depending on the data some of the test-case may passes and some may fails.

Now we create a pie-chart with pass/fail data using following code:

DefaultPieDataset dataset = new DefaultPieDataset();

dataset.setValue("PASS", passPercent);dataset.setValue("FAIL", failPercent);

JFreeChart chart = ChartFactory.createPieChart("Test Case Execution Login", // chart title dataset, // datatrue, // include legendtrue, false);

File pieChart = new File("Login.png");

JXL Example

We are done with pie-chart. Now we will save the pie-chart as image and then that image can be used in the reporting part:

ChartUtilities.saveChartAsJPEG(pieChart, chart, width, height);

This will save the pie-chart as the JPEG image.

WritableImage image = new WritableImage(7, 9, // column, row7, 11, // width, height in terms of

number of cellsnew File("Login.png"));

shSheet.addImage(image);

This will add the image to the excel sheet.

Reporting

Reporting

Thank You!

We love to connect with you.

Twitter (@OSSCubeIndia) Facebook (www.facebook.com/osscubeindia) LinkedIn (www.linkedin.com/company/osscube) Google+ (plus.google.com/u/0/+OSSCubeIndia/posts )