How do you work with several domains through cy.origin?
cy.origin runs a callback in the context of another origin, because the same-origin policy stops a test from driving a page of a foreign origin directly. Commands for the foreign page go inside the block, and outside variables do not reach it: they are passed through args and must be serialisable. In practice an external login is usually bypassed through the API and cy.session, and cy.origin is kept for one test.
Cypress · Sessions and isolation
After clicking “Sign in with SSO” the page changed its origin and the next command fails with an origin error. What now?
A login through an external provider starts on the app’s site, goes to the provider’s form and comes back. Plain cy.get commands on the provider’s page raise an error although the element is visible. For such scenarios there is cy.origin. The lesson explains why, how to pass data into the block and where it is better not to use it.
How it works and why. The browser forbids a page of one origin to read the DOM of a page of another origin (the same-origin policy). A Cypress test runs together with the page of its own origin, so after navigating to another origin it cannot drive the new page directly. cy.origin(url, options, callback) runs the callback in the context of the other origin: Cypress attaches to that page and commands inside the block act there. Since Cypress 14 “another origin” means any other one, including a subdomain, not only a different top-level domain. The callback runs in another context, so outer variables do not reach it: pass data explicitly through args, and it must be serialisable (plain values, objects, arrays, but not functions).
The path of a test between origins
Example: SSO login.
const user = { email: 'qa@test.com', password: 'secret' };
cy.visit('/');
cy.get('[data-cy=sso-login]').click();
// commands for the provider: only inside cy.origin
cy.origin(
'https://auth.provider.com',
{ args: user },
({ email, password }) => {
cy.get('#email').type(email);
cy.get('#password').type(password, { log: false });
cy.get('button[type=submit]').click();
},
);
// back in the primary origin
cy.url().should('include', '/dashboard');
If email and password were just variables nearby, the callback would not see them: they do not exist in it. That is why they travel through args and arrive as a parameter. After the block the provider redirects to the app and the following commands work in the primary origin again.
Limits and alternatives. Custom commands and imports of an outer file do not work inside cy.origin: they are attached inside the callback through Cypress.require(), which needs the experimental experimentalOriginDependencies option turned on in the config. The block should not be used in every test: it is slow, and the provider’s page (captcha, MFA, markup changes) is not under your control. A practical approach: one test checks the SSO flow itself through cy.origin, and the rest log in directly through the API and cy.session.
There is no such limit: the test simply navigates to another origin and all commands work as usual. Several tabs and contexts within one test are also normal.
Common mistakes.
- Using an outer variable in the callback: it is undefined because the block runs in another context.
- Calling
cy.getfor the foreign origin outsidecy.origin: Cypress complains about the origin, not about a missing element. - Passing functions or class instances in
args: they are not serialisable. - Going through
cy.originfor every page of the app: slow and dependent on a page you do not own.
Check yourself
A test declares const token = 'abc' and cy.origin(...) uses token without args. What happens?
Answer
The callback runs in another context, so token does not exist in it and an error occurs. The value must be passed as { args: { token } } and received as a parameter.
Is cy.origin needed to go from app.example.com to auth.example.com?
Answer
Yes. Since Cypress 14 a subdomain counts as another origin, so commands on the auth.example.com page go inside cy.origin.
cy.origin: commands for the foreign origin are written inside this block. Outer variables do not get there, they are passed through args. Middle/Senior: In practice I bypass the external login through the API and cy.session more often and keep cy.origin for one test that checks the flow itself. That way the suite does not depend on a foreign page with captcha or MFA, and the SSO flow is still covered.