我有一个问题,2个元素需要组合并检查结果与标签字符串。
firstName = element(by.model("emergencyContactObject.firstName"));
lastName = element(by.model("emergencyContactObject.lastName"));
firstName.clear().sendKeys("firstName");
lastName.clear().sendKeys("lastName");
firstNameText = firstName.getText().then(function(fnm){
return fnm;
});
browser.wait(firstNameText);
lastNameText = lastName.getText().then(function(lnm){
return lnm;
});
browser.wait(lastNameText);
nameText = firstNameText+' '+lastNameText;
var nameH3 = element(by.css('h3'));
expect(nameH3.getText()).toBe(nameText);
预期'frstName lastName'为'ManagedPromise :: 7999 {[[PromiseStatus]]:“pending”} ManagedPromise :: 8021 {[[PromiseStatus]]:“pending”}'
答案 0 :(得分:1)
问题是browser.wait();
无法解决您的承诺,从then
返回仍会返回承诺,而不是承诺的结果。不幸的是,你必须包装多个then()
,因为你有多个变量。
firstName.getText().then(function(fnm){
lastName.getText().then(function(lnm){
firstNameText = fnm;
lastNameText = lnm;
nameText = firstNameText + ' ' + lastNameText;
var nameH3 = element(by.css('h3'));
expect(nameH3.getText()).toBe(nameText);
});
});
或者,您可以使用async/await
,但必须标记您的功能async
。
var firstNameText = await firstName.getText();
var lastNameText = await lastName.getText();
var nameText = firstNameText + ' ' + lastNameText;
var nameH3 = element(by.css('h3'));
expect(nameH3.getText()).toBe(nameText);
答案 1 :(得分:-1)
我相信,您的问题在于您的代码的这一部分,您要为变量" firstNameText"分配承诺。而不是分配该承诺的结果:
firstNameText = firstName.getText().then(function(fnm){
return fnm;
});
尝试:
var firstNameText;
firstName.getText().then(function(fnm){
firstNameText = fnm;
});