How to split the value & convert string to float in selenium?
package TestCaseA;
import java.text.DecimalFormat;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import BaseClass.BaseInit;
import MyMethods.SignIn;
import Utility.ScreenShot;
public class Cart extends BaseInit {
@Test
public static void cart() throws InterruptedException {
reports.attachReporter(reporters);
ExtentTest Test = reports.createTest("Cart");
driver.get(sitedata.getProperty("url"));
try {
SignIn.login("uss.jignesh@gmail.com", "123456");
Thread.sleep(10000);
WebDriverWait wait = new WebDriverWait(driver, 10);
// wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loginDropDown")));
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Request Refill")));
epresent("cart_dropdown_id").click();
// Actions action = new Actions(driver);
// WebElement we = epresent("Cart_hover_xpath");
// action.moveToElement(we).build().perform();
ScreenShot.getScreenShot("cart.jpg", driver);
} catch (Exception e) {
System.out.println("Element not found");
}
epresent("porder_id").click();
String curl = driver.getCurrentUrl();
if (curl.equalsIgnoreCase(sitedata.getProperty("cart_url"))) {
WebElement divv = driver.findElement(By.xpath("//*[@id=\"medication-cart\"]/div[1]/div[1]"));
List<WebElement> tag1 = divv.findElements(By.tagName("a"));
List<WebElement> tag = divv.findElements(By.className("price"));
float result = 0.0f;
for (int j = 0; j < tag1.size(); j++) {
String geturl = tag1.get(j).getAttribute("href");
String medname = tag1.get(j).getText();
Thread.sleep(5000);
String price = tag.get(j).getText();
System.out.println("Medicine Url: " + geturl + "\n Med Name: " + medname + "\n Price: " + price);
Test.log(Status.INFO, medname + " " + price);
String p1 = price.replace("$", "");
System.out.println("Price after replace: " + p1);
float f = Float.parseFloat(p1);
result = result + f;
}
String formattedString = String.format("%.02f", result);
Test.log(Status.INFO, "Expected med total: " + formattedString);
float formattedString1 = Float.parseFloat(formattedString);
Thread.sleep(1000);
String tprice = epresent("T_price_xpath").getText();
String p1 = tprice.replace("$", "");
System.out.println("\n Total Price: " + p1);
Test.log(Status.INFO, "Actual med total: " + p1);
float f = Float.parseFloat(p1);
if (formattedString1 == f) {
System.out.println("Sub total is right");
Test.log(Status.PASS, "Expected & Actual total is correct");
} else {
System.out.println("Sub total is not right");
Test.log(Status.FAIL, "Expected & Actual total is incorrect");
}
epresent("promo_id").sendKeys("jignesh");
epresent("apply_id").click();
Thread.sleep(5000);
// WebDriverWait wait = new WebDriverWait(driver, 10);
// wait.until(ExpectedConditions.elementToBeClickable(By.id("removePromocode")));
String promoPrice = driver.findElement(By.xpath("//*[@id=\"promo_code_used\"]/div[2]")).getText();
System.out.println(promoPrice);
Test.log(Status.INFO, "Discount price is: " + promoPrice);
String pcode = promoPrice.replace("- $", "");
System.out.println(pcode);
float DiscountPrice = Float.parseFloat(pcode);
Float Result1 = 0.0f;
Result1 = f - DiscountPrice;
System.out.println(Result1);
Test.log(Status.INFO, "Total Payable amount is: " + Result1);
}
reports.flush();
}
}
Comments
Post a Comment