【发布时间】:2019-07-21 14:09:43
【问题描述】:
我有一个量角器测试设置,可以完成可以想象的最基本的事情。它登录到一个网络应用程序。一切似乎都很好,直到我尝试通过在后续页面上查找元素的存在来评估登录是否成功。我确定这是一个同步问题,但不知道如何解决。
注意事项:
- 我对 javascript 还很陌生(尽管对编码不熟悉),因此我们不胜感激代码改进建议。
- 登录功能就像一个魅力,包括等待。
- Async / Promises / Awaits 仍然让我有点不知所措。
登录方式
this.login = async function (repo, username, password, domain) {
if (repo == null){ repo = REPO_UNDER_TEST; }
browser.driver.manage().window().maximize();
await this.openLoginPage(domain).
then( this.selectRepo(repo) ).
then( this.usernameTextBox.sendKeys(username) ).
then( this.passwordTextBox.sendKeys(password) ).
then( this.signInButton.click() ).
then( async function() {
console.log(`I'm starting my WAIT at ${Date().toString()}`);
await browser.wait(
EC.titleIs(repo),
32000
);
} )
};
测试代码
describe('Login Tests', function () {
it('should log you in given a good username/password', async function () {
console.log(`I'm starting my TEST at ${Date().toString()}`);
await loginPage.login(null, 'edwin', 'e', null);
console.log(`I'm starting my EVAL at ${Date().toString()}`);
var searchBox = element.all(by.model('searchQuery'));
expect(searchBox.isPresent()).toBe(true);
expect(true).toBeTruthy();
});
});
console.logs 只是为了确保事情以正确的顺序发生。如果我注释掉我期望 searchBox 出现的行,则测试运行完成并正确评估简单的 expect(true).toBeTruthy()。如果我尝试运行 searchBox 的期望,则测试会挂起,直到我手动将其关闭。然后控制台会告诉我期望失败,因为该项目不存在,即使它在 console.log 之前在屏幕上清晰可见,说明 eval 正在启动。
日志输出
[10:22:42] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
Started
I'm starting my TEST at Wed Feb 27 2019 10:22:45 GMT-0800 (Pacific Standard Time)
I'm starting my WAIT at Wed Feb 27 2019 10:22:46 GMT-0800 (Pacific Standard Time)
I'm starting my EVAL at Wed Feb 27 2019 10:23:06 GMT-0800 (Pacific Standard Time)
F
Failures:
1) Login Tests should log you in given a good username/password
Message:
Expected false to be true.
Stack:
Error: Failed expectation
at UserContext.<anonymous>
(C:\GIT\TestFramework_POCs\protract\TESTS\login-spec.js:15:39)
at process._tickCallback (internal/process/next_tick.js:68:7)
1 spec, 1 failure
Finished in 37.295 seconds
[10:23:22] I/launcher - 0 instance(s) of WebDriver still running
[10:23:22] I/launcher - chrome #01 failed 1 test(s)
[10:23:22] I/launcher - overall: 1 failed spec(s)
[10:23:22] E/launcher - Process exited with error code 1
【问题讨论】: