This test case checks whether a valid coupon code is successfully applied to a booking cart, the total is updated accordingly, and a success message is displayed.
package com.seleniumExamples2024;
public class BookingCart {
private double total;
private String confirmationMessage;
public void addItem(String itemName, double itemPrice) {
// Simulated logic to add an item to the cart
// For simplicity, the item price is directly added to the total
total += itemPrice;
}
public boolean applyCoupon(String couponCode) {
// Simulated logic to apply a coupon
if ("VALIDCODE123".equals(couponCode)) {
// For simplicity, assuming the coupon provides a 10% discount
total *= 0.9;
confirmationMessage = "Coupon applied successfully";
return true;
} else {
confirmationMessage = "Invalid coupon code";
return false;
}
}
public double getTotal() {
return total;
}
public String getConfirmationMessage() {
return confirmationMessage;
}
}
}
package com.seleniumExamples2024;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class CouponCodeValidationTest {
@Test
public void testValidCouponCodeApplication() {
// Step 1: Add an item to the booking cart (simulated action)
BookingCart bookingCart = new BookingCart();
bookingCart.addItem("Hotel Room", 100.0);
// Step 2: Enter a valid coupon code
String validCouponCode = "VALIDCODE123";
// Step 3: Apply the coupon code
boolean couponApplied = bookingCart.applyCoupon(validCouponCode);
// Assert that the coupon was applied successfully
Assert.assertTrue(couponApplied, "Coupon should be applied successfully");
// Soft assertions for floating-point comparison
SoftAssert softAssert = new SoftAssert();
// Step 4: Validate the total value after applying the coupon
double expectedTotal = 90.0;
double actualTotal = bookingCart.getTotal();
double delta = 0.001;
// Use softAssert for floating-point comparison
softAssert.assertTrue(Math.abs(expectedTotal - actualTotal) < delta, "Booking total should reflect the discount");
// Step 5: Validate the confirmation message
String expectedConfirmationMessage = "Coupon applied successfully";
String actualConfirmationMessage = bookingCart.getConfirmationMessage();
// Assert that the confirmation message is as expected
softAssert.assertEquals(actualConfirmationMessage, expectedConfirmationMessage, "Incorrect confirmation message");
// Perform final assertion to fail the test if any of the previous assertions failed
softAssert.assertAll();
}
}
Comentarios