Mustela
Mustela

Reputation: 301

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?

Upvotes: 3

Views: 1168

Answers (3)

Mustela
Mustela

Reputation: 301

It was easy at the end :)

You just need to install chance as a node module.

npm install chance

Then require the node in the spec

// Load Chance
var Chance = require('chance');

And use it where ever you want

chance.email()

Enjoy!

Upvotes: 0

Brine
Brine

Reputation: 3731

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...

Upvotes: 3

Pseudonym
Pseudonym

Reputation: 2072

  1. Okay first you need to download chance.js and add that file to your html directory
  2. Second add script src="chance.js" in a proper script tag your section
  3. Within another script tag you should be able to use any of the functions that are listed on the website

working jsfiddle: http://jsfiddle.net/c96x2cpa/

fiddle js code:

alert(chance.bool());
alert(chance.character());
alert(chance.floating());

Upvotes: 0

Related Questions