Posts

Generalized Apache POI script for reading and writing to .xlsx files

In this post, we will be looking to simplify the script mentioned in this post . Below script is generalized version so it can be used by other classes for reading and writing to .xlsx files. The code for reading and writing to .xlsx is made modular by putting them in different class and making read and write methods static. Any test that needs to read from excel file can just call the readFromXLSX() method and similarly any test that needs to write data to excel file, can just call the writeTestResultToXLSX() method and passing the appropriate parameters to it. Here's the class that contains the read and write methods: Below code makes use of Enumerations to restrict the value of test result to be either PASS or FAIL. This would make TestNG tests that will use these methods, not to write illegal values into the excel sheet.  import java.io.File ; import java.io.FileInputStream ; import java.io.FileNotFoundException ; import java.io.FileOutputStream ; import java.

Reading and writting data from .xlsx spreadsheet using Apache POI API

Image
Why to use Apache POI and not Jexcel API ? Below are the drawbacks of using Jexcel API for interacting with excel : Jexcel supports .xls format of MS excel, which is not the default option we get while saving excel sheets, nowadays. So most of the excel sheets are by default get saved in .xlsx format. Apache POI supports not only .xls but also .xlsx type of spreadsheets. We normally come across scenario such as - we want to read test data from the excel sheet and then write back to same excel sheet with some other related information. This can be achieved using Apache POI and not by Jexcel API as Jexcel does not support modifying a spreadsheet that is being used for reading. Jexcel API is no longer in development i.e. no future updates/enhancements are not possible but Apache POI is still in development and updates/enhancements are very much possible. So, in the long run, it'd be beneficial to use Apache POI in code rather than Jexcel API. Let's see a simple script t

How to Run Windows Commands in Java and Store Their Output in Log File

In our day to day work we need to run windows commands and verify their output. In order to automate these frequently used commands, we normally write them in batch file but this option does not produce log like file and does not have good file formatting features.To solve this problem, below given utility program can serve just the right purpose. Here we make use of java.io class to write output file and make use of java's Runtime class methods to execute windows commands. Below is the code, that will run windows commands and store their output in a .txt file with formatting and date of run of commands. Complete code is available at my GitHub Project to change it to suite your needs. import java.io.BufferedReader ; import java.io.BufferedWriter ; import java.io.File ; import java.io.FileWriter ; import java.io.IOException ; import java.io.InputStreamReader ; import java.util.Arrays ; import java.util.Date ; public class RunningWinCmd { public st

Copy contents of a file to another using Java

Here’s a small utility to copy contents of one file to another using java programming. We make use of java.io package present in JDK. This class provides rich set of I/O streams for reading and writing to files.  For copying contents of one file to another, we will make use of FileReader and FileWriter classes of java.io package. Since methods provided by these classes are low-level i.e they work on single characters or stream of characters, these classes need to wrapped with higher-level classes such as BufferedReader for FileReader and BufferedWriter for FileWriter. These high-level classes are efficient as they make use of buffer and work on large chuck of data at a time. Below is the code, that will copy contents of one file to another. Userinput.txt is the source file and useroutput.txt is destination file. Complete code is available at my GitHub Project. import java.io.BufferedReader ; import java.io.BufferedWriter ; import java.io.File ; import java.io.F

Running Selenium tests on Internet Explorer Browser

The main advantage of Selenium testing is the cross browser testing. So test once written for a browser, can be run on other supported browser as well (off course with little changes :-) ). Let's see how we can run selenium java test on Internet Explorer browser. For this we need - IEDriverServer.exe (it can be downloaded from here ): This is Internet Explorer driver server which is needed by Selenium to execute tests on Internet Explorer browser. We should first follow these steps to make sure that IE is setup correctly so that it can be handled by Selenium properly.  In our script, we will be explicitly mentioning which IEDriverServer.exe to use to run tests. Below is a simple test we can run to understand what it takes to run a test on Internet Explorer. Basic steps for creating script: Create a TestNG class in eclipse While creating TestNG class, select these annotations - @BeforeMethod, @AfterMethod Code for the script would as given below – Click on code to u

Simple Data Driven Framework script

Image
Why use Data driven Framework? We go for data driven framework, to test application behavior for different sets of data. So in this, script remains the same only the data on which the script will work changes as manual testing scenarios. One basic example would be to test login functionality. Typical test case for login functionality have the test cases wherein only data changes are present and basic steps remain the same.  Example basic login test cases: Verify that for valid credentials, user is granted access to system. Verify that for invalid credentials, user is denied access to system. Verify that after successful login user is presented with correct landing page. Basic steps in all above test cases would as given below: Navigate to Application Under Test URL Enter user name Enter password Click on Login/Submit button Verify result When above scenario is to be automated then the best solution is to implement it using Data Driven Framework. Simp