C# 仅将 table 中的第一列文本添加到列表 <string> 以进行断言

C# add only first column text in a table to a List<string> for assertion

我有这个 table,我想验证文件是否已成功上传

我想遍历第一列并将文件名添加到列表以针对预期列表进行断言

这行得通,但我想知道如何修改我的方法以便能够遍历所有列和行,并且我可以将任何列添加到列表中。基本上使该方法更有用,而不是仅将其用于验证文件名,还可以在需要时验证其他列

public List<string> ListofFilesUploaded()
            {
                IWebElement table = WebDriver.Driver.FindElement(By.XPath("//table[@id='files_list']//tbody"));
                IList<IWebElement> rows = table.FindElements(By.TagName("tr"));
                List<string> fileNames = new List<string>();

                foreach (var row in rows)
                {

                    fileNames.Add(row.Text.Split(' ').First());

                }
                return fileNames;

            }

有谁知道如何增强或改进此解决方案?

我相信您可以 return 列表字典而不是 return 列表,其中每个文档拼贴作为键,所有列的列表作为值。

 public Dictionary<string, List<string>>  ListofFilesUploaded()
        {
            IWebElement table = WebDriver.Driver.FindElement(By.XPath("//table[@id='files_list']//tbody"));
            IList<IWebElement> rows = table.FindElements(By.TagName("tr"));
            Dictionary<string, List<string>> fileNames = new Dictionary<string, List<string>>();

            foreach (var row in rows)
            {
               
                List<string> Col_value = new List<string>();
                IList<IWebElement> cols= row.FindElements(By.TagName("td"));
               foreach (var col in cols)
                 {
                     Col_value.Add( col.Text);
                 }
                fileNames.Add(row.Get_Attribute(“title”), Col_value);

            }
            return fileNames;

        }

现在您可以遍历字典以获取所有文件上传的列表和每个文件的腐蚀列值。可以看到下面 link 同样

What is the best way to iterate over a dictionary?

不是只为文件名遍历字符串列表,而是创建一个文件列表,其属性包括名称、大小、修改日期时间、下载数量、可删除等。 List<Files> files = new List<Files>();

然后您可以遍历由文件列表中的文件表示的每一行。