从PO发出承诺时遇到问题。在PO中,它可以正常工作,但在主测试页中,它给出未定义的值。在答应解决之前调用主页上的like函数
PO代码
this.Email = function (){
this.userIcon.getText(this.userIcon).then(function (text){
this.email=text.slice(5);
console.log(this.email);
return this.email
});
主测试页中的代码
email = data.Email();
抱歉,这是PO中的整个代码
var data = function(){
this.user = 'baseer@adnoc.ae';
this.pass = '123123';
this.usrform = element(by.css("input[formControlName=username]"));
this.passinput = element(by.css("input[formControlName=password]"));
this.button = element(by.tagName('button'));
this.text = 'Something smart'
this.dir=Math.floor((Math.random() * 3) + 1);
this.subID;
this.dropdown = element.all(by.css("mat-option"));
this.datepicker = element.all(by.xpath("//mat-datepicker-toggle[@class='mat-datepicker-toggle']"));
this.calendar = element(by.xpath("//div[@class='mat-calendar-header']"));
this.calendar = element(by.xpath("//tbody[@class='mat-calendar-body']"));
this.routeArea;
this.countryOfOrigin = element(by.xpath("//mat-select[contains(@formcontrolname,'countryOfOriginId')]"));
this.countryList = element(by.xpath("//div[@class='select-option-cont']"))
this.userProfileRoute = element(by.xpath("//mat-select[contains(@formcontrolname,'assignToUserProfileId')]"));
this.userIcon=element(by.xpath("//a[@class='mat-menu-item']"));
this.Email = function (){
this.userIcon.getText(this.userIcon).then(function (text){
this.email=text.slice(5);
console.log(this.email);
return this.email
});
};`enter code here`
};
module.exports = new data();
答案 0 :(得分:0)
您的代码应该已经
this.Email = function (){
return this.userIcon.getText(this.userIcon).then(function (text){
this.email=text.slice(5);
console.log(this.email);
return this.email
});
这就是原因-如果您不从函数中返回值,则无法重用
let my_func = function (a, b) {
let some_var = a + b
}
let result = my_func(1,2);
console.log(result) // undefined
因为您缺少return语句,所以如果您这样做
let my_func = function (a, b) {
let some_var = a + b
return some_var
}
let result = my_func(1,2);
console.log(result) // 3
如果您嵌套函数,也会发生同样的情况
let my_func_one = function (a, b) {
let some_var = a + b
return
}
let my_func_two = (a,b) {
my_func_one(a,b)
}
let result = my_func_two(1,2);
console.log(result) // undefined
之所以会这样,是因为您的外部函数不返回任何内容,而只是运行内部函数。但是如果你有
let my_func_one = function (a, b) {
let some_var = a + b
return some_var
}
let my_func_two = (a,b) {
return my_func_one(a,b) // return
}
let result = my_func_two(1,2);
console.log(result) // 3