Randi
Randi

Reputation: 81

Cypress - Add a text to input area using lorem-ipsum

I am adding a random text to input using lorem-ipsum in cypress. Does anyone know how to handle this in cypress?

I am using following method and going to call addComment() method in the test

import { loremIpsum } from "lorem-ipsum";
addcomment() {


 loremIpsum({
    count: 1, // Number of "words", "sentences", or "paragraphs"
    format: "plain", // "plain" or "html"
    paragraphLowerBound: 3, // Min. number of sentences per paragraph.
    paragraphUpperBound: 7, // Max. number of sentences per paragarph.
    random: Math.random, // A PRNG function
    sentenceLowerBound: 5, // Min. number of words per sentence.
    sentenceUpperBound: 15, // Max. number of words per sentence.
    suffix: "\n", // Line ending, defaults to "\n" or "\r\n" (win32)
    units: "sentences", // paragraph(s), "sentence(s)", or "word(s)"

  })
  cy.get('.input > textarea').loremIpsum()
}

Upvotes: 1

Views: 345

Answers (2)

Randi
Randi

Reputation: 81

Install the plugin

npm i lorem-ipsum

Then use the above code mentioned by Alapan Das.

My code

import {LoremIpsum} from 'lorem-ipsum'

addcomment2() {
const lorem = new LoremIpsum({
    sentencesPerParagraph: {
      max: 8,
      min: 4
    },
    wordsPerSentence: {
      max: 16,
      min: 4
    }
  });
  
 
 let txtComment = lorem.generateSentences(3);
 

cy.get(".input > textarea").type(txtComment);

}

Upvotes: 0

Alapan Das
Alapan Das

Reputation: 18634

Assuming you have already installed the plugin. Then your test should look like this:

import {LoremIpsum} from 'lorem-ipsum'
const lorem = new LoremIpsum({
  sentencesPerParagraph: {
    max: 8,
    min: 4,
  },
  wordsPerSentence: {
    max: 16,
    min: 4,
  },
})

describe('test suite', () => {
  it('Test case', () => {
    //Types 5 Sentences
    cy.get('.input > textarea').type(lorem.generateSentences(5))

    //Types 7 Paragraphs
    cy.get('.input > textarea').type(lorem.generateParagraphs(7))
  })
})

Upvotes: 1

Related Questions