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 :

The error message is indicating that you are trying to visit a URL with origin "https://instagram.com" but the actual origin of the application is "https://www.instagram.com". To resolve this issue, you need to modify the URL you are visiting to match the actual origin of the application.

Try this:

 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();
          const updatedInstaUrl = instaUrlString.replace('instagram.com', 'www.instagram.com');
          cy.origin(updatedInstaUrl, { args: updatedInstaUrl }, (updatedInstaUrl) => {
            cy.visit(updatedInstaUrl);
            cy.wait(2000);
            cy.contains('Allow essential and optional cookies').click();
          });
        });
      cy.visit(url);
    });

Leave a Reply