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();
标签:e2e-testing,typescript,protractor,promise,javascript 来源: https://codeday.me/bug/20191118/2026579.html