在C#中逐个重定向到HTML表格特定列中的每个链接

时间:2016-06-02 15:16:51

标签: c# coded-ui-tests

我从数据表中的网页检索了一个表,或者只是说我有一个表,其中第一列包含不同的名称,所有这些名称都是超链接。现在我的要求是我需要逐个点击每个名字在C#或编码的ui。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

它看起来像这样:

[TestMethod]
public async Task TestAllTheLinks()
{
    BrowserWindow browserWindow = BrowserWindow.Launch("mywebsiteurl");

    HtmlTable table = new HtmlTable(browserWindow);
    table.SearchProperties.Add(HtmlTable.PropertyNames.Id, "tableId"); // or other search properties

    List<Exception> failureLinks = new List<Exception>();
    using (var client = new HttpClient())
    {
        for(int rowIndex = 0, max = table.RowCount; rowIndex < max; rowIndex++)
        {
            HtmlCell tableCell = table.GetCell(rowIndex, 0);
            HtmlHyperlink link = new HtmlHyperlink(tableCell);

            // are you sure you want to click?
            // how are you going to test rest of links if you nav away?
            Mouse.Click(link);

            // or would you rather just send an http request to that url to see if it is successful
            string href = link.Href;

            var result = await client.GetAsync(href);
            if (!result.IsSuccessStatusCode)
            {
               failureLinks.Add(new Exception($"Link failed: {href}"));
            }
        }
    }

    if(failureLinks.Any()){
      throw new AggregateException(failureLinks);
    }
}