2

How to access excel files from selenium webdriver using Java ?

I am new to selenium Webdriver, I have a scenario where I have to provide different sets of values to the different test case.

Let, us say in a column there are 100 values first 20 set of values I have to feed to the Testcase1, and next set of 20 values to Testcase 2 and so on till Test case 5.

Michael Durrant
25.3k3 gold badges42 silver badges114 bronze badges
asked Apr 27, 2017 at 9:56
1
  • What framework are you using with WebDriver? Some will allow you to define data sources, where others you'll have to manually code it. Commented Apr 27, 2017 at 11:46

4 Answers 4

1

There are lots of thing you need to understand and implement. 1st: are you going to use excel with extenion .xls or .xlsx. If any of them then use the Apache POI jar to read data from excel.

Apache POI majorly have 2 components

  1. XSSF : which is for .xlsx extension file.
  2. HSSF : which is for .xls extension file.

Now Create a new function using both object(XSSF and HSSF). Function will be like :

public List< Map<String, String>> getExcelDataHSSF(int startRowNo, int EndRowNumber){}
public List< Map<String, String>> getExcelDataXSSF(int startRowNo, int EndRowNumber){}

This function will return your data in List of Map object. Where Map's key is columnName and Value is: your test data respective to column.

Now, You need to write a function which check the extension of excel file and execute the function

if extension is .xlsx then run function getExcelDataXSSF(1,20) else getExcelDataHSSF(1,20).

After that, Go to your Test file and use TestNG dataprovider to fetch data using above function and pass to you test cases.

Sagar007
1,3403 gold badges9 silver badges24 bronze badges
answered Jun 2, 2017 at 7:09
1

Selenium is meant to communicate with web applications. To pick data from excel using java you need to have apache poi jars in your project. If it is a maven project, then the correct artifactId in your pom.Once after the jars are successfully installed, you need to use the classes of the downloaded jars and play with the excel.

answered May 21, 2018 at 14:25
0

You need to interface with excel programmatically and pull in the data so you can then assign variables to pass to your tests.

I'm not sure on your language use, but here is the rest API for excel to get you started.

helpful links:

answered Apr 27, 2017 at 11:44
1
  • I am using TestNG Framework with Java Commented Apr 27, 2017 at 11:48
0

You will have to make a data driven framework in which u will pick up data for each test case from excel and store it in HashMaps. Then u can loop over the data in HashMap using for Loop and pick specified values against each Key

answered May 1, 2017 at 11:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.