'Move an IWeb Element from one list to another and comparing their count using Selenium?

Objective is to create a automated test for QA which asserts that one List is different from another by the count of elements/items which they possess by using the Selenium WebDriver.

This is the webpage for getting the lists : http://demoqa.com/sortable/ and then Connect Lists

This is the code :

[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(); 

So i tried :

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

        list1.Should().NotBe(list2);

The error message :

Message: Did not expect list1 to be 1.

Both lists are returning the count of 1 so they are always the same and not returning the IWeb elements of lists.

Do I need to create a for cycle to iterate each list ? Ideas on how to proceed ?



Solution 1:[1]

sortableListOne and sortableListTwo contains one element, the items container. To get list of all the items you need to locate the <li> tags under each one, after the drag and drop action. Changing the web page doesn't effect previously located elements and you might get 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);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guy