QC 数据提取器(使用 C#)
使用此应用程序,用户可以使用 C# 和 QC(质量中心)公开的 OTA API,将 QC(质量中心)的测试用例执行报告生成到具有精美饼图和表格格式的 HTML 网页中。
摘要
使用此应用程序,用户可以将 QC(质量中心)的测试用例执行报告生成到具有精美饼图和表格格式的 HTML 网页中,使用 C# 和 QC(质量中心)公开的 OTA API。
引言
在进入正题之前,我先澄清一下,互联网上有很多关于使用 VB.NET 生成测试用例执行报告的文章,但使用 C# 生成同样的报告的信息却很少。我开发这个应用程序使用了 C#.NET。在此应用程序中,您可以将 QC 的报告生成为 HTML 网页,该 HTML 页面将包含带有表格格式的已执行测试用例的饼图,此外,您还可以将此报告通过电子邮件发送给您的所有团队成员。除此之外,您还可以将结果保存回 QC 位置以供将来使用。
我提供了哪些新功能?
这个应用程序是一个完整的打包程序,只需运行一个 *.exe,结果就会出现在您的收件箱中。
使用此功能,您将能够:
- 生成测试用例报告。
- 根据报告绘制饼图。
- 将结果存储在 HTML 网页中。
- 使用任何电子邮件服务提供商(如 Outlook 等)通过电子邮件发送此报告。
- 将结果保存到本地硬盘或返回 QC 位置。
- 成为一位准备好报告的清晨管理者。
请参考下图以获得最佳解释。
背景
由于我从事 C# 自动化工作,并且不擅长 VB 脚本和 QTP,因此我想为此目的使用 C#。互联网上关于 C#.NET 的 QC OTA 帮助信息并不多,而且在大公司工作,高层更关心数据和漂亮的报告,而不太在意口头交流,他们更相信报告。所以我认为吸引 C# 自动化工程师也来关注 QC(质量中心)API 报告,最终有一天,网络上将会有足够的信息。
最后但同样重要的一点是,这是我实现这一目标的小小努力。
什么是 OTA API?
您可以在网上找到大量关于此的信息。只需谷歌或必应一下,您就能获得足够的信息。但我仍然想为您提供一些基本信息。
- Open Test Architecture (OTA) API 是一个 COM 库,使您能够:
- 将外部应用程序与质量中心集成。
- 在不使用 GUI 前端的情况下与质量中心应用程序进行交互。
- 绕过 DBA 与 QC 数据库进行交互。
- API 函数可通过 COM 兼容的编程语言访问,例如 Visual Basic、VBScript、C++、C# 等。
- API 有一个入口点 -
TDConnection
对象。
实现
我使用 C# 编程语言来实现我的目标。为了与 QC 通信,我包含了 mercury labs 提供的 TDAPIOLELib DLL。我们使用 Visual Studio IDE 中的控制台应用程序来开发此应用程序,以便可以轻松地将其安排到 Windows 任务计划程序中,以便定期运行。为了存储所有设置和输入值,我使用了 app.config 文件来存储配置值。总的来说,我采用了非常简单直接的方法来完成我的任务,即使是 C# 新手也能理解。
HTML 报告的外观如下。
Using the Code
第一部分:App.config
在深入研究代码之前,我想先解释一下我用来存储所有输入设置的 App.config 文件。这样您就可以对先决条件有所了解。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- QC server location to access the QC website-->
<add key="QCserverURL" value="QCServerURL"/>
<!-- QC domain name -->
<add key="QCdomainName" value="Your Domain Name"/>
<!-- the name of the project for which you want to
retrieve the test case execution report-->
<add key="QCprojectName" value="Your project Name"/>
<!-- user name through which you have to login in to QC-->
<add key="username"
value="<a href="mailto:jawed.ace@gmail.com%22/%3E">jawed.ace@gmail.com</a><!--"/>
Password to login in to QC-->
<add key="pwd" value="myPassword"/>
<!-- Folder names provided in to QC from which you want to generate report.
Multiple location should be separated by semicolon(;)-->
<add key="QCFolderLocationForTestCase"
value="Root\Integration Team\Anna-GR-4\ePrint;Root\Integration Team\Anna-GR-4\ SIPs"/>
<!-- the title of the pie chart, will be appear before putting the graph-->
<add key="ChartTitleForTestCasesReport" value=" eprint ; Sips"/>
<!-- location where you want to store all the pie chart generate by code-->
<add key="locationToSavePieChartImages" value="C:\QCReportImages\"/>
<!-- Test case status names-->
<add key="TCStatusNames" value="Not Completed;No Run;
Failed;Passed;Accepted Failure;Blocked;N/A;Repair;Unsupported"/>
<!-- location to store the HTML file or report generated-->
<add key="htmlWebPageLocation" value="C:\\QcReportIntegrationTeam.html"/>
</appSettings>
</configuration>
第二部分:主程序
这是我们将要提出的主要思路,以便按照我们的要求进行工作,或者我们希望如何处理代码以使其为我们工作。
声明命名空间
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.Drawing;
using System.Drawing.Drawing2D;
using TDAPIOLELib;
要添加 TDAPIOLELib
命名空间,请按照以下步骤操作:
- 转到您的解决方案资源管理器。
- 点击“引用”。
- 选择“添加引用”。
- 打开窗口后,点击“浏览”选项卡。
- 转到路径 C:\Program Files\Common Files\Mercury Interactive\Quality Center。
- 选择 OTAClient.dll 并单击“确定”按钮。如果您没有 OTAClient.dll,则需要从网络上下载。
请参考下图了解详情。
图 4. 添加 OTAClient.dll - 转到您的主程序,按照下图所示添加命名空间。图 5. 添加 TDAPIOLELib 命名空间。
现在,您已准备好使用 OTA API 提供的类和函数。我们将在此之后开始编程。
我们将把 App.config 文件中提供的所有设置读取到本地字符串中,以便在程序中随处使用。
//Read all the inputs value from App.config file and store //into variable for further use. //variable to store UserName to login in to QC string username = ConfigurationSettings.AppSettings["username"].ToString(); //Variable to store Password string pwd = ConfigurationSettings.AppSettings["pwd"].ToString(); //Names of the test Cases similar to QC string strTestCaseStatus = ConfigurationSettings.AppSettings["TCStatusNames"].ToString(); //Folder Name for test cases uploaded in to QC(Root\\Integration Team\\Anna-GR-3) string testSetFolderPath = ConfigurationSettings.AppSettings["QCFolderLocationForTestCase"].ToString(); //Title of the Pie Chart string chartTitleTestCasesReport = ConfigurationSettings.AppSettings["ChartTitleForTestCasesReport"].ToString(); //Location on to local derive where we want to store Pie chart string strLocationToSavePieChartImages = ConfigurationSettings.AppSettings["locationToSavePieChartImages"].ToString(); //Server Location of QC string strServerURL = ConfigurationSettings.AppSettings["QCserverURL"].ToString(); //Domain name for QC access string strDomainName = ConfigurationSettings.AppSettings["QCdomainName"].ToString(); //Project name to access from QC string strProjectName = ConfigurationSettings.AppSettings["QCprojectName"].ToString(); //Local derive location to store HTML web page for result string strHtmlWebpageLocation = ConfigurationSettings.AppSettings["htmlWebPageLocation"].ToString();
现在我们已经准备好了所有输入,可以开始编程了。
- 首先登录 QC。
//Declare an object of TDCoonection TDConnection qctd = new TDConnection(); //Pass the QC server URL qctd.InitConnectionEx(strServerURL); //Connect with the QC qctd.ConnectProjectEx(strDomainName, strProjectName, username, pwd);
- 转到您需要读取测试用例的测试实验室文件夹。
- 将所有测试用例及其相应的状态收集到数据集中。
- 使用数据视图统计通过、失败等各种测试状态的数量。
- 现在我们将调用饼图函数来绘制各种测试状态的饼图。我们将把各种参数传递给饼图方法来绘制我们的饼图,例如:测试状态计数、测试状态名称、图表标题。
要获取绘制饼图的代码,请下载代码并仔细查看。您也可以为此目的使用自己的代码。网络上有很多绘制饼图的代码。
- 饼图方法将返回位图图像。我们将图像保存到本地位置以供 HTML 网页报告使用,否则您可以直接在 HTML 页面中使用它,但出于记录目的,我将其保存在本地位置。图 7. 饼图示例
//Call a method to create pie chart Bitmap btMapImages = GeneratePieChart.pieChart(TCStatus, arrValueNames, chartTitle); //Save that pie chart a particular location btMapImages.Save(strLocationToSavePieChartImages + strImageNames, System.Drawing.Imaging.ImageFormat.Png);
- 之后,我们将调用一个方法将所有数据以表格格式放入 HTML 网页,如下图所示。图 8. HTML 网页格式
有关代码,请查看上传到此网站的代码。我已在每一行中添加了注释,以便您可以更好地、以更 nice 的方式理解。
- 调用您的方法,通过 Outlook 或任何邮件客户端将报告作为附件或嵌入式邮件发送出去。
- 准备好的报告将出现在您的邮箱中。
- 您可以将此 *.exe 添加到您的 Windows
计划任务中,以便每天或每周运行一次,并将报告发送给您的团队成员或您想要的任何人。这难道不棒吗!!!
- 因此,作为自动化工程师,我们的责任是使手动流程更轻松、更自动化,以便我们可以节省时间、精力,当然还有金钱。
类图
代码解释如下:
if (qctd.Connected)
{
//Decaler test set factory
TestSetFactory testSetFactory = (TestSetFactory)qctd.TestSetFactory;
//Define filter
TDFilter filter = (TDFilter)testSetFactory.Filter;
//Go through each test set folder
string[] testCasesFolderNames = testSetFolderPath.Split(';');
//get the chart title names
string[] arrChartTitleTestCasesReport = chartTitleTestCasesReport.Split(';');
int chartTitleCount = 0;
foreach (string testFoldername in testCasesFolderNames)
{
if (testFoldername!=null)
{
filter["CY_FOLDER_ID"] = "\"" +
testFoldername + "\"";
//get all the list of test cases
List testSets = (List)testSetFactory.NewList(filter.Text);
//make sure that the list of test cases is not null
if (testSets != null)
{
//Read all the testcases status from appconfig files.
string[] arrValueNames = strTestCaseStatus.Split(';');
//Define dataset to hold all the values for testcases execution
DataSet dsTC = new DataSet();
dsTC.Tables.Add("TestCaseExecutionReport");
dsTC.Tables[0].Columns.Add("TestName");
dsTC.Tables[0].Columns.Add("TestStatus");
//Read the Test cases status one by one from
//the folder location provided by the user.
foreach (TDAPIOLELib.TestSet tst in testSets)
{
string nameOfTestSuit = tst.Name;
TSTestFactory tstf = (TSTestFactory)tst.TSTestFactory;
List ftest = (List)tstf.NewList(" ");
//integer value to hold the total status count
int[] TCStatus = new int[arrValueNames.Length];
try
{
//trace trough each test cases to get the status
foreach (TDAPIOLELib.TSTest test in ftest)
{
//store that value in to dataset
dsTC.Tables[0].Rows.Add
(test.Name, test.Status);
}
//Get the total row
int totalRow = dsTC.Tables[0].Rows.Count;
//Initialize the value to short the dataset
//to get the status count
int TSrowValue = 0;
//read the dataset to get the total status count
foreach (string TCfilterValue in arrValueNames)
{
if (TCfilterValue != null)
{
DataView dvDataTC = new DataView(dsTC.Tables[0]);
dvDataTC.RowFilter = "TestStatus like '" +
TCfilterValue + "'";
TCStatus[TSrowValue] = dvDataTC.Count;
TSrowValue++;
}
// TCStatus[TCStatus.Length] = totalRow;
}
//local variable to use for Pie chart
//images name
string strImageNames = nameOfTestSuit + "_" +
DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
//Gee the string for chart title
string chartTitle=string.Empty;
if(arrChartTitleTestCasesReport.Length==1)
{
chartTitle = arrChartTitleTestCasesReport[0] +
" " + nameOfTestSuit;
}
else
{
chartTitle = arrChartTitleTestCasesReport
[chartTitleCount] + " " + nameOfTestSuit; ;
}
//Call a method to create pie chart
Bitmap btMapImages = GeneratePieChart.pieChart
(TCStatus, arrValueNames, chartTitle);//Save that pie chart
// in a particular location
btMapImages.Save(strLocationToSavePieChartImages +
strImageNames, System.Drawing.Imaging.ImageFormat.Png);
//call a method to create html webpage
//and put all the report over there
CreateHtmlPage.saveHtmLPage(TCStatus, nameOfTestSuit,
strLocationToSavePieChartImages + strImageNames,
arrValueNames, totalRow, strHtmlWebpageLocation);
//Clear the data set so that all the store rows value
//will get deleted.
dsTC.Clear();
} //end of try block
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} //end of catch block
}
} //end of checking List for test cases
}//end of checking for multiple folder
//increase the count
chartTitleCount += 1;
}//end of for each loop
}//end of checking Connection
您可以访问我的博客以获取更多想法并查看正在进行的工作。
祝您 C# 自动化愉快.....
欢迎提供您的评论和建议。谢谢!!