Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将IWeb元素从一个列表移动到另一个列表,并使用Selenium比较它们的计数?_C#_Selenium_Selenium Webdriver_Automated Tests_Qa - Fatal编程技术网

C# 将IWeb元素从一个列表移动到另一个列表,并使用Selenium比较它们的计数?

C# 将IWeb元素从一个列表移动到另一个列表,并使用Selenium比较它们的计数?,c#,selenium,selenium-webdriver,automated-tests,qa,C#,Selenium,Selenium Webdriver,Automated Tests,Qa,目标是为QA创建一个自动化测试,该测试通过使用Selenium WebDriver来断言一个列表与另一个列表的元素/项目计数不同 这是获取列表的网页:然后连接列表 代码如下: [Test] //Arrange _driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); _driver.Navigate().GoToUrl("http://demoqa.com/sort

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

这是获取列表的网页:然后连接列表

代码如下:

[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(); 
错误消息:

Message: Did not expect list1 to be 1.
两个列表都返回1的计数,因此它们总是相同的,并且不返回列表的IWeb元素


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

sortableListOne
sortablelistwo
包含一个元素,items容器。要获取所有项目的列表,您需要在拖放操作后找到每个标签下的
  • 标签。更改网页不会影响以前定位的元素,您可能会得到
    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);
    
    Actions action=新操作(\u驱动程序);
    action.DragAndDrop(SortableListOneFifth,sortableListToforth).Perform();
    List-sortableListOne=_-driver.FindElements(By.XPath(@/*[@id='sortable1']/li)).ToList();
    List-sortablelistwo=_-driver.FindElements(By.XPath(@/*[@id='sortable2']/li)).ToList();
    var list1=\u sortPage.SortableListOne.Count;
    var list2=_sortPage.sortablelistwo.Count;
    list1.Should().NotBe(list2);
    
    它看起来是
    sortableListOne
    sortableListTwo
    使用id标识WebElements,只找到一个匹配的元素,因此返回为1

    请使用xpath查找
    sortableListOne
    sortableListTwo
    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();
    

    很高兴知道这有帮助。请接受答案
    List<IWebElement> sortableListOne = _driver.FindElements(By.XPath("//ul[@id='sortable1']/li")).ToList();
    
    List<IWebElement> sortableListTwo = _driver.FindElements(By.XPath("//ul[@id='sortable2']/li")).ToList();
    
    var list1 = _sortPage.SortableListOne.Count;
    var list2 = _sortPage.SortableListTwo.Count;
    
    list1.Should().NotBe(list2);