Using random.js inside AngularJS tests end to end

I am trying to generate some random data for my e2e tests. It seems that the only library I found was chance.js . But can't get it to work. This is what I have 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

            });
        });

      

That's all I have so far ... any hint / idea?

+3


source to share


3 answers


First, install chance.js in your project:

npm install chance --save-dev

      

Your project has random.js installed as a node module. Then include it in your spec and instantiate it:

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

      



Then go to your spec. For example:

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

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

      

Hope it helps ...

+3


source


  • First you need to download the chance.js file and add this file to your html directory
  • Second add script src = "chance.js" to the correct script tag in the section
  • Inside another script tag, you can use any of the functions listed on the website

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



script js code:

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

      

0


source


It was easy at the end :)

You just need to install chance as a node module.

npm install chance

      

Then a node in the spec is required

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

      

And use it whenever you want

chance.email()

      

Enjoy!

0


source







All Articles