Cypress仅将我们的域列入白名单
我有一个页面,里面有广告,有时,在iframe中,有时没有 问题是页面超时(60秒),即使看起来已加载。我认为这可能是广告或其他一些跟踪,因此我想添加白名单到我们的资源网址,使任何广告或类似的资源得到删除 这可能不是100%准确的测试方法,但对于我们的案例来说已经足够了 我在beforeach中尝试过使用它(不是最优的,但如果它可以工作,我会将它变成一个命令并使用它)Cypress仅将我们的域列入白名单,cypress,Cypress,我有一个页面,里面有广告,有时,在iframe中,有时没有 问题是页面超时(60秒),即使看起来已加载。我认为这可能是广告或其他一些跟踪,因此我想添加白名单到我们的资源网址,使任何广告或类似的资源得到删除 这可能不是100%准确的测试方法,但对于我们的案例来说已经足够了 我在beforeach中尝试过使用它(不是最优的,但如果它可以工作,我会将它变成一个命令并使用它) 我还在cypress.json中找到了一些黑名单选项,但我需要的是白名单而不是黑名单 Cypress有一个默认的白名单,可以在这
我还在
cypress.json
中找到了一些黑名单选项,但我需要的是白名单而不是黑名单 Cypress有一个默认的白名单,可以在这里找到信息:
更改默认的白名单
cy.server()附带了一个白名单函数,默认情况下可用于筛选
输出对静态资产(如.html、.js、.jsx和)的任何请求
.css
任何通过白名单的请求都将被忽略-不会被忽略
也不会以任何方式将其存根(即使它与
特定路径())
我们的想法是,我们永远不想干预
是通过Ajax获取的
Cypress中的默认白名单函数为:
您可以使用自己的特定逻辑覆盖此功能:
似乎您可以通过在cypress上设置选项来永久覆盖该白名单。服务器:不知道覆盖所有测试,谢谢。然而,我的问题主要源于
fetch
。
cy.server({
whitelist(xhr) {
// Basicly, does it match any of whitelisted URLs?
console.log('whitelisting', xhr.url)
const url = new URL(xhr.url);
const URLwhitelist: string[] = Cypress.env('URLwhitelist');
if (!URLwhitelist.length) {
return true
}
return URLwhitelist.some(allowerdUrl => {
if (allowerdUrl.split('.').length == 2) {
return url.host.includes(allowerdUrl);
} else if (allowerdUrl.startsWith('*.')) {
allowerdUrl = allowerdUrl.slice(1);
return url.host.includes(allowerdUrl);
}
throw new Error(`Unparsable whitelist URL (${allowerdUrl})`);
});
}
});
const whitelist = (xhr) => {
// this function receives the xhr object in question and
// will whitelist if it's a GET that appears to be a static resource
return xhr.method === 'GET' && /\.(jsx?|html|css)(\?.*)?$/.test(xhr.url)
}
cy.server({
whitelist: (xhr) => {
// specify your own function that should return
// truthy if you want this xhr to be ignored,
// not logged, and not stubbed.
}
})