Automatically generate field values ​​for form validation with Protractor

I want to test a simple user registration form using Protractor.

Here's my test:

describe('User Registration Page Tests', function() {
    beforeEach(function() {
        browser.get('/#/register');
    });

    it('user registration success', function(){
        element(by.model('user.organizationName')).sendKeys('someOrg');
        element(by.model('user.firstName')).sendKeys('some');
        element(by.model('user.lastName')).sendKeys('user');
        element(by.model('user.email')).sendKeys('some@user.com');
        element(by.model('user.password')).sendKeys('123456');
        element(by.model('confirmPassword')).sendKeys('123456');

        element(by.id('submitBtn')).click();

        browser.getCurrentUrl().then(function(url) {
            expect(url).toEqual('http://localhost:9001/#/success');     
        });
    });
});

      

For the first time, the test will pass. But next time it will fail because this user already exists.

Is there a way to automatically generate new field values ​​to solve this problem?

Couldn't find anything on google about this ...

+3


source to share


2 answers


Add the current timestamp to the values ​​for firstName and lastName as,



var randVal = Date.now()
............................
element(by.model('user.firstName')).sendKeys('some-' + randVal);
element(by.model('user.lastName')).sendKeys('user-' + randVal);
element(by.model('user.email')).sendKeys('some' + randVal + '@user.com');

      

+2


source


Can use this function to automatically generate username

var.autoGenerateUserName = function() {
        var autoGenerateUserName = "Auto-UserName-";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for (var i = 0; i < 3; i++)
            autoGenerateUserName += possible.charAt(Math.floor(Math.random() * possible.length));
        return  autoGenerateUserName;
    };

      

In this registration form, you can use something like this



element(by.model('user.organizationName')).sendKeys(autoGenerateUserName());

      

or

var userName = autoGenerateUserName();  

element(by.model('user.organizationName')).sendKeys(userName);

      

+1


source







All Articles