top of page

learner'sBlog

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

2 views0 comments

General Shortcuts:

  • Ctrl + C: Copy

  • Ctrl + X: Cut

  • Ctrl + V: Paste

  • Ctrl + Z: Undo

  • Ctrl + Y: Redo

  • Ctrl + A: Select All

  • Ctrl + S: Save

  • Ctrl + P: Print

  • Ctrl + F: Find Windows Shortcuts:

  • Windows Key: Open or close Start Menu

  • Windows Key + D: Show or hide the desktop

  • Windows Key + E: Open File Explorer

  • Windows Key + L: Lock the computer

  • Alt + Tab: Switch between open applications

  • Windows Key + Tab: Open Task View Browser Shortcuts:

  • Ctrl + T: Open a new tab

  • Ctrl + W: Close the current tab

  • Ctrl + Shift + T: Reopen the last closed tab

  • Ctrl + Tab: Switch between open tabs

  • Ctrl + N: Open a new browser window

  • F5 or Ctrl + R: Refresh the page Word Processing Shortcuts:

  • Ctrl + B: Bold

  • Ctrl + I: Italicize

  • Ctrl + U: Underline

  • Ctrl + Left Arrow/Right Arrow: Move cursor to the beginning/end of a word

  • Ctrl + Home/End: Move cursor to the beginning/end of a document

  • Ctrl + Shift + Arrow Keys: Select text File Explorer Shortcuts:

  • Alt + Up Arrow: Go up one level

  • Ctrl + Shift + N: Create a new folder

  • F2: Rename selected item

  • Ctrl + Click: Select multiple non-contiguous items

  • Ctrl + Shift + Esc: Open Task Manager Multitasking Shortcuts:

  • Alt + F4: Close the active window

  • Alt + Space + N: Minimize the active window

  • Alt + Space + X: Maximize the active window

  • Windows Key + Number (1-9): Open or switch to the application in the taskbar

3 views0 comments




Serialization

Deserialization

  ObjectOutputStream class is used to serialize an object to a byte stream.

ObjectInputStream class is used to deserialize a byte stream into an object

Commonly used for saving object states persistently, transmitting objects over a network, or sharing data between different applications.

Crucial for scenarios where data, previously serialized, needs to be retrieved and used in its original object form. Common use cases include reading objects from files, receiving serialized data over a network, or reconstructing objects stored in databases.

Object to Byte Stream Example

// Serialize an object to a byte stream
ByteArrayOutputStream bytArrOPStream = new ByteArrayOutputStream();
ObjectOutputStream objOPStream = new bjectOutputStream(bytArrOPStream);

// Example object to be serialized
MyObject myObject = new MyObject();

// Serialize the object
objOPStream.writeObject(myObject);

// Get the byte stream
byte[] byteStream = bytArrOPStream.toByteArray();

// Close streams
objOPStream.close();
bytArrOPStream.close();

Byte Stream to Object Example

// Deserialize an object from a byte stream
ByteArrayInputStream bytArrIPStream = new ByteArrayInputStream(byteStream);
ObjectInputStream objIPStream = new ObjectInputStream(bytArrIPStream);

// Deserialize the object
MyObject deserializedObject = (MyObject) objIPStream.readObject();

// Close streams
objectInputStream.close();
bytArrIPStream.close();

3 views0 comments
bottom of page