我正在尝试使用静态方法以便在外部使用它。
实用程序文件:
'use strict'
function utils(){}
utils.staticMethod1 = function(){
alert("foo");
};
module.exports = utils();
'use strict;'
let HomePage = require('../page/home_page.js');
let utilsPage = require('../utils/utils.js');
describe("login to website",function(){
let employeeId;
let employeeBday;
let home = new HomePage();
beforeEach(function(){
browser.driver.get("http://foo.com/");
});
it("should succees picking a present",function(){
utilsPage.staticMethod1();
});
});
但我不断收到错误消息:失败:utils 未定义
最佳答案
您的页面文件:
'use strict';
var Utils = function(){
this.methodTest = function(){
console.log("Something"); //alert(this);
};
};
module.exports = Utils;
您的规范文件:
'use strict;'
let Utils = require('../page/utils.Page.js');
describe("login to website",function(){
let employeeId;
let employeeBday;
let utils = new Utils();
beforeEach(function(){
browser.driver.get("http://foo.com/");
});
it("should success picking a present",function(){
utils.methodTest();
expect(browser.getTitle()).toEqual('SomethingToGetAnError');
});
});
您应该了解一下在 Protractor 中使用静态方法的信息,它们会导致您陷入困境。
关于javascript - 尝试将其用作静态方法时未定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45164527/