How can I mock API response to login in Playwright testing?

2.8k Views Asked by At

I'm really new to playwright and I wish to skip the authentication part for a certain internal website. This authentication requires OTP login and I have tried all the solutions that could have worked I found through youtube or research.

This is my test case:

@Test
 public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
                    .setHeadless(false).setSlowMo(5000));
            BrowserContext context = browser.newContext(new Browser.NewContextOptions().setStorageStatePath(Paths.get("responses.json")));

            Page page = context.newPage();
            page.navigate("http://localhost:3000/trader-desktop/login");
        }
}

This is what I found in localStorage while inspecting on chrome, so I added this in the responses.json file

{
"token" : "erfbervje..........",
"tokenType" : "type",
"tokenExpirationTime" : "timestamp", 
"userId" : "745769548",
"realms" : [
 {
 "userRealmId": 45,
      "primaryRealm": false,
      "parentRealmId": 2,
      "parentRealms": [
        {
          "userRealmId": 9,
          "primaryRealm": false,
          "parentRealmId": null,
          "parentRealms": null,
          "realmName": "OnboardFlowTest",
          "asgardUserId": null,
          "role": null,
          "realmIdentifier": "dvds67264bhjd-dfvdfjhb-vfdvd",
          "systemUserId": 56
        }
  ]
},
  "Another Json array"
 ]
 }
0

There are 0 best solutions below