cypresscypress-origin

Cypress: The command was expected to run against origin


I try to realize multi-tabs test which is supported in Cypress 12 by changing the origin of test with cy.origin(). I use https://www.blender.org/ as my baseUrl set in config file, from the Blender main page I extract href to Instagram and change the origin to it. Cypress gives me the following error:

The command was expected to run against origin https://instagram.com but the application is at origin https://www.instagram.com.

Here what I do in the test:

 When('I change the origin of my test configuration', () => {
  cy.window().then((win) => {
    cy.stub(win, 'open').as('Open');
  });
  const url = Cypress.config('baseUrl');
  cy.visit(url);
  cy.window().scrollTo('bottom');
  var instaUrlString;
  cy.get('.social-icons__instagram')
    .invoke('attr', 'href')
    .then(($instaUrl) => {
      instaUrlString = $instaUrl.toString();
      cy.origin(instaUrlString, { args: instaUrlString }, (instaUrlString) => {
        cy.visit(instaUrlString);
        cy.wait(2000);
        cy.contains('Allow essential and optional cookies').click();
      });
    });
  cy.visit(url);
});

enter image description here

When I pass hardcoded string to cy.origin() it works fine. What am I doing wrong?


Solution

  • You are missing the www part of https://www.instagram.com. Cypress is comparing the protocol, path and port number but the paths are different.

    The shorthand version you have passed in via the link href is not acceptable in this situation. The DSN will resolve the shorthand, Cypress will not.

    You could create a function to correct the short version, but what is the point? Just add the correct and working parameter to your cy.origin() command.