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

Executing API + UI Hybrid Testing Flows

πŸ”ŒScenario Overview

Executing API + UI Hybrid Testing Flows

Key Takeaways & Cheat Sheet

  • βœ“Use REST-Assured to send fast backend API setup requests
  • βœ“Extract session tokens or cookies from API responses
  • βœ“Inject extracted cookies directly into the browser driver instance
  • βœ“Bypass slow UI forms by leveraging fast backend API endpoints

Short Direct Answer

API + UI hybrid testing speeds up execution by avoiding slow browser workflows for setup tasks. Use libraries like REST-Assured to authenticate via API and retrieve session cookies. Then, inject those cookies directly into your WebDriver session, allowing you to bypass the login UI and load the target dashboard instantly.

⚠️ Senior Warning (Red Flag)

Do not log in using the UI before every single test. Logging in via UI is slow and adds minutes of overhead. Authenticate via API, inject the session cookies, and jump straight to the page under test.

πŸ’‘ STAR Deep Dive Explanation & Pro Tip

Before injecting cookies via Selenium, you must load the target domain (even a 404 page) first. Selenium throws an InvalidCookieDomainException if you attempt to add cookies to an uninitialized domain.

SeleniumAutomation.java
Selenium 4 + Java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.openqa.selenium.Cookie;

public void loginViaApiAndLaunchUi() {
    // 1. Fetch authentication cookies via REST-Assured API
    Response response = RestAssured.given()
        .formParam("username", "admin")
        .formParam("password", "secret123")
        .post("https://api.careerraah.com/login");
        
    String sessionToken = response.getCookie("SESSIONID");

    // 2. Load domain page in browser first to establish cookie context
    driver.get("https://careerraah.com/404");

    // 3. Inject cookie into Selenium WebDriver
    Cookie cookie = new Cookie("SESSIONID", sessionToken, ".careerraah.com", "/", null);
    driver.manage().addCookie(cookie);

    // 4. Load the dashboard page directly (login form is completely bypassed!)
    driver.get("https://careerraah.com/dashboard");
}