我正在通过Coded UI(手动)编写数据驱动测试。我想从excel文件(Data.xlsx)获取数据。它将从xls表中获取数据并逐个放入Gridview。
例如:文件路径(控件类型:行)将从xls工作表(C:\Users\XXXXX\Desktop\malware\mal.txt)
获取数据。我正在使用这种方法,但无法做到。任何人都可以提供帮助。??
string strFilePath = "C:\\Eclipse\\Checklist.xls";
string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" + "Extended Properties=Excel 8.0;";
OleDbConnection cnCSV = new OleDbConnection(strConnectionString);
cnCSV.Open();
OleDbDataAdapter daCSV = new OleDbDataAdapter();
答案 0 :(得分:2)
关于这个主题有很多问题和答案,但这是一个快速回顾:T
这里有两种访问数据的方法:数据源或手动创建数据类
如何使用数据源可在此处找到:http://msdn.microsoft.com/en-us/library/ee624082.aspx 或者在这里:https://stackoverflow.com/questions/24114537/data-driven-testing-using-excel-file-saved-inside-the-project
另一种方法是创建一个数据类来进行数据访问: 见Coded UI driving test from multiple excel files
使用Microsoft.Office.Interop.Excel;
打开Excel实例:
private Microsoft.Office.Interop.Excel.Application excel;
...
excel = new Microsoft.Office.Interop.Excel.Application();
要打开特定的工作簿:
private Workbook workbook;
....
workbook = excel.Workbooks.Open(@"\\C\Data\YourDriver.xlsx");
获取特定工作表:
Worksheet worksheet = workbook.Worksheets["YourTestCaseWorkSheet"];
将Cell A2作为字符串
Range a2 = worksheet.get_Range("A2");
return a2.Text;
要杀死你的任务:
/// <summary>
/// Quits the excel process
/// </summary>
public void Quit()
{
Marshal.ReleaseComObject(this.worksheet);
Marshal.ReleaseComObject(this.workbook);
this.excel.Quit();
this.excel = null;
}
答案 1 :(得分:0)
//dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using Microsoft.CSharp;
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using System.Windows.Forms;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Threading;
using System.Data.OleDb;
using Microsoft.Office.Interop.Excel;
using System.ComponentModel;
//code for calling the class
Playback.Initialize();
ReadExcel myClassObj = new ReadExcel();
myClassObj.excelRead();
//code for reading excelclass
public void excelRead()
{
Excel.Application appExl = new Excel.Application();
Excel.Workbook workbook = null;
try
{
string methodName = "";
Excel.Worksheet NwSheet;
Excel.Range ShtRange;
//Opening Excel file(myData.xlsx)
appExl = new Excel.Application();
workbook = appExl.Workbooks.Open("E:\\inputSheet", Missing.Value, ReadOnly: false);
NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
ShtRange = NwSheet.UsedRange; //gives the used cells in sheet
int rCnt = 0;
int cCnt = 0;
for (rCnt = 1; rCnt <= ShtRange.Rows.Count; rCnt++)
{
for (cCnt = 1; cCnt <= ShtRange.Columns.Count; cCnt++)
{
if (Convert.ToString(NwSheet.Cells[rCnt, cCnt].Value2) == "Y")
{
methodName = NwSheet.Cells[rCnt, cCnt - 1].Value2;
Type metdType = this.GetType();
MethodInfo mthInfo = metdType.GetMethod(methodName);
if (Convert.ToString(NwSheet.Cells[rCnt, cCnt - 1].Value2) == "fn_AddNum" || Convert.ToString(NwSheet.Cells[rCnt, cCnt - 1].Value2) == "fn_SubNum")
{
StaticVariable.intParam1 = Convert.ToInt32(NwSheet.Cells[rCnt, cCnt + 2].Value2);
StaticVariable.intParam2 = Convert.ToInt32(NwSheet.Cells[rCnt, cCnt + 3].Value2);
object[] mParam1 = new object[] { StaticVariable.intParam1, StaticVariable.intParam2 };
object result = mthInfo.Invoke(this, mParam1);
StaticVariable.intOutParam1 = Convert.ToInt32(result);
NwSheet.Cells[rCnt, cCnt + 4].Value2 = Convert.ToString(StaticVariable.intOutParam1) != "" ? Convert.ToString(StaticVariable.intOutParam1) : String.Empty;
}
else
{
object[] mParam = new object[] { };
mthInfo.Invoke(this, mParam);
NwSheet.Cells[rCnt, cCnt + 4].Value2 = StaticVariable.outParam1 != "" ? StaticVariable.outParam1 : String.Empty;
NwSheet.Cells[rCnt, cCnt + 5].Value2 = StaticVariable.outParam2 != "" ? StaticVariable.outParam2 : String.Empty;
}
NwSheet.Cells[rCnt, cCnt + 1].Value2 = StaticVariable.resultOut;
}
else if (Convert.ToString(NwSheet.Cells[rCnt, cCnt].Value2) == "N")
{
MessageBox.Show("Result is No");
}
else if (Convert.ToString(NwSheet.Cells[rCnt, cCnt].Value2) == "EOF")
{
MessageBox.Show("End of File");
}
}
}
workbook.Save();
workbook.Close(true, Missing.Value, Missing.Value);
appExl.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ShtRange);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(NwSheet);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(appExl);
}
catch (Exception)
{
workbook.Close(true, Missing.Value, Missing.Value);
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.CleanupUnusedObjectsInCurrentContext();
}
}
public void fn_Login()
{
string path = @"E:\log.txt";
try
{
BrowserWindow brwin1 = BrowserWindow.Launch("http://www.ndtv.com");
brwin1.Maximized = true;
HtmlHyperlink htSubmit = new HtmlHyperlink(brwin1);
htSubmit.SearchProperties.Add("InnerText", "Arvind Kejriwal's Appeal Wins Donations for 'Broke' AAP");
htSubmit.DrawHighlight();
StaticVariable.resultOut = true;
}
catch (UITestControlNotFoundException ex)
{
TextWriter tw = new StreamWriter(path, true);
tw.WriteLine(ex.StackTrace);
tw.Close();
StaticVariable.resultOut = false;
}
}
public void fn_LogOut()
{
string path = @"E:\log.txt";
try
{
BrowserWindow brwin1 = BrowserWindow.Launch("http://www.ndtv.com");
brwin1.Maximized = true;
HtmlHyperlink htSubmit = new HtmlHyperlink(brwin1);
htSubmit.SearchProperties.Add("InnerText", "He Has 4 Degrees,But Works as a Garbage Collector in Mumbai");
htSubmit.DrawHighlight();
StaticVariable.resultOut = true;
}
catch (UITestControlNotFoundException ex)
{
TextWriter tw = new StreamWriter(path, true);
tw.WriteLine(ex.StackTrace);
tw.Close();
StaticVariable.resultOut = false;
}
}
public void fn_LinkRes()
{
SqlConnection conRes = new SqlConnection("Data Source=CHDSEZ301257D;Initial Catalog=AGCS;Integrated Security=True");
SqlCommand cmdRes = new SqlCommand();
cmdRes.Connection = conRes;
cmdRes.CommandType = CommandType.Text;
cmdRes.CommandText = "select * from tbl_Links";
SqlDataAdapter daDetails = new SqlDataAdapter(cmdRes);
System.Data.DataTable dtDetails = new System.Data.DataTable();
daDetails.Fill(dtDetails);
StaticVariable.outParam1 = dtDetails.Rows[0][0].ToString();
StaticVariable.outParam2 = dtDetails.Rows[0][1].ToString();
StaticVariable.resultOut = true;
}
public int fn_AddNum(int a,int b)
{
int result = 0;
result = a + b;
StaticVariable.resultOut = true;
return result;
}
public int fn_SubNum(int a, int b)
{
int result = 0;
result = a - b;
StaticVariable.resultOut = true;
return result;
}
//code for static class
public static bool resultOut;
public static int intParam1;
public static int intParam2;
public static int intOutParam1;
public static int intOutParam2;
public static string strParam1;
public static string outParam1;
public static string outParam2;
public static DataTable dtExcel;
//excel data
tcId FuncName Flag Result InParam1 InParam2 OutParam1 OutParam2
Tc_001 fn_Login Y
Tc_002 fn_LogOut N
Tc_003 fn_LinkRes N
Tc_004 fn_AddNum Y 23 34
Tc_005 fn_Login N
Tc_006 fn_LogOut Y
Tc_007 fn_LinkRes N
Tc_008 fn_SubNum N 34 23
Tc_009 fn_Login N
Tc_010 fn_LogOut N
Tc_011 fn_LinkRes Y
Tc_012 fn_AddNum N 23 34
Tc_013 fn_Login N
Tc_014 fn_LogOut N
Tc_015 fn_LinkRes N
Tc_016 fn_SubNum Y 34 23
Tc_017 fn_Login N
Tc_018 fn_LogOut N
Tc_019 fn_LinkRes N
EOF