CodedUI 测试不从 CSV 输入文件读取数据 [英] CodedUI test does not read data from CSV input file

查看:28
本文介绍了CodedUI 测试不从 CSV 输入文件读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用编码的 UI 测试方法映射 CSV 文件.这很可能是一个愚蠢的问题,但我似乎无法为我的问题找到解决方案,至少不是一个有效的解决方案.我已确保将 CSV 文件的属性设置为始终复制.我还通过在测试方法上方写入以下行来导入 CSV 文件.

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\Data\login.csv", "login#csv", DataAccessMethod.Sequential), DeploymentItem("login.csv"), 测试方法]

文件名为 login.csv,它位于 Data 文件夹中.

测试将毫无问题地编译,但一旦测试执行,应从 CSV 文件接收输入的字段将留空,并且执行会中断.我已经尝试使用字符串替换 CSV 文件中的数据,并且效果很好.我用来导入每个参数的代码是:

TestContext.DataRow["用户名"].ToString()

此外,CSV 文件包含以下内容:

用户名、密码、全名admin@mail.com,密码,管理员

谁能指出我忘记了什么.

更新:我指出了这个问题,这个问题似乎只与 csv 文件中的第一列有关.当我尝试导入任何其他值时,它工作得很好.

解决方案

某些文本文件以字节顺序标记 (BOM) 开头.编码 UI 中的 CSV 阅读器不处理 BOM 并将其视为第一个字段名称的一部分.下面的屏幕截图显示了带有 BOM 的 CSV 文件和 Notepad++ 中显示的相同文件的调试跟踪.DataRow.ItemArray[...] 值符合预期.DataRow.Table.Columns.ResultsView[...] 显示字段名称,但第一个字段名称包含 BOM.

这个带有 BOM 的 CSV 文件是在 Visual Studio 中使用解决方案资源管理器创建的 => 添加 => 新项目 => C# => 常规 => 文本文件.以前我用 Microsoft Excel 创建了一个电子表格并将其保存为 CSV 文件,该文件没有 BOM.我还使用 Notepad++ 创建了文件并保存为 CSV,但它们没有 BOM.Visual Studio 似乎创建了带有 BOM 的文件,但在编辑 CSV 文件时它不会添加 BOM.

<小时>

Visual Studio 可以创建具有正确编码的文件.在 第 2 步 - 创建数据集"中rel="nofollow noreferrer">此 Microsoft 页面说明了以下文本.(还要感谢 Holistic Developer 在评论中提供非常相似的详细信息.):

<块引用>

  1. 使用正确的编码保存 .csv 文件很重要.在文件菜单上,选择高级保存选项并选择 Unicode(无签名的 UTF-8)– 代码页 65001 作为编码.

I am having difficulty mapping a CSV file with the Coded UI test method. This is most likely a stupid question but I cannot seem to find a solution for my problem, at least not one that works. I have made sure to set the property of the CSV file to Copy always. I have also imported the CSV file by writing the following line above the test method.

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\Data\login.csv", "login#csv", DataAccessMethod.Sequential), DeploymentItem("login.csv"), TestMethod]

The file name is login.csv and it resides in the Data folder.

The test will compile without any problem but once the test executes the fields that should receive input from the CSV file are left empty and the execution is interrupted. I've tried replacing the data from the CSV file by using Strings and it works perfectly fine. The piece of code I am using to import each parameter is:

TestContext.DataRow["Username"].ToString()

Also, the CSV file contains something along the following lines:

Username,Password,Fullname
admin@mail.com,password,Admin

Is there anyone who can point what it is I am forgetting.

Update: I pinpointed the issue, it seems like the issue only revolves around the first column in the csv file. When I try to import any of the other values it works perfectly fine.

解决方案

Some text files start with a Byte Order Mark (BOM). The CSV reader within Coded UI does not handle the BOM and treats it as part of the first field name. The screen shot below shows the debug trace of a CSV file with a BOM and that same file shown in Notepad++. The DataRow.ItemArray[...] values are as expected. The DataRow.Table.Columns.ResultsView[...] shows the field names, but the first field name includes the BOM.

This CSV file with a BOM was created in Visual Studio using Solution Explorer => Add => New item => C# => General => Text file. Previously I have created a spread sheet with Microsoft Excel and saved it as a CSV file, that file did not have a BOM. I have also created files with Notepad++ and saved as CSV and they did not have a BOM. It appears that Visual Studio creates files with a BOM but when editing CSV files it does not add a BOM.


Visual Studio can create files with the correct encoding. Within "Step 2 - Create a data set" of this Microsoft page it states the text below. (Thanks also to Holistic Developer for providing very similar details in a comment.):

  1. It is important to save the .csv file using the correct encoding. On the FILE menu, choose Advanced Save Options and choose Unicode (UTF-8 without signature) – Codepage 65001 as the encoding.

这篇关于CodedUI 测试不从 CSV 输入文件读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆