在不使用 UIMap/Recorded 代码的情况下从 CSV 文件输入到 Web C# 中的文本字段

Input from CSV file to a text field in Web C# without using UIMap/Recorded code

我是编码 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

请告诉我最简单的方法!

data source 添加到您的测试方法中。 将 csv 文件添加到您的解决方案中。右键单击文件夹/解决方案,添加现有项目,select 文件,将文件 'Copy to Output Directory' 的属性更改为 'copy always'

        [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());            

        }

我无法发表评论,所以我假设您希望他们在搜索下一个值之前按下一个按钮。对我来说最简单的方法是使用 reader 读取 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);
}

注意:我没有写如何让它停止阅读,如果你需要这个,请评论。 如果有任何问题,请在评论中告诉我,我会进行编辑。