Showing posts with label Selenium Webdriver. Show all posts
Showing posts with label Selenium Webdriver. Show all posts

Monday, June 8, 2015

Read and Write Excel file by using Apache POI in Java

Perform following steps first for correct configuration:
  1. Download Apache POI from here
  2. Add Apache POI .jar files in your Java project as External JARs from Build path
  3. For .xls type Excel file use HSSF and for .xlsx type Excel file use XSSF

Read Excel File:


Reading a file using Apache POI is very simple and involve following steps:
  1. Create a file with specific location to find it
  2. Create an Input stream with that file
  3. Create POI Work book(HSSF/ XSSF) with that Input stream
  4. Create POI Work sheet(HSSF/ XSSF) with that Work book
  5. Get number of rows in the file
  6. Get number of columns in the file
  7. Create a 2 dimension array to store file data
  8. Traverse the array and do followings,
    1. Create HSSF/ XSSF row for each row in the file
    2. Create HSSF/ XSSF cell for each cell in the file
    3. Store the cell value in a String variable
    4. Store the String in respective array cell
Lets have a look at the code doing these steps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class SimpleFileReadWrite {
 
     public static void main(String[] args) throws IOException
     { 
         File excel =  new File ("D:\\Credential.xlsx");
         FileInputStream fis = new FileInputStream(excel);
         XSSFWorkbook wb = new XSSFWorkbook(fis);
         XSSFSheet ws = wb.getSheet("creden");

         int rowNum = ws.getLastRowNum() + 1;
         int colNum = ws.getRow(0).getLastCellNum();
         String [][] data = new String [rowNum] [colNum];

         for(int i = 0; i <rowNum; i++){
             XSSFRow row = ws.getRow(i);
                 for (int j = 0; j < colNum; j++){
                     XSSFCell cell = row.getCell(j);
                     String value = cell.toString();
                     data[i][j] = value;
                     System.out.println ("the value is " + value);
                 }
         }
}

Write Excel File:


Writing a file using Apache POI is very simple and involve following steps:
  1. Create a file with specific location to create it
  2. Create an Output stream with that file
  3. Create POI Work book(HSSF/ XSSF) with that Input stream
  4. Create POI Work sheet(HSSF/ XSSF) with that Work book with specific name
  5. Store String values in a 2 dimension array to write in file
  6. Traverse the array and do followings,
    1. Create HSSF/ XSSF row for each row in the file
    2. Create HSSF/ XSSF cell for each cell in the file
    3. Set cell value with respective array cell
Lets have a look at the code doing these steps(here I am using the same code I used for reading file. So this program read from a .xlsx file, store data in an 2 dimension array and write it in another .xlsx file):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class SimpleFileReadWrite {
 
     public static void main(String[] args) throws IOException
     {     
         File excel =  new File ("D:\\Credential.xlsx");
         FileInputStream fis = new FileInputStream(excel);
         XSSFWorkbook wb = new XSSFWorkbook(fis);
         XSSFSheet ws = wb.getSheet("creden");

         int rowNum = ws.getLastRowNum() + 1;
         int colNum = ws.getRow(0).getLastCellNum();
         String [][] data = new String [rowNum] [colNum];

         for(int i = 0; i <rowNum; i++){
             XSSFRow row = ws.getRow(i);
                 for (int j = 0; j < colNum; j++){
                     XSSFCell cell = row.getCell(j);
                     String value = cell.toString();
                     data[i][j] = value;
                     System.out.println ("the value is " + value);
                 }
         }

         File outexcel = new File ("D:\\Output_excel.xlsx");
         FileOutputStream fos = new FileOutputStream(outexcel);
         XSSFWorkbook owb = new XSSFWorkbook();
         XSSFSheet sht = owb.createSheet("New sheet");

         for(int i = 0; i <rowNum; i++){
             XSSFRow orow = sht.createRow(i);
                 for (int j = 0; j < colNum; j++){
                     XSSFCell ocell = orow.createCell(j);
                     ocell.setCellValue(data[i][j]);
                     System.out.println ("the value is " + data[i][j]);
                 }
         }
         
         owb.write(fos);
   fos.flush();
   fos.close();
          
     }
 }

Sunday, September 8, 2013

Selenium simplified tutorial

If you are interested in Selenium Webdriver. Looking for tutorial with good and trusted contents than I have a very special solution for you.

Alan Richardson a very well known tester wrote down a magnificent book called Selenium Simplified. The writer provide a very useful and free course in Udemy and paid course for a more details on Java. Check following links,


I hope this guideline is helpful for you, feel free to post your comment.

Monday, January 30, 2012

Running Selenium Webdriver 2.0 in Opera

Following are the steps I used to run selenium webdriver in Opera
(Windows, Mozilla Firefox, Opera, Eclipse, Java)

Selenium IDE with Mozilla Firefox
- Open your Mozilla Firefox browser and Selenium IDE add on
- Record a simple and short test case for your testing application through Firefox and IDE
- Replay it ensure that cases runs alright as you recorded
- Export Test Case as JUnit4(Webdriver) Script from IDE

Selenium Webdriver/ Selenium 2.0
- Run Eclipse
- Create an new project
- Create a Class for the project without Main()
- Add the downloaded webdriver .jar files to your project in Java Build path-> Library
- Download and add another additional jar for Opera, selenium-server-standalone-2.18.0.jar
- Paste the JUnit4 code exported from IDE
- Hit the play button and enjoy ;)

Opera Configaration
- Opera latest versions
- Replace the code in exported JUnit code
-" driver = new FirefoxDriver(); " replace it with " driver = new OperaDriver(); "
- import com.opera.core.systems.OperaDriver;

Sunday, January 29, 2012

Running Selenium Webdriver 2.0 in IE

Following are the steps I used to run selenium webdriver in IE
(Windows, Mozilla Firefox, IE, Eclipse, Java)

Selenium IDE with Mozilla Firefox
- Open your Mozilla Firefox browser and Selenium IDE add on
- Record a simple and short test case for your testing application through Firefox and IDE
- Replay it ensure that cases runs alright as you recorded
- Export Test Case as JUnit4(Webdriver) Script from IDE

Selenium Webdriver/ Selenium 2.0
- Run Eclipse
- Create an new project
- Create a Class for the project without Main()
- Add the downloaded webdriver .jar files to your project in Java Build path-> Library
- Paste the JUnit4 code exported from IDE
- Hit the play button and enjoy ;)

IE Configaration
- IE 6 or Higher versions
- Go to IE Settings -> Internet options -> Security tab -> Enable protected mode for all type of connections
- Replace the code in exported JUnit code
-" driver = new FirefoxDriver(); " replace it with " driver = new InternetExplorerDriver(); "
- import org.openqa.selenium.ie.InternetExplorerDriver;

Wednesday, January 25, 2012

Running test scripts in Selenium 2.0

Following are the steps I used to run selenium webdriver
(Windows, Mozilla Firefox, Eclipse, Java)

Selenium IDE
- Open your Mozilla Firefox browser and Selenium IDE add on
- Record a simple and short test case for your testing application through Firefox and IDE
- Replay it ensure that cases runs alright as you recorded
- Export Test Case as JUnit4(Webdriver) Script from IDE

Selenium Webdriver/ Selenium 2.0
- Run Eclipse
- Create an new project
- Create a Class for the project without Main()
- Add the downloaded webdriver .jar files to your project in Java Build path-> Library
- Paste the JUnit4 code exported from IDE
- Hit the play button and enjoy ;)

Selenium cont.

I am listing my steps I used to configure selenium in my windows machine, in two different parts

Selenium IDE
- Add it to your Mozilla Firefox browser

Selenium Webdriver
- Download and install Java Runtime Enviroment
- Download and install Frame work you gonna use, I am using Eclipse Classic
- Download all the listed .jar files for webdriver

Move to my next post to running automation in webdriver :)

Lets start automation

So many automation application is available today but 3 most popular is Selenium, Watir and Sahi. Lets start with Selenium as it is most popular, have a very strong community for support and many many tutorial is available over the web to start. Selenium many different parts for different type of functionality. I am gonna use Selenium IDE, Selenium RC and Selenium Webdriver or Selenium 2.0 and others I am ignoring at the moment.

Selenium IDE
- An add on for Mozilla Firefox
- Used for recording activity in browser in terms of web applications
- Can export automation script of variety range of programming language to use in RC/ Webdriver/ Selenium 2.0

Selenium RC/ Selenium Webdriver / Selenium 2.0 (Java)
- Selenim RC or Webdriver or 2.0 is actually is the same application
- RC is the older version with have limited functionality and Webdriver/ Selenium 2.0 is the latest with more functionality in terms of automation scripting
- Support programming languages, I am focusing only in Java
- Exported script of IDE can be Import here

So, lets roll on my next post ;)