gpt4 book ai didi

javascript - Protractor 中失败 : unknown error: browser is not defined after browser.executeScript()

转载 作者:行者123 更新时间:2023-11-28 18:20:53 26 4
gpt4 key购买 nike

我有从浏览器控制台使用 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)*

最佳答案

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

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

关于javascript - Protractor 中失败 : unknown error: browser is not defined after browser.executeScript(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39918939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com