如何在Protractor自动化中使用Javascript命名空间?

时间:2016-11-18 06:04:48

标签: javascript jquery automation jasmine protractor

我有3个文件,即conf.js,actionwords.js,project_test.js。  Actionwords.js和project_test.js是从hiptest工具生成的文件。所以我需要使用这个结构来自动化测试用例。当我浏览cmd时,我收到错误。

我跑了:

protractor conf.js

消息:   失败:无法读取属性' theApplicationURL'未定义的

堆栈:    TypeError:无法读取属性' theApplicationURL'未定义的

// conf.js

exports.config = {
 framework: 'jasmine2',
 directConnect: true,
 seleniumAddress: 'http://localhost:4444/wd/hub',
 specs:['path to/project_test.js'],
 capabilities: { 'browserName': 'chrome' }
 };

// actionwords.js

var Actionwords = {
theApplicationURL: function () {
browser.get('localhost');
browser.driver.manage().window().maximize();
browser.sleep(5000);
   },
};

// project_test.js

describe('Test', function () {
beforeEach(function () {
this.actionwords = Object.create(Actionwords);
});

it('Login_Test (uid:fe6d6670-a864-4d0f-a867-3faf9f51ff8d)', function () {
// Given the application URL
this.actionwords.theApplicationURL();
});
});

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:3)

以这种方式改变:

var actionwords = {
  theApplicationURL: function () {
    browser.get('localhost');
    browser.driver.manage().window().maximize();
    browser.sleep(5000);
  },
};

module.exports = actionwords;

试验:

var actionwords = require("actionwords.js")

describe('Test', function () {
  it('Login_Test (uid:fe6d6670-a864-4d0f-a867-3faf9f51ff8d)', function () {
    // Given the application URL
    actionwords.theApplicationURL();
  });
});

关于this的评论的反应:

您可以在beforeEach

中将其分配到此范围
var actionwords = require("actionwords.js")

describe('Test', function () {
  beforeEach(function () {
    this.actionwords = actionwords;
  });
  it('Login_Test (uid:fe6d6670-a864-4d0f-a867-3faf9f51ff8d)', function () {
    // Given the application URL
    this.actionwords.theApplicationURL();
  });
});

答案 1 :(得分:0)

try .. except中,beforeEach(...)指向传递给它的匿名函数。 this也是如此。

声明it(...)

范围内的变量
describe

答案 2 :(得分:0)

如下所示更新actionword.js

var Actionwords = {
theApplicationURL: function () {
  browser.get('localhost');
  browser.driver.manage().window().maximize();
  browser.sleep(5000);
 },
};
module.exports = new Actionwords();

您的project_test.js就像,

this.actionwords = require("actionword.js");

describe('Test', function () {
 it('Login_Test (uid:fe6d6670-a864-4d0f-a867-3faf9f51ff8d)', function () {
   this.actionwords.theApplicationURL();
 });
});