【问题标题】:Use Page Object Pattern with promise in Protractor在 Protractor 中使用带有 Promise 的页面对象模式
【发布时间】:2017-01-06 11:16:32
【问题描述】:

我有两个班级:

LayerWrapper
Layer

哪些是页面对象。

我想重做方法:

export class LayerPanel {
    public static layers = element.all(by.automationId('layer'));

    public static findLayerByName(layerName: string): Promise<boolean> {
        return this.layers.filter((elem) => {
            return elem.getText().then(text => {
                return text === layerName;
            });
        }).first().then(this.OK, this.Failed);
     }

    private static OK() {
        return new Promise<true>();
    }

    private static Failed {
        console.log('not found');
    }
}

我想重构它,以便返回一个 Layer 页面对象:

public static findLayerByName(layerName: string): Promise<Layer> {
    return this.layers.filter((elem) => {
        return elem.getText().then(text => {
            return text === layerName;
        });
    }).first().then(this.OK, this.Failed);
}

private static OK() {
    return new Promise<Layer>();
}

似乎没问题,但也许这可以以更好的方式完成?

【问题讨论】:

  • 您确定您的代码正常工作吗?似乎您的承诺工作不应该奏效。
  • 你能说出原因吗?
  • 你构造了一个Promise 的新实例而没有传递所需的函数,应该是:new Promise&lt;true&gt;((resolve, reject) =&gt; { ... }),这对我来说很奇怪,但也许你正在使用一个支持它的 Promise 实现跨度>

标签: javascript typescript promise protractor e2e-testing


【解决方案1】:

创建一个对象函数并在其中声明所有相关的页面函数/方法,然后执行 module.exports=new PageName()。这是发送页面(层)对象的最佳实践。您可以按照以下代码:

  var LayerPanel function(){

  this.layers = element.all(by.automationId('layer'));

  this.findLayerByName=function(layerName){
               return this.layers.filter((elem) => {
                  return elem.getText().then(text => {
                          return text === layerName;
                     });
   }).first();
 };

 this.OK() {
    return new Promise<true>();
   }

 this.Failed {
  console.log('not found');
   }    
};

module.exports = new LayerPanel();

【讨论】:

  • 但我想从 findLayerByName 返回一个新层。我在 typescript 导出类中的导出功能
  • @JerzyGruszka,我已经更新了代码,它将重新运行与给定 layerName 匹配的元素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多