我正在使用此示例来了解pageobjects:

https://github.com/angular/protractor/blob/master/docs/page-objects.md


尝试通过对angularHomepage(pageobject)代码使用单独的js文件进行一些重构:

var AngularHomepage = function() {
    this.nameInput = element(by.model('yourName'));
    this.greeting = element(by.binding('yourName'));

    this.get = function() {
        browser.get('http://www.angularjs.org');
    };

    this.setName = function(name) {
        this.nameInput.sendKeys(name);
    };
};


specfile现在看起来像这样:

var angularHomepage = require('/angularjs_homepage.js');

describe('angularjs homepage', function() {

    it('should greet the named user', function() {

        angularHomepage.get();

        angularHomepage.setName('Julie');

        expect(angularHomepage.greeting.getText()).toEqual('Hello Julie!');
    });
});


运行规范时出现错误? :

Message:
       Error: Cannot find module '/angularjs_homepage.js'
       Stacktrace:
       Error: Cannot find module '/angularjs_homepage.js'
       at require (module.js:380:17)
       at Object.<anonymous> (c:\ptor_testing\example_spec.js:1:85)

最佳答案

它需要在pageobjects文件中增加一行:

module.exports = new AngularHomepage();

10-06 08:02