简体   繁体   English

如何使用 testcafe 测试对 API 调用的重试

[英]How to test retries to an API call using testcafe

I am using Testcafe for my integration tests, and I want to test the scenario where my app retries an API call after receiving an error.我正在使用 Testcafe 进行集成测试,并且我想测试我的应用程序在收到错误后重试 API 调用的场景。 I am using the async-retry library to make my calls.我正在使用 async-retry 库来拨打电话。 Retry is a utility I created to wrap the API call so I could wrap boilerplate code for calling async-retry: Retry是我创建的用于包装 API 调用的实用程序,因此我可以包装样板代码以调用 async-retry:

 const response = await Retry( () => { return fetch( buildUrl(env, csrf, '/api/myCustomCall', queryParams), options ); }, 'getRecommendations', { onRetry: () => { console.log('RETRYING'); } } );

For posterity, this is the Retry utility:对于后代,这是Retry实用程序:

 import retry, { AsyncRetryOptions } from 'async-retry'; export const Retry = ( func: () => Promise<any>, name: string, opts: AsyncRetryOptions = {} ): Promise<any> => { const retryOptions = { retries: opts.retries || 3, factor: opts.factor || 2, minTimeout: opts.minTimeout || 3000, maxTimeout: opts.maxTimeout || Infinity, randomize: opts.randomize || true, onRetry: (error: Error, attempt: number) => { console.error( `${new Date().toString()} - ${name} failed ${attempt} times, trying again` ); } }; return retry(func, retryOptions); };

This is my test:这是我的测试:

 test.requestHooks( RequestMock() .onRequestTo(/myCustomCall/) .respond({ error: 'Bad value for request parameter' }, 400, responseHeaders) )('Recommendation request retries 3 times', async (t) => { await playgroundInit(t); await t.expect(recommendationLogger.requests.length).eql(4); });

playgroundInit is a utility function that does things like login and navigating to the page I am testing. playgroundInit是一个实用函数,它可以执行诸如登录和导航到我正在测试的页面之类的操作。 When I was developing, I used the Chrome devtools to block the API request in order to test the retries, which was successful.我在开发的时候,为了测试重试,使用了 Chrome devtools 来阻止 API 请求,成功了。 I saw the retries working.我看到重试工作。 However, I'd like to mimic this in my test to automate this behavior.但是,我想在我的测试中模仿这一点来自动化这种行为。 How do you mock a request in testcafe to trigger retries?您如何在 testcafe 中模拟请求以触发重试?

As I understand it, you are making your API call from the TestCafe test code, but not from the tested page.据我了解,您是从 TestCafe 测试代码而不是从测试页面进行 API 调用。

In this case, the RequestMock mechanism will not be applied.在这种情况下,将不应用 RequestMock 机制。 RequestMock works from API calls that were made from the website page. RequestMock 使用从网站页面进行的 API 调用。 In your case, you initiate a request yourself, so the TestCafe proxy does not process it.在您的情况下,您自己发起请求,因此 TestCafe 代理不会处理它。

If you wish to make RequestMock work you can try making your API calls from the browser but not test side.如果您希望 RequestMock 工作,您可以尝试从浏览器而不是测试端进行 API 调用。 To do this, you can use the ClientFunction mechanism that allows you to inject JS code to the website page.为此,您可以使用允许您将 JS 代码注入网站页面的 ClientFunction 机制。

Since TestCafe 1.20.0, you can use the dedicated t.request method to send requests in your tests.从 TestCafe 1.20.0 开始,您可以使用专用的t.request方法在测试中发送请求 Sending such requests will trigger request hooks that you set in your test (if these requests fulfill the request hooks' parameters).发送此类请求将触发您在测试中设置的请求挂钩(如果这些请求满足请求挂钩的参数)。 You can learn more about TestCafe API testing in the guide .您可以在指南中了解有关 TestCafe API 测试的更多信息。

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

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