微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-在量角器中使用带有承诺的页面对象模式

我有两节课:

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

似乎还可以,但是也许可以用更好的方法来完成?

解决方法:

创建一个对象函数并在其中声明所有相关的页面函数/方法,然后执行module.exports = new PageName().这是发送page(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();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐