URL 更改后使用 browser.getCurrentUrl() 时出现超时错误
我正在为 AngularJS 应用的登录编写量角器测试,并希望验证登录是否成功以及登录后 URL 是否发生变化。我尝试将预期条件与 urlContains() 结合使用,也尝试使用 browser.getCurrentUrl().toContain(),但两者都出现了错误。
exports.config = {
seleniumAddress : 'http://localhost:4444/wd/hub',
specs: ['login.spec.js'],
};
当 URL 正确时,预期条件通过测试。但当 URL 不同时,它会抛出超时错误 “失败:等待 5013 毫秒后超时”。 expect(browser.getCurrentUrl()).toContain('/dashboard') 总是失败并出现以下错误
Stack:
ScriptTimeoutError: script timeout: result was not received in 11 seconds
(Session info: chrome=75.0.3770.142)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.3', java.version: '12.0.1'
Driver info: driver.version: unknown
at Object.checkLegacyResponse (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:546:15)
at parseHttpResponse (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:509:13)
at doSend.then.response (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:441:30)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
From: Task: Protractor.waitForAngular()
at thenableWebDriverProxy.schedule (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver.js:807:17)
at ProtractorBrowser.executeAsyncScript_ (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/built/browser.js:425:28)
at angularAppRoot.then (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/built/browser.js:456:33)
at ManagedPromise.invokeCallback_ (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1376:14)
at TaskQueue.execute_ (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:3084:14)
at TaskQueue.executeNext_ (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:3067:27)
at asyncRun (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2927:27)
at /Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:668:7
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
From: Task: Run it("should login successfully") in control flow
at UserContext.<anonymous> (/Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:19)
From asynchronous test:
Error
at Object.<anonymous> (/Users/ProtractorTest/Tests/login.spec.js:17:3)
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)
at /Users/.nvm/versions/node/v8.9.4/lib/node_modules/protractor/node_modules/jasmine/lib/jasmine.js:93:5
下面是我的代码
it('should login successfully', function () {
browser.get("https://example.com/");
loginobj.username.sendKeys(logindata.email);
loginobj.password.sendKeys(logindata.password);
loginobj.loginbtn.click().then(function(){
browser.getCurrentUrl().then(url => expect(url).toContain('/dashboard'));
//var EC = protractor.ExpectedConditions;
//browser.wait(EC.urlContains('/dashboard'), 5000);
})
我希望当 url 与预期不同时,它应该显示有效的错误消息而不是超时错误。
默认情况下,protractor 会为您处理所有异步。查看您的代码,您依赖于默认的 protractor 行为,即不将
SELENIUM_PROMISE_MANAGER
设置为
false
。
在这种情况下,您为什么要在
click().then()
中执行某些操作?它可以像
loginobj.loginbtn.click();
expect(browser.getCurrentUrl()).toContain('/dashboard');
关于您的代码的一个理论是:一旦您在
click().then()
中有了某些内容,它就会从 protractor 为您处理的承诺队列中移出。除非绝对必要,例如从元素中获取值以供以后在规范中使用,我建议尽可能不要干预量角器异步处理。
希望有所帮助。
我有类似的东西
const currentUrl = await browser.getCurrentUrl().then(url => url);
expect(currentUrl).toContain('/dashboard')
尝试一下也许它会有所帮助,只是没有等待,因为我看到你不使用异步函数
或者像这样
await browser.getCurrentUrl().then(url => expect(url).toContain('/dashboard'));
在 Protractor 中,默认脚本超时为 11 秒, 在上面的代码片段中,
browser.getCurrentUrl().then(url => expect(url).toContain('/dashboard'));
语句需要超过 11 秒才能解决承诺。
解决方案
:在 Protractor 配置文件中,添加以下语句
allScriptsTimeout: timeout_in_millis
。
例如 30 秒超时
allScriptsTimeout: 30000
编辑的配置文件:
exports.config = {
allScriptsTimeout: 30000,
seleniumAddress : 'http://localhost:4444/wd/hub',
specs: ['login.spec.js'],
};