Jasmine 如何将量角器中的承诺转换为字符串

Jasmine 如何将量角器中的承诺转换为字符串,jasmine,protractor,chai,chai-as-promised,Jasmine,Protractor,Chai,Chai As Promised,我对量角器和Jasmine有点陌生,我正在尝试检查使用getText()获取的元素列表是否包含特定的元素: 考虑以下因素 var productNameElements = element.all(by.css('.table-row')).getText(); elementToBeSearched = element(by.css('.table-row .table-row-child(1)')).getText(); 现在,由于上述两个变量都将返回一个承诺,因此通过执行以下操作

我对量角器和Jasmine有点陌生,我正在尝试检查使用getText()获取的元素列表是否包含特定的元素:

考虑以下因素

var productNameElements = element.all(by.css('.table-row')).getText();
    elementToBeSearched = element(by.css('.table-row .table-row-child(1)')).getText();
现在,由于上述两个变量都将返回一个承诺,因此通过执行以下操作:

expect(productNameElements).to.eventually.contain(elementToBeSearched);
它会失败,它确实会失败


因此,我相信将
元素转换成一个字符串将是有益的,并将使我的生活更轻松。请建议如何将
getText()
promise转换为字符串的解决方案。谢谢

让我们假设元素是ele。所以你应该解决承诺的方式是-

ele.getText().then(function(str){
expect(someOtherElement.getText()).toBe(str);
})
然后为你解决承诺。在与
expect
进行比较之前,可以通过输入
console.log(str)
来确认字符串。
PS:expect括号内的承诺将自动解析。

假设元素为ele。所以你应该解决承诺的方式是-

ele.getText().then(function(str){
expect(someOtherElement.getText()).toBe(str);
})
然后为你解决承诺。在与
expect
进行比较之前,可以通过输入
console.log(str)
来确认字符串。
PS:expect括号内的承诺会自动得到解决。

我所做的基本上是相似的:

productNameElements=element.all(by.css('.table row')).getText().then(函数(名称){ 预期(productNameElements).to.finally.contain(名称); });


这似乎对我起到了作用,因为我还使用控制台日志检查了“name”的值,我所做的与此基本相似:

productNameElements=element.all(by.css('.table row')).getText().then(函数(名称){ 预期(productNameElements).to.finally.contain(名称); });


这似乎对我起到了作用,因为我还使用控制台日志检查了“name”的值,下面的代码对我有效:

let txt = (await elementToBeSearched.then()).toString();

以下代码适用于我:

let txt = (await elementToBeSearched.then()).toString();