In Selenium WebDriver, if I select drop down value by 'name' then how do I verify that all products are sorted by name?
IWebElement productSort = TestSetup.driver.FindElement(By.XPath("//span/select[@data-test='product_sort_container']"));
productSort.Click();
System.Threading.Thread.Sleep(5000);
IWebElement name = TestSetup.driver.FindElement(By.XPath("//div/span/select/option[@value='az']"));
name.Click();
System.Threading.Thread.Sleep(5000);
CodePudding user response:
These are fun. The way I have accomplished this is to create a list of the before view and then the sorted view into a list. Then take the before view and sort it via linq and then run a compare.
add libraries:
using System.Linq;
using System.Collections.Generic;
List<String> item = new List<string>();
// grab the data that you want to sort
IReadOnlyList<IWebElement> cells = TestSetup.driver.FindElements("your xpath for all items in the list");
// loop through and add into into the ArrayList
foreach (IWebElement cell in cells)
{
item.Add(cell.Text.Trim());
}
List<string> listDefault = item.Select(e => e).ToList();
List<string> listSorted = listDefault.OrderBy(x => x).ToList(); //sort list asc to compare
Assert.IsTrue(listDefault.SequenceEqual(listSorted));