量角器元素(..)从单独的文件返回未定义 [英] Protractor element(..) returning undefined from a separate file

查看:83
本文介绍了量角器元素(..)从单独的文件返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个量角器测试,并且在我的test.step.js文件中

I'm writing a Protractor test and in my test.step.js file I have

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

这可以按预期工作并通过.

This works as expected and passes.

相反,我创建了一个test.page.js文件,并在其中放置了this.field = element(by.css('...'));,然后在我的步骤文件中添加了

Instead I created a test.page.js file and in there put 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');
       });
    });
}

然后未定义字段.我也尝试过在页面文件中添加getText(),但是再次变得未定义,或者被告知我无法在未定义上调用'then'.

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.

在我看来,这应该与第一个示例完全相同,但是我与Angular或JavaScript的专家相去甚远.

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.

test.page.js看起来像:

test.page.js looks like:

"use strict";

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

    return TestPage;
});

希望有人可以阐明为什么会发生这种情况,以及我应该怎么做才能将CSS选择器放在页面文件中以便重新使用.

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.

谢谢

推荐答案

您的代码new TestPage;返回构造函数TestPage,但从未调用过.

Your code new TestPage; returns the constructor TestPage, but it's never called.

您可以返回课程:

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

module.exports = TestPage;

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

或该类的实例:

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

module.exports = new TestPage();

var testPage = require("...");
testPage.field.getText().then(...

这篇关于量角器元素(..)从单独的文件返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆