简体   繁体   English

如何调用specs中的函数 - 量角器

[英]how to call the function in the specs - Protractor

I write this script under the module: 我在模块下编写这个脚本:

  var enter_search = function () { this.clickSearch = function (value) { element(by.id('searchbutton')).click(); }; this.waitElementFound = function (value) { var EC = protractor.ExpectedConditions; browser.wait(EC.presenceof(value), 35000); }; }; module.exports = new enter_search(); 

and to call this function on my spec, i wrote this: 并在我的规范上调用此函数,我写道:

 var search = require('enter_search'); var loadtxt = element (by.id('text')); it('waits for the element', function(){ search.waitElementFound(loadtxt); search.clickSearch(); }); 

When I execute the test, it gives me an error undefined function. 当我执行测试时,它给我一个错误未定义的函数。 Not sure what went wrong. 不知道出了什么问题。 Thanks 谢谢

To use functions from one file in another, you should export the function and then require it in the other file. 要使用另一个文件中的函数,您应该导出该函数,然后在另一个文件中要求它。 Here's an example - 这是一个例子 -

File test.js 文件test.js

var search =  require('./helper.js');
var loadtxt = element(by.id('text'));

it('waits for the element', function(){
    search.waitElementFound(loadtxt);
});

File helper.js 文件helper.js

var waitElementFound = function (value) {
    var EC = protractor.ExpectedConditions;
    browser.wait(EC.visibilityOf(value), 35000);
};

module.exports = new waitElementFound(); //export the function

Hope this helps. 希望这可以帮助。

我已经解决了这个问题,我只需要在var x = function之外声明我的变量..新手的挣扎:)无论如何,谢谢@Girish Sotur

var enter_search = function () { this.clickSearch = function (value) { element(by.id('searchbutton')).click(); }; this.waitElementFound = function (value) { var EC = protractor.ExpectedConditions; browser.wait(EC.presenceof(value), 35000); }; }; module.exports = new enter_search();

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

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