使用selenium web驱动程序java验证电子商务网站中的排序顺序

使用selenium web驱动程序java验证电子商务网站中的排序顺序,java,selenium,sorting,Java,Selenium,Sorting,这里我的场景是,验证电子商务网站中的排序顺序 有一个网站,如果我在搜索框中提供一些值并点击搜索,那么它将获得一些结果(超过500页)随机出现的价格将是800500002000100000等如果我点击过滤器并从较低价格排序到最高价格,它将在2,510020005001000000000等中给出结果 现在我用selenium编写了这个脚本,我只在第1页上获取价格,输入名为IntegerList1的列表名,并使用集合和名为sortedprices的列表按排序顺序排列列表:-[7300, 84999,

这里我的场景是,验证电子商务网站中的排序顺序 有一个网站,如果我在搜索框中提供一些值并点击搜索,那么它将获得一些结果(超过500页)随机出现的价格将是
800500002000100000等
如果我点击过滤器并从较低价格排序到最高价格,它将在
2,510020005001000000000等
中给出结果

现在我用selenium编写了这个脚本,我只在第1页上获取价格,输入名为
IntegerList1
的列表名,并使用集合和名为sortedprices的列表按排序顺序排列列表:-
[7300, 84999, 123000, 130000, 139000, 225000, 229000, 235850, 240000, 247900, 265000, 294900, 305000, 315000, 329000, 330000, 334900, 344900, 359000, 379000, 380000, 424900, 427850, 465000, 479000, 479000, 498000, 499000, 525000, 575000, 590000, 590000, 599000, 638900, 699000, 835000, 870000, 925000, 926000, 979900, 999000, 1050000, 1070000, 1085000、1150000、1395000、1595000、26200000]

但是,当我使用select类进行排序并获取结果并将它们全部放入另一个名为Integer list2的列表中时:-
[250、650、700、750、800、800、850、1000、1250、1400、1600、1600、1650、1800、2750、4800、7300、7700、8000、8500、10000、11750、15000、18500、22000、22500、22500、24900、25000、29900、30000、30000、30000、31000、32700、33999、34000、34900、34990、35000、35000、35000、35000、35000、35000、35000、35000、35000、35000、37000、38500、39900、40000、40000]

这里的sortedprices列表仅在第1页对价格进行排序,但
Integerlist2
通过产品对产品进行排序,现在我想比较排序后的列表和
intlist2
列表。有人能帮我摆脱它吗,代码如下:

public void sortingPrices() throws InterruptedException{  
driver.get("URL");
List<WebElement> table = driver.findElements(By.cssSelector("[class*='data- 
price']"));
ArrayList<String> Prices1 = new ArrayList<String>();
List<Integer> IntList1 = new ArrayList<Integer>();
for(int i=0;i<table.size();i++){
Prices1.add(table.get(i).getText().replace("$", "").replace(",", ""));
}
System.out.println("prcies are "+Prices1);
System.out.println("Page 1 consists of " +Prices1.size()+ "  price 
elements");
Thread.sleep(3000);

/*converting string list Prcies1 to integer list */
for (String s: Prices1){
    IntList1.add(Integer.valueOf(s));  
}
System.out.println("Integerlist1 is "+IntList1);    

/*Array list named as sortedPrices and passing integer list into it to sort*/
ArrayList<Integer> sortedPrices = new ArrayList<Integer>(IntList1);
Collections.sort(sortedPrices);
System.out.println("Sorted list is "+sortedPrices);
/*clicking on sort dropdown and arranging in ascending order*/  
Select s = new Select(driver.findElement(By.cssSelector("[id='srp- 
sortby']")));
s.selectByValue("1");
Thread.sleep(5000);
List<WebElement>table2= driver.findElements(By.cssSelector("[class*='data- 
price']"));
List<Integer> IntList2 = new ArrayList<Integer>();
ArrayList<String> Prices2 = new ArrayList<String>();
for(int i=0;i<table2.size();i++){
    Prices2.add(table2.get(i).getText().replace("$", "").replace(",", ""));
    }
    System.out.println("price2 are "+Prices2);
/*converting string list to integer list */
for (String s1: Prices2){
    IntList2.add(Integer.valueOf(s1));  
    }
    System.out.println("Integer2 list is "+IntList2);   
}

您可以首先从web排序,获取值,用代码排序,然后比较它们是否相等

Select s = new Select(driver.findElement(By.cssSelector("[id='srp-sortby']")));
s.selectByValue("1");    
List<WebElement> elements = driver.findElements(By.cssSelector("[class*='data-price']"));
List<String> webSortedPrices = elements.stream().map(WebElement::getText).collect(toList());
List<String> mySortedPrices = elements.stream().map(WebElement::getText).sorted(Comparator.comparingInt(Integer::valueOf)).collect(toList());
Assert.assertEquals(webSortedPrices, mySortedPrices, "Should be sorted from lower to highest price");
Select s=new Select(driver.findelelement(By.cssSelector(“[id='srp-sortby']”));
s、 selectByValue(“1”);
列表元素=driver.findElements(By.cssSelector(“[class*='data-price']”);
列出webSortedPrices=elements.stream().map(WebElement::getText.collect(toList());
List mySortedPrices=elements.stream().map(WebElement::getText).sorted(Comparator.comparingInt(Integer::valueOf)).collect(toList());
Assert.assertEquals(webSortedPrices,mySortedPrices,“应按从低到高的价格排序”);

谢谢你的回答,瑟斯先生,我在使用“collect(to List())”时出错了,“toList()”在这里是什么意思?你需要Java 8无论如何,我知道了你的排序方法,谢谢你,我解决了它。
Select s = new Select(driver.findElement(By.cssSelector("[id='srp-sortby']")));
s.selectByValue("1");    
List<WebElement> elements = driver.findElements(By.cssSelector("[class*='data-price']"));
List<String> webSortedPrices = elements.stream().map(WebElement::getText).collect(toList());
List<String> mySortedPrices = elements.stream().map(WebElement::getText).sorted(Comparator.comparingInt(Integer::valueOf)).collect(toList());
Assert.assertEquals(webSortedPrices, mySortedPrices, "Should be sorted from lower to highest price");