0

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!

2
  • 1
    What do you want to do with this text ? You can access it simply by using this.dummyText eg. cy.type(this.dummyText) Commented Nov 8, 2021 at 9:20
  • I want to use it in cy.type(), I've updated the question, check it out :) Commented Nov 8, 2021 at 9:20

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

it worth mentioning that you can't just write cy.generateDummyText(280); cy.type(this.dummyText) since the cypress commands are asynchronous
0

You can get the value either by alias:

cy.generateDummyText(280); cy.get("@dummyText").then(dummyText => { cy.get('myinput').type(this.dummyText) }) 

or directly by the command return value:

cy.generateDummyText(280).then(dummyText => { cy.get('myinput').type(this.dummyText) }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.