简体   繁体   English

使用Selenium和Specflow C#验证所有值都在下拉列表中

[英]Verify all values are in the dropdown list using selenium and specflow c#

im new to selenium, im writting some functional tests. 我是硒的新手,我写了一些功能测试。 I am now struggling to verify the values that are in the dropdown list. 我现在正在努力验证下拉列表中的值。

 1. I have my step like:

Then I confirm the Status options include
 | *Current Status*                   |
 | In Any Status                      |
 | Pending Allocation                 |
 | Work In Progress                   |

 2. My Search page method


    public void VerifyValuationStatusOptions(Table statusList)
    {
        var elementId = "myId";
        var displayedHeadings = new List<string>();

        WaitForElements(By.Id(elementId));
        IList<IWebElement> elements = FindElements(By.Id(elementId)).ToList();

        foreach (var item in elements)
        {
            displayedHeadings.Add(item.Text);
        }
        foreach (var row in statusList.Rows)
        {
            displayedHeadings.Should().Contain(row["In Any Status"]);
            displayedHeadings.Should().Contain(row["Pending Allocation"].TrimEnd());
            displayedHeadings.Should().Contain(row["Work In Progress "].TrimEnd());               
        }
    }

Then i get the error: System.IndexOutOfRangeException : Could not find a column named 'In Any Status' in the table. 然后我得到错误:System.IndexOutOfRangeException:在表中找不到名为“处于任何状态”的列。

Here's how I do it. 这是我的方法。

Specflow scenario step Specflow场景步骤

    And the 'TimeZoneId' dropdown should contain the values 
    | Value                           | Text                                                          |
    |                                 | --Select--                                                    |
    | Dateline Standard Time          | (UTC-12:00) International Date Line West                      |
    | UTC-11                          | (UTC-11:00) Coordinated Universal Time-11                 |
    | Hawaiian Standard Time          | (UTC-10:00) Hawaii                                        |

And the implemented step definition, where p0 is the select element name 以及已实现的步骤定义,其中p0是选择元素名称

    [Then(@"the '(.*)' dropdown should contain the values")]
    public void ThenTheDropdownShouldContainTheValues(string p0, Table table)
    {
        var dropdown = new SelectElement(Selenium.FindElement(By.Name(p0)));

        var dropDownTextValues = dropdown.Options.Select(webElement => webElement.Text).ToList();
        var textValues = table.Rows.Select(x => x[1]).ToList();

        CollectionAssert.AreEquivalent(dropDownTextValues, textValues);

        var dropDownValues = dropdown.Options.Select(webElement => webElement.GetAttribute("value")).ToList();
        var values = table.Rows.Select(x => x[0]).ToList();

        if (values.All(x => x == String.Empty))
            return;

        CollectionAssert.AreEquivalent(dropDownValues, values);
    }

you could get a little fancier to make sure the value and text match exactly, but this works for me. 您可以花一点儿钱来确保值和文本完全匹配,但这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM