【问题标题】:Move an IWeb Element from one list to another and comparing their count using Selenium ?将 IWeb 元素从一个列表移动到另一个列表并使用 Selenium 比较它们的计数?
【发布时间】:2018-08-01 07:32:03
【问题描述】:

目标是为 QA​​ 创建一个自动化测试,该测试通过使用 Selenium WebDriver 拥有的元素/项目的数量来断言一个列表与另一个列表不同。

这是获取列表的网页:http://demoqa.com/sortable/ 然后连接列表

这是代码:

[Test]

//Arrange

_driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
_driver.Navigate().GoToUrl("http://demoqa.com/sortable/"); 

List<IWebElement> sortableListOne = _driver.FindElements(By.Id("sortable1")).ToList();

IWebElement sortableListOneFifth = _driver.FindElement(By.XPath(@"//*[@id=""sortable1""]/li[5]"));

List<IWebElement> sortableListTwo = _driver.FindElements(By.Id("sortable2")).ToList();

IWebElement sortableListTwoForth = _driver.FindElement(By.XPath(@"//*[@id=""sortable2""]/li[4]"));

//Act

Actions action = new Actions(_driver);
            action.DragAndDrop(SortableListOneFifth, SortableListTwoForth)
                .Perform(); 

所以我尝试了:

//Assert
        var list1 = _sortPage.SortableListOne.Count;
        var list2 = _sortPage.SortableListTwo.Count;

        list1.Should().NotBe(list2);

错误信息:

Message: Did not expect list1 to be 1.

两个列表都返回 1 的计数,因此它们始终相同,并且不返回列表的 IWeb 元素。

我是否需要创建一个 for 循环来迭代每个列表?关于如何进行的想法?

【问题讨论】:

    标签: c# selenium selenium-webdriver automated-tests qa


    【解决方案1】:

    看起来 sortableListOnesortableListTwo WebElements 是使用 id 标识的,并且只找到一个匹配的元素,因此它返回为 1。

    请使用下面的 xpath 找到 sortableListOnesortableListTwo WebElements

    代码:

    List<IWebElement> sortableListOne = _driver.FindElements(By.XPath("//ul[@id='sortable1']/li")).ToList();
    
    List<IWebElement> sortableListTwo = _driver.FindElements(By.XPath("//ul[@id='sortable2']/li")).ToList();
    

    更改 sortableListOneandsortableListTwo` 元素定位器后,请在您的测试方法中使用以下内容,它将返回正确的计数

    var list1 = _sortPage.SortableListOne.Count;
    var list2 = _sortPage.SortableListTwo.Count;
    
    list1.Should().NotBe(list2);
    

    【讨论】:

    • 很高兴知道它有帮助。请采纳答案
    【解决方案2】:

    sortableListOnesortableListTwo 包含一个元素,即项目容器。要获取所有项目的列表,您需要在每个项目下找到&lt;li&gt; 标签,拖放操作之后。更改网页不会影响先前定位的元素,您可能会收到StaleElementReferenceException

    Actions action = new Actions(_driver);
    action.DragAndDrop(SortableListOneFifth, SortableListTwoForth).Perform();
    
    List<IWebElement> sortableListOne = _driver.FindElements(By.XPath(@"//*[@id='sortable1']/li")).ToList();
    List<IWebElement> sortableListTwo = _driver.FindElements(By.XPath(@"//*[@id='sortable2']/li")).ToList();
    
    var list1 = _sortPage.SortableListOne.Count;
    var list2 = _sortPage.SortableListTwo.Count;
    
    list1.Should().NotBe(list2);
    

    【讨论】:

      猜你喜欢
      • 2017-11-28
      • 1970-01-01
      • 2018-09-03
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      相关资源
      最近更新 更多