1

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.

Dan Hodson
  • 309
  • 4
  • 18

1 Answers1

1

did you try using a protractor params config attribute?

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

Then u can access it by

browser.params.myid
Iamisti
  • 1,680
  • 15
  • 30
  • Thank you @lamisti, This is helping me to set that global variable. I am using `params: {recordId: 0},` then inside my 'it' I have `console.log("recordId " + browser.params.recordId);` which logs '0'. Then `element(by.css('.add-message')).evaluate('newEntry.id').then(function(id) { browser.params.recordId = id; console.log("ID = " + browser.params.recordId); return browser.params.recordId; });` which logs '2', then `console.log("recordId " + browser.params.recordId);` which logs '0' again. – Dan Hodson Jan 04 '16 at 12:24
  • How can I get the last log to be updated to the value of '2'? – Dan Hodson Jan 04 '16 at 12:24
  • the update should work. Are you sure you fecth global variables in the right time for your expect/console.log functions? In order to have the right value you should not prefetch the value for your describe blocks, just right before you want to use it. – Iamisti Jan 04 '16 at 12:29
  • I may be calling that last log wrongly, because the list of the 3 logs are: recordId 0, recordId 0, ID = 2, meaning that the one that 'should' be called last, is being called 2nd... – Dan Hodson Jan 04 '16 at 12:31
  • yes u do a promise.then and right after it you log it. So for the first run it'll run through your code and run your 3rd console.log, then it'll resolve your promise and log the 2 remaining logs inside the `then` callback – Iamisti Jan 04 '16 at 12:33
  • Thank you @lamisti, you're right. Using that console.log to check that the ID was indeed set correctly was being called before it should be (in my mind). But by creating a new spec called after the one where the ID was set, I was able to determine that it should be set to 2. Thanks again. I will accept your answer :) – Dan Hodson Jan 04 '16 at 12:41
  • don't forget to have the right order of executing your webtest files in the right order (to have it overwritten in the right place and time). Either you do a filesort by name and take care of the file namings, or do it by yourself. – Iamisti Jan 04 '16 at 12:43