ClientFunction:_axios2 未定义

ClientFunction: _axios2 is not defined

我是 运行 TestCafe UI 自动化,使用 ClientFunctions 触发 API 请求(以便我可以传递会话 cookie)。

目前我有一个带有 fetch 的 ClientFunction,它工作正常...除了我们现在正在测试 IE 11 并且不支持 Fetch。

获取代码:

const fetchRequestClientFunction = ClientFunction((details, endpoint, auth, method) => {
  return window
    .fetch(endpoint, {
      method,
      credentials: 'include',
      headers: new Headers({
        accept: 'application/json',
        'Content-Type': 'application/json',
      }),
      body: JSON.stringify(details),
    })
    .then(httpResponse => {
      if (httpResponse.ok) {
        return httpResponse.json();
      }
      return {
        err: true,
        errorMessage: `There was an error trying to send the data ${JSON.stringify(
          details
        )} to the API endpoint ${endpoint}. Status: ${httpResponse.status}; Status text: ${httpResponse.statusText}`,
      };
    });
});

然而,当我尝试将其切换到 axios 时...不是那么多:

import axios from 'axios';

const axiosRequest = ClientFunction((details, endpoint, auth, method) => {
  return axios({
      method,
      auth,
      url: endpoint,
      data: details,
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      timeout: 3000,
    })
    .then(httpResponse => {
      if (httpResponse.status < 300) return httpResponse;

      return {
        err: true,
        errorMessage: `There was an error trying to send the data ${JSON.stringify(
          details
        )} to the API endpoint ${endpoint}. Status: ${httpResponse.status}; Status text: ${httpResponse.statusText}`,
      };
    });
});

尝试使用 window.axios,并将 axios 作为依赖项传递。我也试过在没有 ClientFunction 的情况下发出 axios 请求...尽管收到 200 的响应,但网站没有按预期更新。

每次我得到 _axios2 is not definedwindow.axios is not a function。非常感谢您提供指导。

TestCafe ClientFunctions 仅允许可序列化对象作为依赖项。您需要 axios 在客户端才能发送这样的请求。