javascript - Selenium/WebdriverJs/Protractor promise 与页面对象链接

标签 javascript selenium promise chaining

我目前正在 Protractor/selenium 中实现页面对象模式。

由于 Protractor 中的每个方法都返回一个 promise ,为了一致性,我的页面对象中的方法也应该返回 promise 。

此外,我的页面对象可能具有返回另一个页面对象或自定义页面对象(如 LeftNavigation、MainContent)的函数。页面对象应该在 promise 中返回,而不是返回页面对象本身。目前我真的不明白该怎么做。

另外,我想在不使用 .then(..) 方法的情况下链接我的方法调用。对于 WebElements,可以在不调用 .then(..) 方法的情况下调用更多函数,例如

browser.driver.findElement(By.css('#someid')).findElement(By.css('#somebutton')).click();

我也想用页面对象模式来实现:

let pagePromise = AdminBaseBage.get(); // returns a Promise<AdminBasePage>
let mContent = page.mainContent;// should return a Promise<MainContent>
let titlePromise = mContent.getModuleTitle(); // returns a Promise<string>

甚至更好

AdminBaseBage.get().mainContent.getModuleTitle();

下面是我的 PageObjects 的摘录,这里有一些问题:

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 I would like to return a promise.
    }

    /**
     * @returns {LeftNavigation}
     */
    get leftNavigation() {
        //Instead of return the object directly, I would like to return a promise here. 
        //But how?
        return this._leftNavigation;
    };

    /**
     * @returns {MainContent}
     */
    get mainContent() {
        //Instead of return the object directly, I would like to return a 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 the title of the main content
     *
     * @returns {webdriver.promise.Promise<string>}
     */
    getMainContentTitle() {
        return this._element_mainContent
                   .findElement(By.id('moduleTitle'))
                   .getText();
    }

}

/** @type {MainContent} */
module.exports = MainContent;

你能给点建议吗? 我希望在某种程度上清楚我要解释的内容:-)

问候

最佳答案

您不应该尝试让 PageObject 成为 Promise。 PageObject 应该是方法/属性工厂,因此不应成为执行流程中的约束。 我会通过返回具有属性的元素而不是尝试在构造函数中定位所有元素来保持简单:

describe('Suite', function() {

    it('should module title be ...', function() {
        let pageAdmin = AdminBaseBage.get();
        let mContent = pageAdmin.mainContent;
        let titlePromise = mContent.getModuleTitle();
        expect(titlePromise).toEqual('module title');
    });

});


class MainContent {

    constructor() {

    }

    get element_module_title() { return element(By.css('#maincontent #moduleTitle')); }

    /**
     * Gets the title of the main content
     *
     * @returns {webdriver.promise.Promise<string>}
     */
    getModuleTitle() {
        return this.element_module_title.getText();
    }

}

关于javascript - Selenium/WebdriverJs/Protractor promise 与页面对象链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37110568/

相关文章:

asp.net - 加载前跳出框架页面

javascript - 如何在 Javascript 中访问 MVC PartialView 模型?

java - 当缩放级别为 100% 时,如何单击 UI 上不可见的元素?

javascript - Nodejs Lambda,.promise() 的用途是什么?

javascript - 链接多个 Promise(处理回调)

javascript - 检查一个 div 是否接触到另一个 div

javascript - 给 anchor 标签的点击事件监听器添加 void(0) for href 和 'return false' 有什么影响?

python - 在 Python 中使用 Selenium 检查 URL 更改的最佳方法是什么?

python - 加速beautifulsoup

javascript - 处理多个 React 组件之间的 Promise