Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use Cypress Variable From Command

I have a utility command in cypress:

Cypress.Commands.add('generateDummyText', (length: number, delay?: number) => {
  const wordLength = 5;

  const lorem = new LoremIpsum({
    wordsPerSentence: {
      max: wordLength,
      min: wordLength,
    },
    words: ['word1', 'word2', 'word3', 'word4', 'word5'],
  });

  const words = lorem.generateWords(Math.ceil(length / wordLength));

  cy.wrap(words).as('dummyText');
});
it('test', () => {
  // Generate dummy text
  cy.generateDummyText(280);

  // I want to type the "dummyText variable here"
  cy.get('myinput').type()
});

How can this be done? I don’t need to use a custom cypress command, but I’d prefer to use one. Looking forward to your reply!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can access it just by using:

cy.type(this.dummyText)

Another option is just to use faker to generate the random words for you.
Add the faker module to you project, then you can just do:

const faker = require("faker"); 

.type(faker.random.words(5));

instead of using a custom command for it

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading