Jerzy Gruszka
Jerzy Gruszka

Reputation: 1759

Use Page Object Pattern with promise in Protractor

I have two classes:

LayerWrapper
Layer

which are page objects.

I want to rework the method:

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

I want to refactor it, so that I would return a Layer page object:

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

it seems to be ok, but maybe this could be done in a better way ?

Upvotes: 3

Views: 450

Answers (1)

Optimworks
Optimworks

Reputation: 2547

Create an object function and declare all related page functions/methods in it and then do module.exports=new PageName(). This is best practice to send a page(Layer) object. You can follow below code:

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

Upvotes: 3

Related Questions