带有 cookie 的 Testcafe 请求 [英] Testcafe request with cookies

查看:32
本文介绍了带有 cookie 的 Testcafe 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to find a method in testcafes API similar to Cypress' request.

Cypress 的 request 会将任何 cookie 附加到请求中已经存在于浏览器中,因此 http 请求看起来像是来自浏览器/用户.

Cypress' request will attach any cookies to the request that already exist in the browser so that http requests will look like they're being made from the browser / user.

testcafe 有没有类似的功能?

Is there a similar function in testcafe?

推荐答案

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

You can modify your cookies using the ClientFunctions mechanism. These cookies will be added to further requests. This approach is safe, since every test in TestCafe starts with clear cookies, so cookies modification will not affect other tests. I prepared an example, please see it:

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();
});

这篇关于带有 cookie 的 Testcafe 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆