0

i am on Beginning stage in SELENIUM. Now, i am trying functional testing on selenium web-driver. As driving EXCEL data is very essential on functional testing, i am struggling a lot in this are.

How to use Excel data?

2
  • 1
    Selenium is a library in your programming language of choice. That language has also libraries to read Excel files. Rest is "matter of simple programming". I suspect you need to improve your programming skills, there is nothing specific to use Excel input with Selenium as compared to other input sources (beyond using Excel libraries). – Peter M. - stands for Monica Mar 22 '16 at 14:40
  • 1
    Possible duplicate of Efficient way of reading data from Excel in Selenium C# – ECiurleo Mar 23 '16 at 10:50
0

What you are looking for is Data Driven Test or for short is DDT. You can use Excel as external source of data for you tests.

I assume you are using Python's unittest framework to write test cases.

First you will need to install ddt library by running:

pip install ddt

To read values from spreadsheet you will need to install another library by running:

pip install xlrd

Here is how you can import data from your sheet and use in your testcases:

   import xlrd, unittest
   from ddt import ddt, data, unpack
   from selenium import webdriver
   def get_data(file_name):
       # create an empty list to store rows
       rows = []
       # open the specified Excel spreadsheet as workbook
       book = xlrd.open_workbook(file_name)
       # get the first sheet
       sheet = book.sheet_by_index(0)
       # iterate through the sheet and get data from rows in list
       for row_idx in range(1, sheet.nrows):
           rows.append(list(sheet.row_values(row_idx, 0, sheet.ncols)))
       return rows
    @ddt
    class [Your Class Name](unittest.TestCase):
        def setUp(self):
        [Insert your setUp code here]


    # get the data from specified Excel spreadsheet 
    # by calling the get_data function 
    @data(*get_data("TestData.xlsx"))
    @unpack
    def test_[your_test_name](self, [your_argument_1], [your_argument_2]): 
           [your test method]  


    def tearDown(self):
        # close the browser window
        self.driver.quit()
if __name__ == '__main__':
    unittest.main()

The @unpack decorator is used to unpack spreadsheet values into multiple arguments. The test_[your_test_name] method accepts your value arguments([your_argument_1]...), which will be mapped to the spreadsheet values by ddt.

2
  • And how to do that? – IAmMilinPatel Mar 23 '16 at 1:27
  • Yes Of-course!!! i am using Selenium mars, where i can see some tables that are used for retrieving excel data. how to use that? – Paarthiban NK Mar 23 '16 at 3:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.