Testing 等待两个条件,直到其中一个成功
我正在使用TestCafe,无法实现此行为:Testing 等待两个条件,直到其中一个成功,testing,automated-tests,conditional-statements,e2e-testing,testcafe,Testing,Automated Tests,Conditional Statements,E2e Testing,Testcafe,我正在使用TestCafe,无法实现此行为: 使用角色机制登录我的网站 检查条件: Url更改或 出现一条错误消息 我知道如何使用ClientFunction获取当前url,并使用Selector检查元素可见性,但我不知道如何将两者混合使用 可能吗 基于NoriSte的回答(谢谢!),这是一个根据我的用例改编的工作示例。我觉得它既不优雅也不可扩展(如果您想要更多条件,该怎么办) AuthPage.js test.js 你有一个我可以测试它的公共页面吗? 不管怎样,我起草了这个代码,我不
角色
机制登录我的网站- Url更改或
- 出现一条错误消息
ClientFunction
获取当前url,并使用Selector
检查元素可见性,但我不知道如何将两者混合使用
可能吗
基于NoriSte的回答(谢谢!),这是一个根据我的用例改编的工作示例。我觉得它既不优雅也不可扩展(如果您想要更多条件,该怎么办) AuthPage.js test.js
你有一个我可以测试它的公共页面吗? 不管怎样,我起草了这个代码,我不确定它是否适合你的需要,也不确定它是否有效
import { ClientFunction } from 'testcafe';
const getLocation = ClientFunction(() => document.location.href);
test('redirect OR error', async t => {
let errorMesageDidAppear = false;
let didRedirect = false;
const oldUrl = getLocation();
await t.useRole(yourUser)
// If a redirect happens TestCafe automatically waits for it
// @see http://devexpress.github.io/testcafe/documentation/test-api/built-in-waiting-mechanisms.html#waiting-for-redirects
// if it doesn't happen TestCafe goes ahead with the test
const newUrl = getLocation();
didRedirect = newUrl !== oldUrl;
if(!didRedirect) {
await Selector('#ERROR_BOX_ID').with({ visibilityCheck: true })();
errorMesageDidAppear = true;
// if it doesn't appear the test will fail due to the timeout limit
}
// now you have two flags: didRedirect and errorMesageDidAppear
});
我很像这里看到的代码
而我最终得到了一个
ClientFunction
,轮询了两个条件:
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);
}
})();
});
});
好的,抱歉耽搁了,我很抱歉。我将根据您的解决方案发回一个解决方案。我必须在
getLocation
调用前面放置一些wait
,并且Role
不起作用。
import { ClientFunction } from 'testcafe';
const getLocation = ClientFunction(() => document.location.href);
test('redirect OR error', async t => {
let errorMesageDidAppear = false;
let didRedirect = false;
const oldUrl = getLocation();
await t.useRole(yourUser)
// If a redirect happens TestCafe automatically waits for it
// @see http://devexpress.github.io/testcafe/documentation/test-api/built-in-waiting-mechanisms.html#waiting-for-redirects
// if it doesn't happen TestCafe goes ahead with the test
const newUrl = getLocation();
didRedirect = newUrl !== oldUrl;
if(!didRedirect) {
await Selector('#ERROR_BOX_ID').with({ visibilityCheck: true })();
errorMesageDidAppear = true;
// if it doesn't appear the test will fail due to the timeout limit
}
// now you have two flags: didRedirect and errorMesageDidAppear
});
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);
}
})();
});
});