Search code examples
javascripttestingprotractorstring-operations

how to get the length of the given web element in protractor tests


I would like to find out the length of the variable which I get from the screen. If I use

var driverID = element(by.id('driverID')).getAttribute('value') 
driverID.length

it is throwing and error as Property 'length' does not exist on type'Promise <string>. Can some one help me in finding the length of the string.

I would also like to know how to use string operations in protractor tests. In this case I want to know, if the string first index is 'character or a number' and the second index is 'character or a number'. I want to do this by using the length of the string.


Solution

  • use the count() method for the number of elements inside a Promise.

    Now for your specific problem, you should do something like:

    element(by.id('driverID')).getAttribute('value').then(function(attr) {
        var length = attr.length; // I don't really need why you need this length
        if (!isNaN(attr[0]) && !isNaN(attr[1])) {
            // 1st and 2nd characters of the string are numbers.
        }
    })