在Protractor中使用具有承诺的页面对象模式

时间:2016-08-30 08:56:15

标签: javascript typescript promise protractor e2e-testing

我有两个班级:

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>();
}

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

1 个答案:

答案 0 :(得分:3)

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

  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();