Data driven test automation with Excel

Download Ranorex Studio Excel Sample (16KB)

Microsoft Excel is often used to store and manage test cases and test data. Why? Because Excel is a widespread tool. Nearly every Windows based machine has installed Microsoft Office.
Following example describes how to feed a Ranorex test automation process with test data stored within an Excel table. The example consists of 3 main parts:

  • Excel sheet containing test data
  • Ranorex automation project
  • Excel connector to read test data from Excel

Excel Sheet

As usual, we use the Windows calculator in this example of data driven testing. Each row defines a single test case with test data inputs and expected outputs. In addition, each test case has a short description.

ExcelConnector class

To provide a user-friendly interface for retrieving test data from an Excel sheet, we implement an ExcelConnector. This class wraps the functionality of a Microsoft COM library to access rows and cells in a data sheet. Thus we have to add the COM library as reference object to our project.

  1. public class ExcelConnector   
  2. {   
  3.     private string excelFile = null;   
  4.     private Excel.Application excelObj = null;   
  5.     private Excel.Workbook workBook = null;   
  6.     private Excel.Worksheet worksheet = null;   
  7.     private UInt16 currentRowIndex = 0;   
  8.     private string[] inputs;   
  9.     private string[] outputs;   
  10.   
  11.     public string ExcelFile   
  12.     {   
  13.         get  
  14.         {   
  15.             return this.excelFile;   
  16.         }   
  17.     }   
  18.     public UInt16 CurrentRowIndex   
  19.     {   
  20.         get  
  21.         {   
  22.             return this.currentRowIndex;   
  23.         }   
  24.     }          
  25.   
  26.     public ExcelConnector(string excelFile, string[] inputs, string[] outputs, bool load, UInt16 startRow)   
  27.     {   
  28.         this.excelFile = excelFile;   
  29.         this.inputs = inputs;   
  30.         this.outputs = outputs;   
  31.         if (load)   
  32.            this.LoadFile();   
  33.         currentRowIndex = startRow;   
  34.     }   
  35.   
  36.     public void LoadFile()   
  37.     {   
  38.         excelObj = new Excel.Application();   
  39.         System.Threading.Thread.CurrentThread.CurrentCulture = new  
  40.                                              System.Globalization.CultureInfo(”en-US”);   
  41.         workBook = excelObj.Workbooks.Open(this.excelFile, 0, true, 5, “”, “”, true,   
  42.                                              Excel.XlPlatform.xlWindows, “\t”, falsefalse, 0, truefalsefalse);   
  43.         Excel.Sheets sheets = workBook.Worksheets;   
  44.         worksheet = (Excel.Worksheet)sheets.get_Item(1);   
  45.     }   
  46.   
  47.     public TestData GetNext()   
  48.     {   
  49.         string[] arrInputs = new string[inputs.Length];   
  50.         string[] arrOutputs = new string[outputs.Length];   
  51.   
  52.         for (int i = 1; i < this.inputs.Length+1; i++)   
  53.         {   
  54.             Excel.Range cell = (Excel.Range)worksheet.Cells[currentRowIndex, i];   
  55.             if ( ((string)cell.Text).Length == 0 )   
  56.                 return null;   
  57.             arrInputs[i - 1] = (string)cell.Text;   
  58.         }   
  59.         for (int i = 0; i < this.outputs.Length ; i++)   
  60.         {   
  61.             Excel.Range cell = (Excel.Range)worksheet.Cells[currentRowIndex, i + inputs.Length + 1];   
  62.             if ( ((string)cell.Text).Length == 0 )   
  63.                 return null;   
  64.             arrOutputs[i] = (string)cell.Text;   
  65.         }   
  66.   
  67.         Excel.Range cellComment = (Excel.Range)worksheet.Cells[currentRowIndex,   
  68.                                                         inputs.Length+outputs.Length+1];   
  69.         currentRowIndex++;   
  70.         return new TestData(arrInputs, arrOutputs, (string)cellComment.Text);   
  71.     }   
  72.   
  73.     public void Dispose()   
  74.     {   
  75.         excelObj.Quit();   
  76.     }   
  77. }  

The constructor of the class ‘ExcelConnector’ specifies the name of the Excel file, the amount of in- and outputs defined by each test case and where to start reading test data. The ‘GetNext()’ method returns a simple TestData object representing a single row from the excel sheet.

Ranorex test automation code

The following code shows how to use the ‘ExcelConnector’ class to read test data from the excel sheet.

  1. TestData testData;   
  2.   
  3. openFileDialog.DefaultExt = "xlsx";   
  4.   
  5. openFileDialog.Filter = "XLS file (*.xls)|*.xls|XLSX file (*.xlsx)|*.xlsx|All files (*.*)|*.*";   
  6. if ( openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK )   
  7. {   
  8.     this.tbExcelFile.Text = openFileDialog.FileName;   
  9. }   
  10.   
  11. void InitExcelConnection()   
  12. {   
  13.     string[] testDataInputs = new string[] { “Input1″, “Input2″, “Input3″, “Input4″ };   
  14.     string[] testDataOutputs = new string[] { “Output1″ };   
  15.     if (System.IO.File.Exists(tbExcelFile.Text))   
  16.          excelConnector = new ExcelConnector(this.tbExcelFile.Text,   
  17.                                     testDataInputs, testDataOutputs, true,2);   
  18.     else  
  19.          throw new RanorexException(null,”Specified file does not exist!”);   
  20. }   
  21.   
  22. InitExcelConnection();   
  23.   
  24. while ((testData = excelConnector.GetNext()) != null )   
  25. {   
  26.     Ranorex.Control bt = null;   
  27.     foreach (string str in testData.Inputs)   
  28.     {   
  29.         foreach (char c in str)   
  30.         {   
  31.             bt = calcForm.FindChildText(c.ToString());   
  32.             Mouse.ClickControl(bt);   
  33.         }   
  34.     }   
  35.     bt = calcForm.FindControlId(403);   
  36.     // Compare calculator output with expected value   
  37.     Ranorex.Validate.HasText(bt, testData.Outputs[0] + “, “,testData.Description + ” validation”,false);   
  38. }  

An Excel connector could be a smart solution to reuse existing manual tests stored in excel sheets and to provide an easy to use test case data base, especially for those testers without deeper Ranorex test automation knowledge.

你可能感兴趣的:(automation)