电子商务应用程序,我必须测试过滤项目从低到高的价格意味着下一个项目总是应该大于等于前一个。我必须比较相同的硒脚本,需要得到结果(通过/失败),如果所有项目都相应地显示。下面是我为同一页上同一页上的价格清单写的脚本,有24个项目,但我不知道如何比较价格。请帮帮我。
public class Price extends WebDriverCommonLib
{
@Test
public void lowToHigh() throws InterruptedException
{
Driver.driver.get("http://.....");
Driver.driver.findElement(By.xpath("//a[@class='submit-form']//i[@class='fa fa-search']")).click();
Select select = new Select(Driver.driver.findElement(By.name("product-sort")));
select.selectByVisibleText("Price - Low to High");
normalWait();
java.util.List<WebElement> price = Driver.driver.findElements(By.xpath("//span[@class='find_prices']"));
System.out.println(price.size());
//List ourAl = new ArrayList<>();
for (int i = 0; i<price.size(); i=i+1)
{
System.out.println(price.get(i).getText());
}
}
}
这里我得到了输出:
24日4.005.005.005.005.005.005.005.005.005.005.005.005.005.006.006.006.006.006.006.006.006.006.007.00
1)首先将所有的价格值添加到动态数组列表中,
ArrayList<Float> priceList = new ArrayList<Float>();
for (int i = 0; i<price.size(); i=i+1) {
priceList.add(Float.parseFloat(price.get(i).getText()));
}
if(!ascendingCheck(priceList)){
Assert.fail("Not is ascending order");
}
2)并创建以下方法来验证订单,
Boolean ascendingCheck(ArrayList<Float> data){
for (int i = 0; i < data.size()-1; i++) {
if (data.get(i) > data.get(i+1)) {
return false;
}
}
return true;
}
我会使用稍微不同的方法。我将在一个列表中获取所有的价格,对列表进行排序,然后将其与原始列表进行比较。如果两个列表相等,则该列表已排序。
// scrape price elements
List<WebElement> price = driver.findElements(By.xpath("//span[@class='find_prices']"));
// extract the prices from the price elements and store in a List
List<String> prices = new ArrayList<String>();
for (WebElement e : price)
{
prices.add(e.getText());
}
// make a copy of the list
List<String> sortedPrices = new ArrayList<String>(prices);
// sort the list
Collections.sort(sortedPrices);
// true if the prices are sorted
System.out.println(sortedPrices.equals(prices));
除了JeffC,如何使用junit或测试来做断言而不是打印比较?
assertEquals("Sorting low to high prices aint working.",prices, sortedPrices);