Home > Enterprise >  Cypress Typescript: How do we get aliases' values out of cy.origin?
Cypress Typescript: How do we get aliases' values out of cy.origin?

Time:10-01

I have this scenario where I have to get a data from another origin and use that data to my baseUrl. How do I take the aliases out? I get an error whenever I call them. enter image description here

Here the script with cy.origin()

// first part of scenario

const credentials = {
            username: username,
            password: password,
        }
        cy.origin(Cypress.env(‘other’Url), { args: credentials }, ({ username, password }) => {
            cy.visit('/')
            cy.get('input[id="session_username"]').type(username)
            cy.get('input[id="session_password"]').type(password)
            cy.get('input[value="Log in"]').click()
            cy.wait(3000)
            // get details
            cy.get(‘<firstElementLocator>’).invoke('text').then((firstAlias) => {
                cy.wrap(firstAlias).as('firstAlias')
            })
            cy.get('<secondElementLocator>').invoke('text').then((secondAlias) => {
                cy.wrap(secondAlias).as('secondAlias')
            })
        })

// visit baseUrl

cy.visit('/')
cy.get(‘@firstAlias’).then((firstAlias) => {
    cy.get(‘@secondAlias’).then((secondAlias) => {
        var firstData = String(firstAlias)
        var secondData = String(secondAlias)
        cy.get(‘<thirdElementLocator>’).type(firstData)
        cy.get(‘<fourthElementLocator>’).type(secondData)
    })
})

After trying what @Fody answered (thank you Fody) there was no error in the scripts but when I tried to run it, I'm getting the error below.

enter image description here

Here is the script: cypress.config.ts

let data: any
let key: any
let value: any
module.exports = defineConfig({
  e2e: {
    async setupNodeEvents(on: (arg0: string, arg1: any) => void, config: any) {
      // implement node event listeners here
      const bundler = createBundler({
        plugins: [createEsbuildPlugin(config)],
     });
     on('task', {
      setValue(key:any, value:any) {
        data[key] = value
        return null
      },
      getValue(key:any) {
        return data[key] || null
      },
    })
      on('file:preprocessor', bundler);
      await addCucumberPreprocessorPlugin(on, config);
      
      return config;
      
    },
    experimentalSessionAndOrigin: true,

Step implementation:

cy.get('table > tbody > tr:nth-child(1) > td:nth-child(2)').invoke('text').then((generatedSMN) => {
            cy.task('setValue', { key: 'generatedSMN', value: String(generatedSMN) })
        })

CodePudding user response:

There were broad hints about improved interop between base domain and origin, but in the meantime here's a small work-around using tasks.

cypress.config.js

const { defineConfig } = require("cypress");

const data = {}

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        setValue({key, value}) {
          data[key] = value
          return null
        },
        getValue(key) {
          return data[key] || null
        },
      })
    },
    experimentalSessionAndOrigin: true,
  },
});

Test

cy.visit('http://example.com')

cy.origin('google.com', { args: credentials }, (credentials) => {
  cy.task('setValue', {key:'one', value:1})
})

cy.task('getValue', 'one')
  .as('firstAlias')
  .should('eq', 1)

CodePudding user response:

@Fody's answer is correct, but if you're having trouble implementing the task, try using a fixture instead:

cy.visit(domain1)

cy.origin(domain2, { args: credentials }, (credentials) => {
  ...
  cy.get(selector).invoke('text').then(text => {
    cy.writeFile('./cypress/fixtures/first.json', text)
  })
})

cy.fixture('first.json').as('firstAlias')

  • Related