赛普拉斯仅将我们的域列入白名单
Cypress whitelist only our domains
我有一个页面,其中有广告,有时在 iframe 中,有时不。
问题是页面超时(60 秒),即使它看起来已加载。我认为这可能是广告或其他一些跟踪,因此我想将白名单添加到我们的资源 URL 以便删除任何广告或类似资源。
这可能不是 100% 准确的测试方法,但对于我们的案例来说已经足够好了。
我已经尝试在 beforeEach 中使用它(不是最优的,但如果它可行,我会把它变成一个命令并使用它)
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})`);
});
}
});
我也在cypress.json
中找到了一些黑名单选项,但我需要白名单而不是黑名单。
赛普拉斯有一个默认的白名单,可以在这里找到信息:
https://docs.cypress.io/api/commands/server.html#Options
Change the default whitelisting
cy.server() comes with a whitelist function that by default filters
out any requests that are for static assets like .html, .js, .jsx, and
.css.
Any request that passes the whitelist will be ignored - it will not be
logged nor will it be stubbed in any way (even if it matches a
specific cy.route()).
The idea is that we never want to interfere with static assets that
are fetched via Ajax.
The default whitelist function in Cypress is:
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)
}
You can override this function with your own specific logic:
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.
}
})
看来您可以通过在 cypress.server 上设置选项来永久覆盖该白名单:https://docs.cypress.io/api/cypress-api/cypress-server.html#Syntax
我有一个页面,其中有广告,有时在 iframe 中,有时不。
问题是页面超时(60 秒),即使它看起来已加载。我认为这可能是广告或其他一些跟踪,因此我想将白名单添加到我们的资源 URL 以便删除任何广告或类似资源。
这可能不是 100% 准确的测试方法,但对于我们的案例来说已经足够好了。
我已经尝试在 beforeEach 中使用它(不是最优的,但如果它可行,我会把它变成一个命令并使用它)
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})`);
});
}
});
我也在cypress.json
中找到了一些黑名单选项,但我需要白名单而不是黑名单。
赛普拉斯有一个默认的白名单,可以在这里找到信息: https://docs.cypress.io/api/commands/server.html#Options
Change the default whitelisting
cy.server() comes with a whitelist function that by default filters out any requests that are for static assets like .html, .js, .jsx, and .css.
Any request that passes the whitelist will be ignored - it will not be logged nor will it be stubbed in any way (even if it matches a specific cy.route()).
The idea is that we never want to interfere with static assets that are fetched via Ajax.
The default whitelist function in Cypress is:
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)
}
You can override this function with your own specific logic:
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.
}
})
看来您可以通过在 cypress.server 上设置选项来永久覆盖该白名单:https://docs.cypress.io/api/cypress-api/cypress-server.html#Syntax