Our strategy for using Coded UI to test our software is to create several Excel spreadsheets each with the testing steps for a particular test case. There will be one Driver spreadsheet that has a worksheet that lists the test cases to be run. The idea is to read the Driver spreadsheet and then go open the individual test case spreadsheets to run the test steps. The test steps will list a button or field, the action to perform (click, edit) and the data value or other expected result which will be passed to helper functions in the UIMap or hand written map.
In my Coded UI project I have only one test method and it has one spreadsheet data binding associated with it. I can open that file and read it with no problems. How do I close that spreadsheet file and go open a different one at run time?
What other ways are there to do what I am trying to do?
Use
using Microsoft.Office.Interop.Excel;
To open an instance of Excel:
private Microsoft.Office.Interop.Excel.Application excel;
...
excel = new Microsoft.Office.Interop.Excel.Application();
To open a specific workbook:
private Workbook workbook;
....
workbook = excel.Workbooks.Open(@"\\C\Data\YourDriver.xlsx");
To get a specific worksheet:
Worksheet worksheet = workbook.Worksheets["YourTestCaseWorkSheet"];
To Kill your task:
/// <summary>
/// Quits the excel process
/// </summary>
public void Quit()
{
Marshal.ReleaseComObject(this.worksheet);
Marshal.ReleaseComObject(this.workbook);
this.excel.Quit();
this.excel = null;
}
Put the Opening on the Excel sheet in the [TestInitialize] method and the Quit method in the [TestCleanup]. You should try - catch all test cases though with the Quit method to insure your excel processes close.
Note that test methods cannot run other test methods. So you'll be required to use one test case to run your multiple Excel test cases.
You can also build a batch file to run your tests through MSTEST.EXE.