从函数内的promise返回值到测试规范

时间:2018-11-19 20:46:10

标签: protractor

我是Protractor的新手,我遇到了从Angular表单文本字段获取文本值,然后尝试使用该值稍后在同一测试规范中执行操作的问题。

这是测试规范代码:

it('Demo Test, () => {

        // Submit the form and get the transaction number
        var submitButton = element(by.id('submit'));
        submitButton.click(); 

        // get the generated transaction number from the screen
        let transactionNum = confirmPage.getTransactionNum();


        // would like to use the transActionNum variable to sendkeys into a 
        // text element
        var searchInput = element(by.id('search'));
        searchInput.sendkeys(transactionNum);                
});

以下是一些来自类的具有“ getTransactionNum”功能的代码

var transactionNumValue = element(by.id('transactionNumber'));

public getTransactionNum(): any {

    this.transactionNumValue.get().getText().then((transactionValue: string) 
 => {
    return transactionValue;
});

当我运行测试规范并且测试试图在变量'transactionNum'中键入值时,我得到' transactionNum未定义'

我想将交易号作为文本值返回给测试规格,与承诺相反。

感谢帮助

2 个答案:

答案 0 :(得分:1)

您错过了return之前的this.transactionNumValue,因此应删除get()之前的get().getText()

var transactionNumValue = element(by.id('transactionNumber'));

public getTransactionNum(): any {

    return this.transactionNumValue.getText()
               .then((transactionValue: string)=>{
                      return transactionValue;
               });
}

答案 1 :(得分:0)

var transactionNumValue = element(by.id('transactionNumber'));



public getTransactionNum(): any { return transactionNumValue.getText().then((transactionValue: string) => { return transactionValue; });

在您的类构造函数中,定义量角器的ElementFinder类型的locotors。

如果要返回的元素数组可能是$$elements.all,则使用量角器的类型ElementArrayFinder

相关问题