πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Scenarios
Scenario 94 of 100
E2E Scenarios
Advanced

Automating Payment Integrations inside Third-Party Popups

πŸ’³Scenario Overview

Automating Payment Integrations inside Third-Party Popups

Key Takeaways & Cheat Sheet

  • βœ“Monitor browser window counts as the payment flow initializes
  • βœ“Switch driver focus explicitly to the child payment gateway handle
  • βœ“Complete payment authorization inside the secure third-party gateway
  • βœ“Switch driver focus back to the parent merchant window

Short Direct Answer

Many sites handle checkout using third-party payment gateways (like PayPal or Stripe) that open in separate windows or tabs. To automate this, record the parent window handle, monitor and switch to the newly opened tab, submit payment details, close the tab, and switch back to the parent window context.

⚠️ Senior Warning (Red Flag)

Do not attempt to interact with checkout fields if a payment modal opens in a new window. You must switch the driver context explicitly, or all commands will fail with a NoSuchElementException.

πŸ’‘ STAR Deep Dive Explanation & Pro Tip

Always verify window counts before switching. If the payment gateway loads inside an iframe instead of a tab, use switchTo().frame() instead.

SeleniumAutomation.java
Selenium 4 + Java
// βœ… Store parent window handle
String parentWindow = driver.getWindowHandle();

// Click payment button that opens third-party dialog
driver.findElement(By.id("pay-via-paypal-btn")).click();

// βœ… Wait for new window to open and switch context
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(d -> d.getWindowHandles().size() > 1);

for (String windowHandle : driver.getWindowHandles()) {
    if (!windowHandle.equals(parentWindow)) {
        driver.switchTo().window(windowHandle);
        break;
    }
}

// Perform actions in secure third-party checkout screen
driver.findElement(By.id("paypal-email")).sendKeys("buyer@paypal.com");
driver.findElement(By.id("confirm-payment")).click();

// βœ… Close payment window and return to parent merchant page
driver.close();
driver.switchTo().window(parentWindow);
// Verify merchant success screen
assert driver.getTitle().contains("Order Confirmed");