簡體   English   中英

硒中的產品折扣計算驗證

[英]Product discount calculation verification in selenium

所有我有疑問!

我想問一下,當產品添加到購物車時,我們在自動化測試中有任何選項可以檢查折扣計算。 以下是我的問題的場景:

將打折產品添加到購物車 當我們進入結帳頁面時,系統會根據折扣的百分比檢查產品的價格是否正確。 如果價格正確,則進一步進行。 4.折扣顯示在網站上。 網址: https : //focusclothing.pk/collections/men

我的代碼在這里:

WebElement popup_close = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[4]/div/div[2]/div[4]/div/div/button")));
popup_close.click();
WebElement sale = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[3]/div[1]/div/div[2]/div/div/div/nav/ul/li[8]/a")));
         sale.click();
         List<WebElement> product_list = driver.findElements(By.xpath("/html/body/div[3]/div[2]/div/div/div[8]/div[1]"));
         
         for (WebElement items_product : product_list) {
             System.out.println(items_product.getText());
         }
         System.out.println("Original Price of Items");
         List<WebElement>  Original_price = driver.findElements(By.className("was"));
         System.out.println(Original_price.size());
         
         for(int i=0; i<Original_price.size(); i++) {
             List<String> Original_price_1st = new ArrayList<String>();
             Original_price_1st.add(Original_price.get(i).getText());
             System.out.println(Original_price.get(i).getText());
             }
         System.out.println("Discounted Price of items");
         List<WebElement> Discounted_price=driver.findElements(By.className("onsale"));
           System.out.println(Discounted_price.size());
           for(int i=0; i<Original_price.size();i++)
           {
                   List<String> Discounted_price_lst = new ArrayList<String>();
               Discounted_price_lst.add(Discounted_price.get(i).getText());
               System.out.println(Discounted_price.get(i).getText());
           }
           for(int i=0;i<Discounted_price.size();i++) {
               List<String> Original_price_lst_1 = new ArrayList<String>();
               List<String> Discounted_price_lst_1 = new ArrayList<String>();
               String original_price_removecurrencysymbol = Original_price.get(i).getText().substring(3,Original_price.get(i).getText().length());
               String discount_price_removecurrencysymbol = Discounted_price.get(i).getText().substring(3,Discounted_price.get(i).getText().length());
               System.out.println(original_price_removecurrencysymbol);
               System.out.println(discount_price_removecurrencysymbol);
               
              double difference = Double.valueOf(original_price_removecurrencysymbol) - Double.valueOf(discount_price_removecurrencysymbol);
              System.out.println("Difference is:"+difference);
           }

我面臨的錯誤截圖: https : //prnt.sc/18yvejc直到那里我的代碼找到原始價格和折扣價。 此外,我編寫了代碼來查找價格之間的差異,但出現錯誤。 找到價格之間的差異后,我想根據折扣的百分比檢查差異是否正確。

進入折扣頁面后:

您可以使用以下代碼(此代碼僅適用於您在共享 URL 上看到的第一個網絡元素或圖像或產品):

driver.get("https://focusclothing.pk/collections/men-gym-wear");
WebDriverWait wait = new WebDriverWait(driver, 10);
String a = driver.findElement(By.xpath("(//div[@class='price'])[1]/div[@class='onsale']")).getText();
String b = driver.findElement(By.xpath("(//div[@class='price'])[1]/div[@class='was']")).getText();
String c = driver.findElement(By.xpath("(//div[@class = 'sale-item icn'])[1]")).getText();
System.out.println(a +" "+  b +" "+ c);
String[] aa = a.split("\\.");
String[] bb = b.replaceAll(",", "").split("\\.");
float aaa = Float.parseFloat(aa[1] + "." + aa[2]);
float bbb = Float.parseFloat(bb[1] + "." + bb[2]);
float cc = Float.parseFloat(c.split("\\%")[0]);
float off_cal = bbb - (bbb * cc)/100;
if( off_cal == aaa) {
      System.out.println("matched");
}

更新 1:

得到所有這些:

driver.manage().window().maximize();
        driver.get("https://focusclothing.pk/collections/men-gym-wear");
        WebDriverWait wait = new WebDriverWait(driver, 10);
        ArrayList<WebElement> a = (ArrayList<WebElement>) driver.findElements(By.xpath("//div[@class='price']/div[@class='onsale']"));
        ArrayList<WebElement> b = (ArrayList<WebElement>) driver.findElements(By.xpath("//div[@class='price']/div[@class='was']"));
        ArrayList<WebElement> c = (ArrayList<WebElement>) driver.findElements(By.xpath("//div[@class = 'sale-item icn']"));
        List<Float> aaaa = new ArrayList<Float>() ;
        List<Float> bbbb = new ArrayList<Float>() ;
        List<Float> cccc = new ArrayList<Float>() ;
        System.out.println(a.size() + " " + b.size() + " " + c.size());
        for(int i = 0; i<a.size(); i++) {
             String[] aaa = a.get(i).getText().replaceAll(",", "").split("\\.");
             System.out.println(aaa[1] + "." + aaa[2]);
             aaaa.add(Float.parseFloat(aaa[1] + "." + aaa[2]));
             System.out.println(aaaa.get(i));
             
             String[] bbb = b.get(i).getText().replaceAll(",", "").split("\\.");
             System.out.println(bbb[1] + "." + bbb[2]);
             bbbb.add(Float.parseFloat(bbb[1] + "." + bbb[2]));
             System.out.println(bbbb.get(i));
             
             String[] ccc = c.get(i).getText().split("\\%");
             System.out.println(ccc[0]);
             cccc.add(Float.parseFloat(ccc[0]));
             System.out.println(cccc.get(i));
             
             float off_cal = bbbb.get(i) - (bbbb.get(i)* cccc.get(i))/100;
             if( off_cal == aaaa.get(i)) {
                 System.out.println("matched");
             }
        }

我的本地機器輸出:

[RemoteTestNG] detected TestNG version 7.4.0
Test Base Bot has been activated.
Before suite executed successfully
Starting ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429}) on port 10675
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1625643389.399][WARNING]: This version of ChromeDriver has not been tested with Chrome version 91.
Jul 07, 2021 1:06:29 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
17 17 17
976.50
976.5
1395.00
1395.0
30
30.0
matched
836.50
836.5
1195.00
1195.0
30
30.0
matched
836.50
836.5
1195.00
1195.0
30
30.0
matched
836.50
836.5
1195.00
1195.0
30
30.0
matched
1256.50
1256.5
1795.00
1795.0
30
30.0
matched
976.50
976.5
1395.00
1395.0
30
30.0
matched
976.50
976.5
1395.00
1395.0
30
30.0
matched
1186.50
1186.5
1695.00
1695.0
30
30.0
matched
836.50
836.5
1195.00
1195.0
30
30.0
matched
836.50
836.5
1195.00
1195.0
30
30.0
matched
1186.50
1186.5
1695.00
1695.0
30
30.0
matched
836.50
836.5
1195.00
1195.0
30
30.0
matched
1186.50
1186.5
1695.00
1695.0
30
30.0
matched
976.50
976.5
1395.00
1395.0
30
30.0
matched
976.50
976.5
1395.00
1395.0
30
30.0
matched
1186.50
1186.5
1695.00
1695.0
30
30.0
matched
1186.50
1186.5
1695.00
1695.0
30
30.0
matched
PASSED: testSO

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


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

==============

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM