【问题标题】:Return a value from a promise within function to the test spec从函数内的承诺返回一个值到测试规范
【发布时间】:2018-11-19 20:46:10
【问题描述】:

我是 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 is undefined

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

感谢您的帮助

【问题讨论】:

    标签: protractor


    【解决方案1】:

    你在this.transactionNumValue之前错过了return,应该删除get().getText()之前的get()

    var transactionNumValue = element(by.id('transactionNumber'));
    
    public getTransactionNum(): any {
    
        return this.transactionNumValue.getText()
                   .then((transactionValue: string)=>{
                          return transactionValue;
                   });
    }
    

    【讨论】:

      【解决方案2】:
      var transactionNumValue = element(by.id('transactionNumber'));
      
      
      
      public getTransactionNum(): any { return transactionNumValue.getText().then((transactionValue: string) => { return transactionValue; });
      

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

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

      【讨论】:

        猜你喜欢
        • 2016-04-02
        • 2017-04-07
        • 1970-01-01
        • 1970-01-01
        • 2018-07-27
        • 2019-01-23
        • 2017-05-26
        • 2016-10-04
        • 2015-11-27
        相关资源
        最近更新 更多