【问题标题】:How to add custom request header to testcafe test suite?如何将自定义请求标头添加到 testcafe 测试套件?
【发布时间】:2020-12-03 04:21:40
【问题描述】:

我正在通过 testcafe 运行一堆测试。现在我需要为每个测试添加一个自定义请求标头,该标头唯一标识调用来自 testcafe 套件而不是真实用户。

有没有办法一次将自定义标头添加到所有测试用例中?

我正在查看this,但似乎我需要更新每个固定装置才能使其正常工作。所以,我想知道在调用测试套件之前是否可以在顶级文件中设置它?

编辑:

所以,这就是我目前正在做的事情。我创建了一个包含该类的新文件:

import { RequestHook } from 'testcafe';

class CustomHeader extends RequestHook {
    constructor () {
        // No URL filtering applied to this hook
        // so it will be used for all requests.
        super();
    }
    
    onRequest (e) {
        e.requestOptions.headers['my_custom_variable'] = 'my_value';
    }

    onResponse (e) {
        // This method must also be overridden,
        // but you can leave it blank.
    }
}

const customHeader = new CustomHeader();
export default customHeader;

然后在我的每个测试文件中,将fixture 更新成这样:

import { customHeader } from 'customer_header'

fixture(`Test app avail`)
  .page(appURL)
  .requestHooks(customHeader)
  .beforeEach(async() => {
    await myTestfunction();
  });

这有意义吗?

【问题讨论】:

    标签: javascript reactjs testing automated-tests testcafe


    【解决方案1】:

    目前无法在测试运行级别指定挂钩。但是,如果在您的情况下更新每个夹具不合理,您可以在有关此功能的讨论中使用解决方法posted。为了将您的请求挂钩应用到测试套件中的每个夹具,您需要更改“setup.js”(来自上面的解决方法),如下所示:

    export const fixture = (...args) => global.fixture(...args)
        .requestHooks(customHeader)
        .beforeEach(async t => {
            console.log('each');
        });
    

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多