失败:未知错误:在量角器中的browser.executeScript()之后未定义浏览器

时间:2016-10-07 13:51:45

标签: javascript protractor

我有从浏览器控制台使用api登录的方法。

    this.logInViaAPI = function (user) {
    browser.executeScript(function (user) {
        var xhr = new XMLHttpRequest();
        xhr.open("POST", '/api/v1/....', true);
        xhr.setRequestHeader('token', browser.params.tokens.token);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.send('{"email": "' + user.login + '", "password": "' + user.apipassword + '"}');
    });
};

并在测试中使用它

   beforeAll(function () {
    browser.get("");
});


it("test", function (){
    helpers.logInViaAPI(browser.params.regularUser);
    browser.refresh();
});

我得到下一个错误。也许我应该做另外一些事情,但我没有想法。

*- Failed: unknown error: browser is not defined
      (Session info: chrome=53.0.2785.143)
      (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 10.0 x86_64)
            (Session info: chrome=53.0.2785.143)
            (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 10.0 x86_64)
              at new bot.Error (\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:108:18)
              at Object.bot.response.checkResponse (\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\response.js:109:9)
              at \node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:379:20
              at [object Object].promise.Promise.goog.defineClass.invokeCallback_ (/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:1337:14)
              at [object Object].promise.ControlFlow.goog.defineClass.goog.defineClass.abort_.error.executeNext_.execute_ (/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2776:14)
              at [object Object].promise.ControlFlow.goog.defineClass.goog.defineClass.abort_.error.executeNext_ (/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2758:21)
              at goog.async.run.processWorkQueue (\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:124:15)
              at process._tickCallback (node.js:368:9)
          From: Task: Run it("test") in control flow
              at [object Object].promise.Promise.goog.defineClass.invokeCallback_ (/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:1337:14)
              at [object Object].promise.ControlFlow.goog.defineClass.goog.defineClass.abort_.error.executeNext_.execute_ (/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2776:14)
              at [object Object].promise.ControlFlow.goog.defineClass.goog.defineClass.abort_.error.executeNext_ (/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2758:21)
              at goog.async.run.processWorkQueue (\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:124:15)
          From asynchronous test: 
          Error
              at Suite.<anonymous> (\test\e2e\specs\search\search.loggedIn.spec.js:86:5)
              at Object.<anonymous> (\test\e2e\specs\search\search.loggedIn.spec.js:8:1)
              at Module._compile (module.js:435:26)
              at Object.Module._extensions..js (module.js:442:10)
              at Module.load (module.js:356:32)
              at Function.Module._load (module.js:311:12)*

1 个答案:

答案 0 :(得分:3)

您通过executeScript执行的脚本在其范围内不会有browser。您需要通过arguments 参数化您的脚本:

var token = browser.params.tokens.token;
browser.executeScript(function (arguments) {
    var token = arguments[0];
    ...
    xhr.setRequestHeader('token', token);
}, token);