简体   繁体   English

以 Excel 文件作为数据源的数据驱动编码 UI 测试

[英]Data driven coded UI test with excel file as a data source

I built a coded UI test and I want to make it a data driven test , I want to get the data from an excel file ( Data.xls )located in the same directory with the project files, I used this line as shown in the MSDN site , but it didnt work , what changes should I do to the line ?我构建了一个编码的 UI 测试,我想使它成为一个数据驱动的测试,我想从与项目文件位于同一目录中的 excel 文件 (Data.xls) 中获取数据,我使用了这一行,如MSDN 站点,但它不起作用,我应该对线路做哪些更改?

    [DataSource("System.Data.Odbc", "Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xls)};dbq=|DataDirectory|\\Data.xls;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true", "Sheet1$", DataAccessMethod.Sequential), TestMethod]

thank you谢谢你

Here's the line of code that I use to connect the DataSource to my tests:这是我用来将数据源连接到我的测试的代码行:

[DataSource("System.Data.Odbc", "Dsn=Excel Files;dbq=|DataDirectory|\\Logins.xlsx;defaultdir=C:\\Data\\Logins.csv;driverid=1046;maxbuffersize=2048;pagetimeout=5", "Sheet1$", DataAccessMethod.Sequential), DeploymentItem("Logins.xlsx"), TestMethod]

Then in my code I used:然后在我的代码中我使用了:

LoginParams.TextBoxInfo = TestContext.DataRow["ColumnHeader"].ToString();

However, I did set my excel spreadsheet as a Deployment item.但是,我确实将我的 excel 电子表格设置为部署项目。 I'm not sure if you've already done that.我不确定你是否已经这样做了。

As a workaround create a new class, and add this data source.作为解决方法,创建一个新类,并添加此数据源。 You can use the same UIMap, and same code, just add a new Coded UI Test.您可以使用相同的 UIMap 和相同的代码,只需添加一个新的编码 UI 测试。 "Project"->"New CodedUI Test". “项目”->“新的 CodedUI 测试”。

I have used same excel workbook but different sheets for my tests as below:我使用了相同的 excel 工作簿,但我的测试使用了不同的工作表,如下所示:

namespace Data_driven_testing
{
/// <summary>
/// Summary description for CodedUITest2
/// </summary>

[CodedUITest]

public class CodedUITest2
{
    public CodedUITest2()
    {
    }

    [DataSource("System.Data.Odbc", "Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xls)};dbq=D:\\CodedUI\\Data_driven_testing\\Data_driven_testing\\Data.xls;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true", "Sheet2$", DataAccessMethod.Sequential), TestMethod]

    public void CodedUITestMethod1()
    {


        this.UIMap.RecordedMethod3Params.UITextBox1EditText = TestContext.DataRow["Input1"].ToString();
        this.UIMap.RecordedMethod3Params.UITextBox2EditText = TestContext.DataRow["Input2"].ToString();
        this.UIMap.RecordedMethod3();


        // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
        // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
    }



 namespace Data_driven_testing
 {
   /// <summary>
   /// Summary description for CodedUITest1
   /// </summary>
[CodedUITest]
public class CodedUITest1
{
    public CodedUITest1()
    {
    }

    //[TestMethod]
    [DataSource("System.Data.Odbc", "Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xls)};dbq=D:\\CodedUI\\Data_driven_testing\\Data_driven_testing\\Data.xls;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true", "Sheet1$", DataAccessMethod.Sequential), TestMethod]


    public void CodedUITestMethod1()
    {
        this.UIMap.RecordedMethod1Params.UITextBox3Text = TestContext.DataRow["Input1"].ToString();
        this.UIMap.RecordedMethod1Params.UITextBox4EditText = TestContext.DataRow["Input1"].ToString();

        this.UIMap.RecordedMethod1();
        this.UIMap.RecordedMethod2Params.UITextBox5EditText = TestContext.DataRow["Input3"].ToString();
        this.UIMap.RecordedMethod2Params.UITextBox6EditText = TestContext.DataRow["Input4"].ToString();

        // this.UIMap.RecordedMethod2();


    }

If you could have provided us with a stack trace, may be we could have given you the exact solution.如果您可以为我们提供堆栈跟踪,也许我们可以为您提供确切的解决方案。

Still, seeing your comment I think, If you get a connection error, it's most likely due to the fact that you don't have the 2007 Office System Driver installed for the OLEDB provider.尽管如此,看到您的评论,我认为,如果出现连接错误,很可能是因为您没有为 OLEDB 提供程序安装 2007 Office System Driver。

You can download it from the following Microsoft link: http://www.microsoft.com/en-us/download/details.aspx?id=23734您可以从以下 Microsoft 链接下载: http : //www.microsoft.com/en-us/download/details.aspx?id=23734

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM