Protractor: get item id

I had this with these asynchronous calls throughout the transporter and javascript in general. What used to be for two lines of code now is 10. Here is an example:

I am writing a protractor utility method to simply check certain DOM properties in a group of related divs and enter text fields. This is for the validation framework I'm working on. The idea is to pass the protractor element to this method, and then based on this element identifier check certain DOM properties on the appropriate divs and enter text fields. Here's how I earned it:

/**
 * Checks for error in a NUAF field 
 * @param {String or Element} field . 
 * @param {string} errorText expected validation error text to appear in tooltip
 */
exports.checkForError = function (field, errorText) {

    var innerCheck = function(fieldId) {
        expect(fieldId).not.toBe(undefined);
        var elmntd = element(by.id('divInput.'+fieldId));
        expect(elmntd).not.toBe(null);
        expect(elmntd.getAttribute('tooltip')).toContain(errorText);
        expect(exports.hasClass(element(by.id('prnt.'+fieldId)), 'has-error')).toBe(true);
    };

    // this unbelievably complex block of code gets the id of the 
    // field argument.  If string was passed, the fieldid is just that .
    if (typeof field === 'string') {
        innerCheck(field);
    } else {
        //what used to be field.id now needs 6 lines of code?
        field.getAttribute('id').then(
            function(idAttribute) { 
                console.log( "*********: "+idAttribute );
                innerCheck(idAttribute);
            }
        ); 
    }
};

Question: Is there a better, less detailed way to write a block of code field.getAttribute('id').then. It's just embarrassing to write all this to get the item id.

+4
1

... , innerCheck :

// this unbelievably complex block of code gets the id of the 
// field argument.  If string was passed, the fieldid is just that .
if (typeof field === 'string') {
    innerCheck(field);
} else {
    field.getAttribute('id').then(innerCheck); 
}

+3

All Articles