带有 cookie 的 Testcafe 请求

oka*_*try 3 javascript ajax typescript testcafe cypress

我试图在 testcafes API 中找到一种类似于 Cypress 请求的方法。

Cypress 的请求会将任何 cookie 附加到浏览器中已经存在的请求中,以便 http 请求看起来像是从浏览器/用户发出的。

testcafe 中是否有类似的功能?

Ale*_*aev 5

您可以使用ClientFunctions机制修改您的 cookie 。这些 cookie 将被添加到进一步的请求中。这种方式是安全的,因为 TestCafe 中的每个测试都是从清除 cookie 开始的,所以 cookie 的修改不会影响其他测试。我准备了一个例子,请看:

import { ClientFunction } from 'testcafe';

const setCookie = ClientFunction(() => {
    document.cookie = "myCustomCookie=myCustomValue";
});

fixture `fixture`
    .page `http://google.com`;

test(`1`, async t => {
    await setCookie();

    await t.typeText('input[type=text]', 'test');

    await t.debug();
});
Run Code Online (Sandbox Code Playgroud)