Dan Hodson
Dan Hodson

Reputation: 309

Protractor reuseable values

I am having a bit of trouble with getting values from Protractor testing and being able to reuse those values.

I have an app that creates new records from a form and then displays them back to the user. On a successful addition, the user is presented with a success alert, displaying the ID of the newly created record. "You have successfully added an entry with the ID {{newEntry.id}}".

I have a suite of tests asserting that all the fields are correctly validated etc, which all work correctly. I now want to test the Update side of things by taking the newly created record and testing if a new set of values updates correctly. Therefore I want to take that ID of the newly created record and reuse it.

I have created the variable ID at the top of my suite,

var id;

I then run all the validation tests on the form and submit a correct submission. I then check if the success message is shown and that, in this instance, the ID = 2.

describe('Add users', function() {

    var endpoint = "users";
    var id;

    correctSubmission(endpoint, id);

    function correctSubmission(endpoint, id) {
        describe('Testing correct submission', function() {
            it('should navigate back to the list page', function() {
                expect(browser.getCurrentUrl()).toBe("list/" + endpoint);
            });
            it('should display a success message', function() {
                expect(element(by.css('.alert-success')).isPresent()).toBeTruthy();
            });
            it('should get the record ID from the success message', function() {
                expect(element(by.css('.add-message')).evaluate('newEntry.id')).toEqual(2);
                id = element(by.css('.add-message')).evaluate('newEntry.id');
                return id;
            });
        });
    };
});

I need to basically get that ID that equals 2 and return it back to the Global ID so that I can use it across other tests. Obviously that ID is currently an unresolved promise, and I have tried to use:

protractor.promise.all(id).then(function (result) {
    console.log("ID is: " + result);
});

But this only logs the string.

I am a bit lost with what to do next as I have tried all sorts, but to no avail and I am pushed for time on this project.

Many thanks if you can help this Protractor n00b.

Upvotes: 1

Views: 370

Answers (1)

Iamisti
Iamisti

Reputation: 1710

did you try using a protractor params config attribute?

exports.config = {
    params: {
        myid: 'somevaluehere'
    }
};

Then u can access it by

browser.params.myid

Upvotes: 1

Related Questions