javascript - Selenium/WebdriverJs/Protractor promise chaining with page objects -


i implementing page object pattern in protractor/selenium.

since every method in protractor returns promise, consitency methods in page object should return promises.

further more page objects may have functions return page object or custom of page object (like leftnavigation, maincontent). instead of returning page object itself, page object should returned within promise. not undestand how that.

additionally chain method calls without using .then(..) method. webelements possible call further functions without calling .then(..) method, e.g.

browser.driver.findelement(by.css('#someid')).findelement(by.css('#somebutton')).click(); 

i achieve page object pattern:

let pagepromise = adminbasebage.get(); // returns promise<adminbasepage> let mcontent = page.maincontent;// should return promise<maincontent> let titlepromise = mcontent.getmoduletitle(); // returns promise<string> 

or better

adminbasebage.get().maincontent.getmoduletitle(); 

below extract of pageobjects questions here:

adminbasepage.js

var leftnavigation = require('../../pageobject/leftnavigation.js'); var maincontent = require('../../pageobject/maincontent.js');  class adminbasepage {      constructor() {         this._leftnavigation = new leftnavigation();         this._maincontent = new maincontent();     }      /**      * @returns {promise<adminbasepage>}      */     static getpage() {         return browser.driver.get("index.php").then(function() {             return new adminbasepage();         });     }      /**      * @returns <loginpage>      */     logout() {         this.leftnavigation.logout();         return new loginpage(); //also here return promise.     }      /**      * @returns {leftnavigation}      */     leftnavigation() {         //instead of return object directly, return promise here.          //but how?         return this._leftnavigation;     };      /**      * @returns {maincontent}      */     maincontent() {         //instead of return object directly, return promise here.          //but how?         return this._maincontent;     }; }  module.exports = adminbasepage; 

maincontent.js

class maincontent {      constructor() {          /** @type {webelementpromise} */         this._element_maincontent = this.webdriver.findelement(by.css('#maincontent'));      }       /**      * gets title of main content      *      * @returns {webdriver.promise.promise<string>}      */     getmaincontenttitle() {         return this._element_maincontent                    .findelement(by.id('moduletitle'))                    .gettext();     }  }  /** @type {maincontent} */ module.exports = maincontent; 

can give advice? hope somehow clear trying explain :-)

regards

you shouldn't try make pageobject promise. pageobject supposed method/property factory , shouldn't constraint in execution flow. keep simple returning element property rather trying locate elements in constructor:

describe('suite', function() {      it('should module title ...', function() {         let pageadmin = adminbasebage.get();         let mcontent = pageadmin.maincontent;         let titlepromise = mcontent.getmoduletitle();         expect(titlepromise).toequal('module title');     });  });   class maincontent {      constructor() {      }      element_module_title() { return element(by.css('#maincontent #moduletitle')); }      /**      * gets title of main content      *      * @returns {webdriver.promise.promise<string>}      */     getmoduletitle() {         return this.element_module_title.gettext();     }  } 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -