【问题标题】:Verify sorting order in e-commerce website using selenium web driver java使用 selenium web driver java 验证电子商务网站中的排序顺序
【发布时间】:2018-08-08 19:28:10
【问题描述】:

我的场景是,验证电子商务网站中的排序顺序 有一个网站,如果我在搜索框中提供一些值并点击搜索,那么它将随机获取一些结果(超过 500 页),如果我点击过滤器并从低到高排序,现在价格将为800,50000,200,100000 etc价格然后它将在2,5,100,200,500,1000,20000 etc 中给出结果。

现在我在 selenium 中为此编写了脚本,我只在第 1 页上获取价格并输入名为 IntegerList1 的列表名称,并使用集合按排序顺序排列列表,这个名为 sortedprices 的列表:- @ 987654324@

但是当我使用 select class 进行排序并获取结果并将这些都放在另一个名为 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, 37500, 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);   
}

结果:-

[RemoteTestNG] detected TestNG version 6.14.3
Starting ChromeDriver 2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab) on port 29529
Only local connections are allowed.
Aug 08, 2018 1:12:03 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Page 1 consists of 48  price elements
Integerlist1 is [7300, 84999, 139000, 225000, 235850, 925000, 123000, 424900, 1050000, 26200000, 599000, 265000, 525000, 590000, 1595000, 1395000, 638900, 334900, 1070000, 979900, 427850, 1085000, 294900, 999000, 465000, 247900, 926000, 305000, 380000, 379000, 479000, 315000, 590000, 359000, 479000, 835000, 229000, 499000, 498000, 699000, 240000, 344900, 130000, 870000, 329000, 359900, 330000, 575000]
Sorted list is [7300, 84999, 123000, 130000, 139000, 225000, 229000, 235850, 240000, 247900, 265000, 294900, 305000, 315000, 329000, 330000, 334900, 344900, 359000, 359900, 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, 1395000, 1595000, 26200000]
price2 are [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, 37500, 38500, 39900, 40000, 40000]
Integer2 list is [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, 37500, 38500, 39900, 40000, 40000]
PASSED: sorting

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

【问题讨论】:

    标签: java selenium sorting


    【解决方案1】:

    你可以先从网上排序,取值,用代码排序,然后比较是否相等。

    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");
    

    【讨论】:

    • 感谢 sers 先生的回答,当我使用 'collect(to List())' 时出现错误,'toList()' 在这里是什么意思?
    • 你需要 Java 8
    • 无论如何,我知道了你的排序方法,谢谢你,我解决了。
    猜你喜欢
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 2013-08-31
    • 2021-03-17
    相关资源
    最近更新 更多