Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using chance.js inside AngularJS end to end tests

I'm trying to generate some random data for my e2e tests. Seems that the only library I found was chance.js. But can't make it works. This is what I've tried so far:

describe('In the login page', function () {
  var chance = new Chance(); // ReferenceError: Chance is not defined

}

Then adding

beforeEach(function(){

        browser.executeScript(
            function() {
                return chance;
            }).then(function(_chance){
                chance = _chance; //It returns an object, but can't use any of the methods.

            });
        });

But if I try

beforeEach(function(){

        browser.executeScript(
            function() {
                return chance.email(); //Note this line here
            }).then(function(_chance){
                chance = _chance; //It returns an email

            });
        });

Thats all I have so far... any clue/idea?

like image 497
Mustela Avatar asked Apr 13 '26 08:04

Mustela


1 Answers

First, install chance.js in your project:

npm install chance --save-dev

This installs chance.js as a node module in your project. Then include it in your spec and instantiate:

var chance = require('../node_modules/chance').Chance();

Then call in your spec. For example:

it('should add a new friend', function() {
    var friendName = chance.string()
    friendPage.addFriend(friendName);

    expect(friendPage.inResults(friendName)).toBeTruthy();
});

Hope that helps...

like image 66
Brine Avatar answered Apr 16 '26 02:04

Brine