Struggling to extract values from Protractor testing for reuse? No worries, I've got you covered!
Imagine this scenario: you have an app that generates new records through a form and then showcases them to the user. Upon successful addition, a message pops up with the ID of the recently created record, saying "You have successfully added an entry with the ID {{newEntry.id}}". All good so far.
You already have a bunch of tests in place to validate fields correctly, but now it's time to delve into updating those records. To do so, you need to grab the ID of the newly created record and utilize it for your next set of tests.
To kick things off, let's create a variable called 'ID' at the beginning of our test suite:
var id;
After running all validation tests and submitting a correct form, we end up checking if the success message is displayed, with the ID in this instance being 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;
});
});
};
});
Great! We managed to capture the ID as 2, but here comes the tricky part - turning that ID into a globally usable value for all subsequent tests. The catch is, the current ID is trapped as an unresolved promise. You attempted using `protractor.promise.all(id)` to resolve it, only to end up logging the string instead.
Feeling a bit lost? Don't worry; we've all been there. With project deadlines looming, the pressure can be intense. Hang tight, keep experimenting, and remember that every protractor started as a n00b at some point!
Good luck on your Protractor journey!