如何在我们创建文件的同一文件中调用this.function

时间:2019-03-21 14:53:17

标签: javascript node.js selenium-webdriver protractor

在我的项目中,我们尝试构建量角器框架。因此,在js文件中,我们使用this.functionname语法创建了一些函数,并且我们希望在同一文件中也存在的函数中需要时重新使用该函数。为清晰起见,我在下面提供了代码...

这是我的可重用功能JS文件

var fb=require("H:/workspace/Protractor_PT/src/pages/FbPage.js");
var action=function(){

    beforeAll(function(){
        browser.ignoreSynchronization=true;
        browser.get("https://facebook.com");
        login();
    });

    this.clickElement=function(element){
        element.click();
    }

    this.enterText=function(element,text){
        element.sendKeys(text);
    }

    var login=function(){
        action.enterText(fb.emailField(),"dfdsfds");
        action.enterText(fb.passfield(),"dfdsfds");
    }
}
module.login=new action();

在顶部,您可以找到函数this.clickElement和this.enterText ..我想在另一个名为login ..的函数中使用这些函数,但是当我使用“ action.enterText”调用这些函数时-action是文件的主要全局变量...我遇到错误

错误日志

 Message:
    Failed: action.enterText is not a function
  Stack:
    TypeError: action.enterText is not a function
        at login (H:\workspace\Protractor_PT\src\pages\Utilities.js:19:13)
        at UserContext.<anonymous> (H:\workspace\Protractor_PT\src\pages\Utilities.js:7:3)
        at C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:112:25
        at new ManagedPromise (C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1077:7)
        at ControlFlow.promise (C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2505:12)
        at schedulerExecute (C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:95:18)
        at TaskQueue.execute_ (C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:3084:14)
        at TaskQueue.executeNext_ (C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:3067:27)
        at asyncRun (C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2974:25)
        at C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:668:7
    From: Task: Run beforeAll in control flow
        at UserContext.<anonymous> (C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:94:19)
        at Jasmine.execute (C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\jasmine\lib\jasmine.js:200:12)
        at C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\built\frameworks\jasmine.js:132:15
        at Function.promise (C:\Users\DELL\AppData\Roaming\npm\node_modules\protractor\node_modules\q\q.js:682:9)
    From asynchronous test:
    Error
        at new action (H:\workspace\Protractor_PT\src\pages\Utilities.js:4:2)
        at Object.<anonymous> (H:\workspace\Protractor_PT\src\pages\Utilities.js:23:14)
        at Module._compile (module.js:643:30)
        at Object.Module._extensions..js (module.js:654:10)
        at Module.load (module.js:556:32)
        at tryModuleLoad (module.js:499:12)
        at Function.Module._load (module.js:491:3)
        at Module.require (module.js:587:17)
        at require (internal/module.js:11:18)

1 spec, 1 failure
Finished in 0.049 seconds

[20:22:32] I/launcher - 0 instance(s) of WebDriver still running
[20:22:32] I/launcher - chrome #01 failed 1 test(s)
[20:22:32] I/launcher - overall: 1 failed spec(s)
[20:22:32] E/launcher - Process exited with error code 1

1 个答案:

答案 0 :(得分:-1)

在您创建内部函数的情况下,变量 action 是未知的。 您可以执行一个指向父函数的局部变量,并使用它代替 action

您可以执行以下操作:

var fb = require('H:/workspace/Protractor_PT/src/pages/FbPage.js')
var action = function () {
    var localAction = this;    // <<<-----------   HERE
    beforeAll(function () {
        browser.ignoreSynchronization = true
        browser.get('https://facebook.com')
        login()
    })
    this.clickElement = function (element) {
        element.click()
    }
    this.enterText = function (element, text) {
        element.sendKeys(text)
    }
    var login = function () {
        localAction.enterText(fb.emailField(), 'dfdsfds') // Use local var here.
        localAction.enterText(fb.passfield(), 'dfdsfds')
    }
}
module.login = new action()