【问题标题】:C# add only first column text in a table to a List<string> for assertionC# 仅将表中的第一列文本添加到 List<string> 以进行断言
【发布时间】:2020-12-25 06:27:23
【问题描述】:

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

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

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

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;

            }

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

【问题讨论】:

    标签: c# selenium testing automation automated-tests


    【解决方案1】:

    我相信您可以返回一个列表字典,而不是返回一个列表,其中每个文档图块作为键,所有列的列表作为值。

     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;
    
            }
    

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

    What is the best way to iterate over a dictionary?

    【讨论】:

    • 谢谢!这就是我要找的
    【解决方案2】:

    不要只为文件名迭代字符串列表,而是创建一个具有名称、大小、修改日期时间、numberOfDownloads、isDeleteable 等属性的文件列表。 List&lt;Files&gt; files = new List&lt;Files&gt;();

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

    【讨论】:

    • 感谢您的回复!那么,您将如何针对预期的文件列表声明这一点?
    猜你喜欢
    • 2012-12-26
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多