如何在量角器测试中获得给定web元素的长度

时间:2017-12-14 14:16:49

标签: javascript testing protractor string-operations

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

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

投掷错误为Property 'length' does not exist on type'Promise <string>。有人可以帮我找到字符串的长度。

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

2 个答案:

答案 0 :(得分:0)

试试:

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

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

答案 1 :(得分: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.
    }
})