0

我想找出我从屏幕上得到的变量的长度。如果我使用

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

它是抛出和错误Property 'length' does not exist on type'Promise <string>。有人可以帮我找到字符串的长度吗?

我还想知道如何在量角器测试中使用字符串操作。在这种情况下,我想知道,如果字符串第一个索引是“字符或数字”,第二个索引是“字符或数字”。我想通过使用字符串的长度来做到这一点。

4

2 回答 2

0

试试看:

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

您可以使用正则表达式解决的第二个问题

于 2017-12-14T14:19:30.463 回答
0

使用count()Promise 中元素数量的方法。

现在针对您的具体问题,您应该执行以下操作:

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.
    }
})
于 2017-12-14T14:29:06.763 回答