简体   繁体   English

如何将自定义请求 header 添加到 testcafe 测试套件?

[英]How to add custom request header to testcafe test suite?

I have a bunch of tests that I am running through testcafe.我有一堆测试正在通过 testcafe 运行。 Now I need to add a custom request header for each test that uniquely identifies the call is originating from the testcafe suite and not a real user.现在我需要为每个测试添加一个自定义请求 header,以唯一标识调用来自 testcafe 套件而不是真实用户。

Is there a way to add the custom header to all the test cases at once?有没有办法一次将自定义 header 添加到所有测试用例?

I was looking at this but it seems like I would need to update each fixture to get this working.我正在看这个,但似乎我需要更新每个固定装置才能使其正常工作。 So, I wanted to know if there's a way I can set it on a top level file before calling the test suite?所以,我想知道是否有办法在调用测试套件之前将其设置在顶级文件中?

EDIT:编辑:

So, this is what I am currently doing.所以,这就是我目前正在做的事情。 I have created a new file that contains the class:我创建了一个包含 class 的新文件:

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;

And then in my each test file, update the fixtures to be like this:然后在我的每个测试文件中,将固定装置更新为如下所示:

import { customHeader } from 'customer_header'

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

Does this make sense?这有意义吗?

Currently there is no way to specify hooks on the test run level.目前无法在测试运行级别指定挂钩。 But, if updating each fixture is not reasonable in your case, you can use the workaround posted in the discussion about this feature.但是,如果在您的情况下更新每个固定装置不合理,您可以使用有关此功能的讨论中发布的解决方法。 In order to apply your request hook to each fixture in the test suite you'll need to change "setup.js" (from the workaround above) as follows:为了将您的请求挂钩应用到测试套件中的每个装置,您需要更改“setup.js”(来自上面的解决方法),如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM