Data driven test automation with Excel
11168 단어 automation
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
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.
view plain
copy to clipboard
public class ExcelConnector
{
private string excelFile = null;
private Excel.Application excelObj = null;
private Excel.Workbook workBook = null;
private Excel.Worksheet worksheet = null;
private UInt16 currentRowIndex = 0;
private string[] inputs;
private string[] outputs;
public string ExcelFile
{
get
{
return this.excelFile;
}
}
public UInt16 CurrentRowIndex
{
get
{
return this.currentRowIndex;
}
}
public ExcelConnector(string excelFile, string[] inputs, string[] outputs, bool load, UInt16 startRow)
{
this.excelFile = excelFile;
this.inputs = inputs;
this.outputs = outputs;
if (load)
this.LoadFile();
currentRowIndex = startRow;
}
public void LoadFile()
{
excelObj = new Excel.Application();
System.Threading.Thread.CurrentThread.CurrentCulture = new
System.Globalization.CultureInfo(”en-US”);
workBook = excelObj.Workbooks.Open(this.excelFile, 0, true, 5, “”, “”, true,
Excel.XlPlatform.xlWindows, “\t”, false, false, 0, true, false, false);
Excel.Sheets sheets = workBook.Worksheets;
worksheet = (Excel.Worksheet)sheets.get_Item(1);
}
public TestData GetNext()
{
string[] arrInputs = new string[inputs.Length];
string[] arrOutputs = new string[outputs.Length];
for (int i = 1; i < this.inputs.Length+1; i++)
{
Excel.Range cell = (Excel.Range)worksheet.Cells[currentRowIndex, i];
if ( ((string)cell.Text).Length == 0 )
return null;
arrInputs[i - 1] = (string)cell.Text;
}
for (int i = 0; i < this.outputs.Length ; i++)
{
Excel.Range cell = (Excel.Range)worksheet.Cells[currentRowIndex, i + inputs.Length + 1];
if ( ((string)cell.Text).Length == 0 )
return null;
arrOutputs[i] = (string)cell.Text;
}
Excel.Range cellComment = (Excel.Range)worksheet.Cells[currentRowIndex,
inputs.Length+outputs.Length+1];
currentRowIndex++;
return new TestData(arrInputs, arrOutputs, (string)cellComment.Text);
}
public void Dispose()
{
excelObj.Quit();
}
}
public class ExcelConnector
{
private string excelFile = null;
private Excel.Application excelObj = null;
private Excel.Workbook workBook = null;
private Excel.Worksheet worksheet = null;
private UInt16 currentRowIndex = 0;
private string[] inputs;
private string[] outputs;
public string ExcelFile
{
get
{
return this.excelFile;
}
}
public UInt16 CurrentRowIndex
{
get
{
return this.currentRowIndex;
}
}
public ExcelConnector(string excelFile, string[] inputs, string[] outputs, bool load, UInt16 startRow)
{
this.excelFile = excelFile;
this.inputs = inputs;
this.outputs = outputs;
if (load)
this.LoadFile();
currentRowIndex = startRow;
}
public void LoadFile()
{
excelObj = new Excel.Application();
System.Threading.Thread.CurrentThread.CurrentCulture = new
System.Globalization.CultureInfo(”en-US”);
workBook = excelObj.Workbooks.Open(this.excelFile, 0, true, 5, “”, “”, true,
Excel.XlPlatform.xlWindows, “\t”, false, false, 0, true, false, false);
Excel.Sheets sheets = workBook.Worksheets;
worksheet = (Excel.Worksheet)sheets.get_Item(1);
}
public TestData GetNext()
{
string[] arrInputs = new string[inputs.Length];
string[] arrOutputs = new string[outputs.Length];
for (int i = 1; i < this.inputs.Length+1; i++)
{
Excel.Range cell = (Excel.Range)worksheet.Cells[currentRowIndex, i];
if ( ((string)cell.Text).Length == 0 )
return null;
arrInputs[i - 1] = (string)cell.Text;
}
for (int i = 0; i < this.outputs.Length ; i++)
{
Excel.Range cell = (Excel.Range)worksheet.Cells[currentRowIndex, i + inputs.Length + 1];
if ( ((string)cell.Text).Length == 0 )
return null;
arrOutputs[i] = (string)cell.Text;
}
Excel.Range cellComment = (Excel.Range)worksheet.Cells[currentRowIndex,
inputs.Length+outputs.Length+1];
currentRowIndex++;
return new TestData(arrInputs, arrOutputs, (string)cellComment.Text);
}
public void Dispose()
{
excelObj.Quit();
}
}
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.
view plain
copy to clipboard
TestData testData;
openFileDialog.DefaultExt = "xlsx";
openFileDialog.Filter = "XLS file (*.xls)|*.xls|XLSX file (*.xlsx)|*.xlsx|All files (*.*)|*.*";
if ( openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK )
{
this.tbExcelFile.Text = openFileDialog.FileName;
}
void InitExcelConnection()
{
string[] testDataInputs = new string[] { “Input1″, “Input2″, “Input3″, “Input4″ };
string[] testDataOutputs = new string[] { “Output1″ };
if (System.IO.File.Exists(tbExcelFile.Text))
excelConnector = new ExcelConnector(this.tbExcelFile.Text,
testDataInputs, testDataOutputs, true,2);
else
throw new RanorexException(null,”Specified file does not exist!”);
}
InitExcelConnection();
while ((testData = excelConnector.GetNext()) != null )
{
Ranorex.Control bt = null;
foreach (string str in testData.Inputs)
{
foreach (char c in str)
{
bt = calcForm.FindChildText(c.ToString());
Mouse.ClickControl(bt);
}
}
bt = calcForm.FindControlId(403);
// Compare calculator output with expected value
Ranorex.Validate.HasText(bt, testData.Outputs[0] + “, “,testData.Description + ” validation”,false);
}
TestData testData;
openFileDialog.DefaultExt = "xlsx";
openFileDialog.Filter = "XLS file (*.xls)|*.xls|XLSX file (*.xlsx)|*.xlsx|All files (*.*)|*.*";
if ( openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK )
{
this.tbExcelFile.Text = openFileDialog.FileName;
}
void InitExcelConnection()
{
string[] testDataInputs = new string[] { “Input1″, “Input2″, “Input3″, “Input4″ };
string[] testDataOutputs = new string[] { “Output1″ };
if (System.IO.File.Exists(tbExcelFile.Text))
excelConnector = new ExcelConnector(this.tbExcelFile.Text,
testDataInputs, testDataOutputs, true,2);
else
throw new RanorexException(null,”Specified file does not exist!”);
}
InitExcelConnection();
while ((testData = excelConnector.GetNext()) != null )
{
Ranorex.Control bt = null;
foreach (string str in testData.Inputs)
{
foreach (char c in str)
{
bt = calcForm.FindChildText(c.ToString());
Mouse.ClickControl(bt);
}
}
bt = calcForm.FindControlId(403);
// Compare calculator output with expected value
Ranorex.Validate.HasText(bt, testData.Outputs[0] + “, “,testData.Description + ” validation”,false);
}
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.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Azure Monitor, Azure Automation (Runbooks)을 사용하여 App Service (PaaS)의 모니터링 알림 대상 (Webhook)을 Chatwork로 만들기PaaS는 Zabbix Agent와 같은 감시 에이전트를 설치할 수 없기 때문에 감시 힘들다. 그렇지만 IaaS로 DB라든가 만들면 감시는 에이전트로 할 수 있지만, 메인터넌스가 힘들다. 가능하면 PaaS 장애를 C...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.