我正在为AngularJS应用程序编写用于登录的量角器测试,并希望验证登录是否成功以及登录后url是否更改。我尝试将预期条件与urlContains()一起使用,还尝试与browser.getCurrentUrl()。toContain()一起使用,但两者均出现错误。
exports.config = {
seleniumAddress : 'http://localhost:4444/wd/hub',
specs: ['login.spec.js'],
};
URL正确时,预期条件通过测试。但是,如果网址不同,则会引发超时错误 “失败:等待时间在5013ms之后超时”。 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与预期的URL不同时,它应该显示有效的错误消息,而不是超时错误。
答案 0 :(得分:1)
默认情况下,量角器为您处理所有异步。查看您的代码,您所依赖的是默认的量角器行为,即未将SELENIUM_PROMISE_MANAGER
设置为false
。
在这种情况下,为什么要在click().then()
内部做某事?它可以像
loginobj.loginbtn.click();
expect(browser.getCurrentUrl()).toContain('/dashboard');
关于您的代码的一种理论:一旦click().then()
中包含某些内容,它就会偏离量角器为您处理的承诺队列。除非绝对必要,否则为了从元素中获取价值以供以后在规范中使用,我建议不要过多地使用量角器异步处理。
希望有帮助。
答案 1 :(得分:0)
我有类似的东西
const currentUrl = await browser.getCurrentUrl().then(url => url);
expect(currentUrl).toContain('/dashboard')
尝试一下,也许会有所帮助,只是等一下,因为我看到您没有使用异步功能
或类似的
await browser.getCurrentUrl().then(url => expect(url).toContain('/dashboard'));
答案 2 :(得分:0)
在量角器中,默认脚本超时为11秒, 在以上代码段中,
browser.getCurrentUrl().then(url => expect(url).toContain('/dashboard'));
声明要花费11秒以上才能解决承诺。
解决方案:在量角器配置文件中,添加以下语句
allScriptsTimeout: timeout_in_millis
。
例如30秒超时
allScriptsTimeout: 30000
编辑的配置文件:
exports.config = {
allScriptsTimeout: 30000,
seleniumAddress : 'http://localhost:4444/wd/hub',
specs: ['login.spec.js'],
};