2017-07-18 59 views
0

我试图使用静态方法以便在外部使用它。尝试将其用作静态方法时未定义方法

utils的文件:

'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(); 
 
     }); 
 
    });

但我不断收到错误说:Failed: utils is not defined

+0

'this'在(假设)静态方法没有任何意义!此外,如果你;要使用stacksnippets来演示问题 - 确保它们能够正常工作(即,做一些事情,不要错误) – Jamiec

+0

如何导入utils文件? –

+0

@YuryTarabanko - 对于这个问题我甚至不知道'utils'是如何导出的* – Jamiec

回答

1

你的页面文件:

'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'); 
    }); 

}); 

你应该偷看了一下关于量角器使用静态方法,它们会使你粗糙的目的。

Explanation

Better explanation about Page Objects Pattern in testing

+0

一般来说,我确实使用页面对象模式,但由于某些其他原因需要使用外部文件。 –

+0

如果您需要使用外部文件(例如信息)来对付表单,您应该像载入json文件一样加载它进行测试。 F.Ex:'browser.params.users = require('./ shared.params。'+ countryCode +'.json')。用户;' – Alf

+0

我使用了你的代码,但得到一个错误。错误:TypeError:无法设置未定义的属性'方法' –