0

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
VonNet
  • 71
  • 1
  • 1
  • 13

2 Answers2

0

try that:

var driverID = element(by.id('driverID')).getAttribute('value');
driverID.then((attribute) => {
    console.log(attribute.length)
});

the second issue you can resolve using regex

Kacper
  • 1,201
  • 1
  • 9
  • 21
0

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.
    }
})
eLRuLL
  • 18,488
  • 9
  • 73
  • 99
  • thanks a lot for the solution. Can you please let me know how to get the substring in protractor tests? – VonNet Dec 14 '17 at 14:45
  • 1
    the `attr` is already a string, we already resolved the Promise and you can use direct javascript with that attribute, check [this](https://www.w3schools.com/jsref/jsref_substring.asp) – eLRuLL Dec 14 '17 at 14:50