testing - 等待两个条件,直到一个成功
问题描述
我正在使用TestCafe,但无法实现此行为:
Role
使用机制登录我的网站- 检查条件:
- 网址更改或
- 出现错误消息
我知道如何使用获取当前 urlClientFunction
并检查元素可见性,Selector
但我不知道如何将两者混合。
可能吗 ?
基于 NoriSte 的回答(谢谢!),这是一个改编自我的用例的工作示例。我发现它既不优雅也不可扩展(如果您想要 ORing 更多条件,该怎么办)。
AuthPage.js
import { Selector, t, Role, ClientFunction } from "testcafe"
const url = 'http://localhost:8081'
const loginUrl = `${url}/login`;
const logoutUrl = `${url}/logout`;
const loginInput = Selector('[data-qa="login input"]');
const passwordInput = Selector('[data-qa="password input"]');
const submitButton = Selector('[data-qa="submit button"]')
const loginError = Selector('[data-qa="login error"]');
const getLocation = ClientFunction(() => document.location.href.toString());
const login = (username, password) => async tt =>
await tt
.typeText(loginInput, username)
.pressKey('tab')
.typeText(passwordInput, password)
.click(submitButton);
export default class AuthPage {
constructor () {
this.roles = {
// 'almighty god': Role(loginUrl, login('filou', 'filoucfou')),
// 'bad mutafuka': Role(loginUrl, login('badbadbad', 'password'))
// Can't use Role, don't know why ?!
'almighty god': login('gooood', 'password'),
'bad mutafuka': login('badbadbad', 'password')
}
}
async loginWith (roleName) {
const oldUrl = await getLocation();
// await t
// .useRole(this.roles[roleName]);
// Does weird thing, not what i would expect
await this.roles[roleName](t)
if (await loginError.with({ timeout: 5000, visibilityCheck: true }).exists) {
console.log('login error:', oldUrl)
return {url: oldUrl, login: false};
}
await t
.wait(10000);
const newUrl = await getLocation()
return {url: newUrl, login: oldUrl !== newUrl};
}
}
测试.js
import LoginPage from 'AuthPage'
fixture `sinoc-fixture`
.page `http://localhost:8081`;
const loginPage = new LoginPage()
test('Login with wrong credentials should fail', async (t) => {
const result = await loginPage.loginWith('bad mutafuka')
await t
.expect(result.login === false).ok()
});
test('Login with good credentials should succeed', async (t) => {
const result = await loginPage.loginWith('almighty god')
await t
.expect(result.login === true)
.ok()
.expect(result.url)
.contains('/dashboard');
});
解决方案
我最终得到了 a ClientFunction
,轮询了 2 个条件:
const getLocationAndError = ClientFunction(({url}) => {
return new Promise(resolve => {
(function polling() {
const errorModal = document.querySelector('[data-qa="login error"]');
const newUrl = document.location.href.toString();
const navigated = newUrl !== url;
if (errorModal || navigated) {
resolve({navigated, loginError: !!errorModal, url: newUrl});
} else {
setTimeout(polling, 200);
}
})();
});
});
推荐阅读
- angular - Angular 7:Formvalidation,Validators.pattern 不起作用
- java - 如何访问偏好布局内的视图,Android
- daml - 从 Java 或 JavaScript 以外的语言使用 DAML Ledger API
- javascript - 拖拽上传
- swift - Swift 包构建错误:无法构建 Objective-C 模块“Darwin”
- case-insensitive - Blue Prism 决策阶段忽略字母大写
- reactjs - 在 xState 操作中设置上下文并触发方法
- ios - 如何在 Swift 4 中初始化代码生成的按钮边框
- debugging - OpenCL + Intel System Studio 2019 - GPU 调试问题
- android - 在 android 中按下时更改选项卡布局的选项卡中的文本大小