简体   繁体   English

量角器元素(..)从单独的文件返回未定义

[英]Protractor element(..) returning undefined from a separate file

I'm writing a Protractor test and in my test.step.js file I have 我正在编写一个量角器测试,并且在我的test.step.js文件中

element(by.css('...')).getText().then(function (text) {
    expect(text).to.equal('expectedText');
});

This works as expected and passes. 这按预期方式工作并通过。

Instead I created a test.page.js file and in there put this.field = element(by.css('...')); 相反,我创建了一个test.page.js文件,并在其中放置了this.field = element(by.css('...')); and then in my step file had 然后在我的步骤文件中

"use strict"

module.exports = function exampleTest() {
    var TestPage = require("...");
    var testPage = new TestPage;
    ...
    test.Then(..., function (next) {
       testPage.field.getText().then(function (text) {
          expect(text).to.equal('expectedText');
       });
    });
}

then field is undefined. 然后字段是未定义的。 I have also tried adding getText() in the page file, but again get undefined or get told that I can't call 'then' on undefined. 我也尝试过在页面文件中添加getText(),但是再次得到未定义或被告知我无法在未定义上调用'then'。

In my mind, this should do exactly the same thing as the first example, but I'm far from an expert with Angular or JavaScript. 在我看来,这应该与第一个示例完全相同,但是我与Angular或JavaScript的专家相去甚远。

test.page.js looks like: test.page.js看起来像:

"use strict";

module.exports = (function () {
    function TestPage() {
        this.field = element(by.css('...'));
    }

    return TestPage;
});

Hoping someone can shine some light on why this is happening and what I should do instead to be able to put the CSS selector inside a page file for re-use. 希望有人可以阐明为什么会发生这种情况,以及我应该怎么做才能将CSS选择器放在页面文件中以便重新使用。

Thanks 谢谢

Your code new TestPage; 您的代码为new TestPage; returns the constructor TestPage , but it's never called. 返回构造函数TestPage ,但从未调用过。

You could return the class : 您可以返回课程:

function TestPage() {
    this.field = element(by.css('...'));
}

module.exports = TestPage;
var TestPage = require("...");
var testPage = new TestPage;
testPage.field.getText().then(...

Or an instance of the class: 或类的实例:

function TestPage() {
    this.field = element(by.css('...'));
}

module.exports = new TestPage();
var testPage = require("...");
testPage.field.getText().then(...

The way you defined re-usable element locators looks different. 定义可重用元素定位器的方式看起来有所不同。 I am following some thing like below 我正在关注以下内容

Step 1: Define a .js file which should contain the Locator objects and re-usable methods 步骤1:定义一个.js文件,其中应包含Locator对象和可重复使用的方法

var Login = {

    PageElements: {
        emailInput: element(by.css('#email')),
        passwordInput: element(by.css('#password')),
        loginForm: element(by.css('#form')),
    },
    doLogin: function doLogin() {
        this.PageElements.emailInput.sendKeys('blahblah@email.com');
        this.PageElements.passwordInput.sendKeys('blahblah');
        this.PageElements.loginForm.submit();
    },
};

module.exports = Login;

Step 2: Call these page objects in your test classes. 步骤2:在测试类中调用这些页面对象。

var LoginPage = require('../pageobjects/LoginPage.js');
it('Scenario1_Login',function(){
 LoginPage.PageElements.emailInput.sendKeys('blahblah');
});

More details here 在这里更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM