赛普拉斯仅将我们的域名列入白名单

时间:2018-10-12 08:44:48

标签: cypress

我有一个页面,其中有时在iframe中有广告,有时没有。

问题是页面超时(60秒),即使它看起来已加载。我认为这可能是广告或其他跟踪信息,因此我想在我们的资源网址中添加白名单,以便删除任何广告或类似资源。

这可能不是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中找到了一些黑名单选项,但是我需要白名单而不是黑名单。

1 个答案:

答案 0 :(得分:1)

Cypress具有默认的白名单,可以在以下位置找到信息: https://docs.cypress.io/api/commands/server.html#Options

  

更改默认白名单

     

cy.server()带有白名单功能,默认情况下会对其进行过滤   排除对.html,.js,.jsx和   .css。

     

任何通过白名单的请求都将被忽略-不会   也不会以任何方式存根(即使它与   特定的cy.route())。

     

这个想法是,我们永远不想干扰那些   通过Ajax获取。

     

赛普拉斯的默认白名单功能是:

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.
}
})

您似乎可以通过在cypress.server上设置选项来永久覆盖该白名单:https://docs.cypress.io/api/cypress-api/cypress-server.html#Syntax

相关问题