2016-08-09 31 views
0

電子商務應用程序我必須測試過濾項目形式低到高價格意味着下一個項目總是應該大於等於前一個。 我必須與硒腳本進行比較,並且如果所有項目都相應顯示,則需要獲得結果(PASS/FAIL)。 下面我在一頁中的同一頁上爲獲得價格清單寫了腳本,有24個項目,但我不知道如何比較價格。請幫我一下。要測試電子商務排序項目(高價格到低價格)顯示是否正確使用硒webdriver


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

在這裏我得到的輸出:

4.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 6.00 6.00 6.00 6.00 6.00 6.00 6.00 6.00 6.00 7.00

回答

1

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; 
    } 
0

我會用稍微不同的方法。我會得到列表中的所有價格,對列表進行排序,然後將其與原始列表進行比較。如果兩個列表相同,則列表被排序。

// 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)); 
+0

JeffC嗨, 我使用你所建議的代碼,價格比較,但它並不總是給出正確的結果。如果價格上漲Rs9.00到Rs10.00或Rs99.00到Rs100.00,那麼它給出錯誤的輸出「False」,但實際上它必須是真實的。 我必須進行價格比較以及多頁分頁。 請幫助我如何分類價格與分頁。 – Ashu

+0

這應該作爲一個新問題發佈。如果您發現這些答案中的任何一條都有幫助,那麼您應該注意它們。你應該標記最能夠接受的答案,這樣問題就不會得到回答。 http://stackoverflow.com/help/someone-answers – JeffC

0

除了JeffC,如何使用junit或testng來做斷言而不是打印比較?

assertEquals("Sorting low to high prices aint working.",prices, sortedPrices);