我是编码UI测试的初学者,编码不是我的强项。我想用简单的答案来问你们这个问题。
假设我在C#中有一个自动脚本
如果我希望用CSV文件中的某些值替换静态字符串值我该怎么做?
期望:
以下是我的代码。
public CodedUITest1()
{
}
[TestMethod]
public void openBrw()
{
BrowserWindow browser = BrowserWindow.Launch("www.google.com");
UITestControl UISearch = new UITestControl(browser);
UISearch.TechnologyName = "Web";
UISearch.SearchProperties.Add("ControlType", "Edit");
UISearch.SearchProperties.Add("Id", "lst-ib");
Keyboard.SendKeys(UISearch, "Australian Cricket Team");
this.UIMap.
我的CSV文件看起来像1列
column1
How to win matches
Where to find dragons
japan in 1998
请告诉我最简单的方法!
答案 0 :(得分:0)
在data source中添加测试方法。 将csv文件添加到您的解决方案中。右键单击文件夹/解决方案,添加现有项目,选择文件,更改文件的属性'复制到输出目录'始终'
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod]
[TestMethod]
public void openBrw()
{
BrowserWindow browser = BrowserWindow.Launch("www.google.com");
UITestControl UISearch = new UITestControl(browser);
UISearch.TechnologyName = "Web";
UISearch.SearchProperties.Add("ControlType", "Edit");
UISearch.SearchProperties.Add("Id", "lst-ib");
//search by column1
Keyboard.SendKeys(UISearch, TestContext.DataRow["column1"].ToString());
}
答案 1 :(得分:0)
我无法评论,因此我假设您希望他们在搜索下一个值之前按下按钮。对我来说最简单的方法是使用读取器读取csv文件然后读取一行,当他们按下按钮读取下一行
private String path = @"Your Path Here"
private int i = 1;
private static String BrowserLocation;
public Form1()
{
InitializeComponent();
}
public void Form1_Load(){ //You can click on the form in the designer to automatically create this method
BrowseLocaton = ReadCsvLine(path, i);
openBrsw(BrowseLocation);
//Instantly browse to first line in the csv file when it loads
}
public openBrsw(String LaunchPath) //I edited the method
{
BrowserWindow browser = BrowserWindow.Launch(LaunchPath);
//put your browser properties here
}
public static String ReadCSVLine(String CsvPath, int LineNumber)
{
//this retrieves the line number + 1 (LineNumber = 99 retrieves line 100) because of the column header
return File.ReadLines(CsvPath).ElementAtOrDefault(LineNumber);
//Method above is same as File.ReadAllLines(CsvPath, Encoding.default).ElementAt(LineNumber)
}
private void NextButton_Click()
{//Make a button called NextButton and set this method to clicked event in designer(Comment if you need help on this)
//When they click on this button navigate to next address
i++;
BrowseLocaton = ReadCsvLine(path, i);
openBrsw(BrowseLocation);
}
注意:我没有说明如何让它停止阅读,如果你需要,请评论。 如果有任何问题,请将其作为评论告知我,我将对其进行编辑。