cypresscypress-origin

Cypress origin command using dynamic data in a secondary origin


I am trying to follow the following code from the Cypress documentation.

cy.origin(
  'supersecurelogons.com',
  // Send the args here...
  { args: sentArgs },
  // ...and receive them at the other end here!
  ({ username, password }) => {
    cy.visit('/login')
    cy.get('input#username').type(username)
    cy.get('input#password').type(password)
    cy.contains('button', 'Login').click()
  }
)

The code that I am using is below. My problem is that both pathname & search are undefined, this means that the visit command is failing.

I was wondering what it is that I am doing wrong here, any help gratefully received.

const workaroundUrl = new URL('https://www.validurl.com/pathname?search=true');
const url = { pathname: workaroundUrl.pathname, search: workaroundUrl.search }
cy.origin(workaroundUrl.origin, { args: { url } }, ({ pathname, search }) => {
  cy.visit(pathname + search);
});

I am using v10.4.0.


Solution

  • I think you need to match up the brackets (send and receive) like this

    const workaroundUrl = new URL('https://www.validurl.com/pathname?search=true');
    const url = { pathname: workaroundUrl.pathname, search: workaroundUrl.search }
    
    cy.origin(workaroundUrl.origin, { args: {url} }, ({url}) => {
    
      const { pathname, search } = url;  // destructure the url arg
    
      cy.visit(pathname + search);
    });
    

    That would make the example you mentioned incorrect.

    I've used this pattern successfully origin - Usage

    cy.origin('https://www.acme.com', { args: { hits } }, ({ hits }) => {