Cypress E2E testing using keycloak-js

I use Cypress for E2E testing front end JS Apps. Cypress does not allow you to navigate to a different domain, other than the one being tested without adding the option "chromeWebSecurity": false to cypress.json config file.

By adding "chromeWebSecurity": false I can navigate to the keycloak login page, however cypress is then unable to interact with the login form.

I know some people will say why do you want to actually login when you can just pretend to login, but I need to be logged in, because my site relies on the functions provided by keycloak-js such as hasResourceRole().

How do other people do E2E testing with keycloak and continue to use the clients provided by keycloak, such as keycloak-js?

1 Like

We have done this with success (although have since migrated away from Keycloak to our in house SSO service).

Like you, chromeWebSecurity needs to be set to false, but otherwise it worked. Are you doing a redirect or popup? For redirect, the following worked fine for us…

cy.visit(Cypress.env("host")); // Our own site
  cy.get(".button").click(); // A login button that starts the process
  cy.get("#username") // The username field on the keycloak login page
    .type(Cypress.env("username"))
    .get("#password")
    .type(Cypress.env("password"));
  cy.get("#kc-login").click();
  cy.wait(300);
1 Like

We use cypress as well ( for the new keycloak admin client ) and we didn’t do anything in particular with to test the login see: GitHub - keycloak/keycloak-admin-ui: Keycloak Admin Console

I can login with cy.get('input[type=submit]').click();

But I got an error as below pic shows:


it said Cookie not found. Please make sure cookies are enabled in your browser.

I already set chromeWebSecurity to false in cypress.json and confirmed it in Cypress GUI window.