selenium中的java产品折扣计算验证

sirbozc5  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(463)

我只有一个疑问!
我想问一下,在自动化测试中,当产品添加到购物车时,我们是否可以选择检查折扣计算。下面是我问题的情景:
当我们进入结账页面时,将折扣产品添加到购物车系统,根据折扣百分比检查产品价格是否正确。如果价格正确,则继续。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 在此之前,我的代码可以找到原价和折扣价。此外,我编写了代码来查找价格之间的差异,但出现了一个错误。找到价格差异后,我想根据折扣百分比检查差异是否正确。

q8l4jmvw

q8l4jmvw1#

进入折扣页面后:
您可以使用以下代码(此代码仅适用于您在共享url上看到的第一个web元素、图像或产品):

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
==================================

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

相关问题