How to use fakerjs in Cypress

Viewed 9996

I am trying to use fakerjs in my cypress tests to randomly generate fake data for my forms. I have tried exporting it in support/index.js which did not work.

Is there any standard way to add fakerjs to all cypress specs instead of adding it in every spec file?

3 Answers

First off, what's wrong with importing it in every spec?

That being said, you can do this:

cypress/support/index.js:

cy.faker = require('faker');

your specs:

it(`test`, () => {
    const words = cy.faker.lorem.words();
});
For reference javascript :- 
Https://zetcode.com/javascript/fakerjs/

You can use these in cypress :-
npm i faker

const faker = require("faker"); 

let username = faker.name.findName()
let email = faker.internet.email()
let password = faker.internet.password()

Use all these in single specs file.

I tried it and found very simple. Steps are mentioned below,

1- require faker

const faker = require("faker");

2- User where you want like this

   Variable declaration -> var firstName = faker.Name.findName();
   Use in locator -> cy.get("#firstName").type(firstName);

I hope it would help :)

Related