规范的方法来定义量角器页面对象量角器、方法来、定义、对象

2023-09-13 02:53:17 作者:身控

我们一直在使用页对象模式了好一阵子。这肯定有助于组织的终端到终端的测试,使测试更具可读性和清洁。

We've been using the Page Object pattern for quite a while. It definitely helps to organize the end-to-end tests and makes tests more readable and clean.

由于使用页面对象来组织测试量角器文档页面向我们展示了,我们定义的每个页面对象作为一个函数,并使用来实例吧:

As Using Page Objects to Organize Tests Protractor documentation page shows us, we are defining every page object as a function and use new to "instantiate" it:

"use strict";

var HeaderPage = function () {
    this.logo = element(by.css("div.navbar-header img"));
}

module.exports = HeaderPage;

用法:

"use strict";

var HeaderPage = require("./../po/header.po.js");

describe("Header Look and Feel", function () {
    var header;

    beforeEach(function () {
        browser.get("/#login");
        header = new HeaderPage();
    });

    it("should show logo", function () {
        expect(header.logo.isDisplayed()).toBe(true);
    });

});

不过,最近在Protractor:角测试变得容易谷歌测试博客文章中,我注意到一个页面对象定义为一个对象:

But, recently in the Protractor: Angular testing made easy Google Testing Blog post, I've noticed that a page object is defined as an object:

var angularHomepage = {
    nameInput : element(by.model('yourName')),
    greeting : element(by.binding('yourName')),
    get : function() {
        browser.get('index.html');
    },
    setName : function(name) {
        this.nameInput.sendKeys(name);
    }
};

什么是这两种方式来介绍页面对象之间的区别?我应该preFER一个顶着一个?

What is the difference between these two ways to introduce Page Objects? Should I prefer one against the other?

推荐答案

最后,我认为这是个人的preference的问题。

Ultimately, I think it is a question of personal preference.

是的,你可以使用构造方式和实例在每个测试套件单身......是的,你可以使用一个简单的对象字面如上......是的,你可以使用一个工厂函数...

Yes, you can use the constructor pattern and instantiate a singleton in each test suite... yes you could use a simple object literal as above... yes you could use a factory function...

使用继承通过类(伪或ES2015语法是否)与通过混入扩展的对象是一般的应用程序开发中更广泛的辩论,别提端到端测试,结构化code!

Structuring code using inheritance via "classes" (whether pseudo- or ES2015 syntax) vs objects extended via mixins is a much wider debate within application development in general, never mind e2e tests!

最主要的是在您的测试套件明确,一贯的做法,促进code重用尽可能。

The main thing is clear, consistent practice across your test suites and promoting code reusability wherever possible.