我们可以在 testcafe 中编写具有 Next JS API 的应用程序的端到端测试吗?

Can we write e2e tests of a app having Next JS API in testcafe?

我们的应用程序采用 React + NextJS。框架就像(浏览器代码->(调用)->Next JS Api->(调用)->外部休息API)。我想使用 Testcafe 执行 E2E 测试,我将只模拟外部 API 调用。我们怎样才能达到同样的效果?

是的,您可以使用 testcafe 编写 E2E 测试。它模拟实际用户体验并与您的网站互动。它不依赖于底层技术堆栈。您只需要托管您的应用程序并将 url 提供给 testcafe 配置。使用常规 css 选择器来选择页面元素并进行断言。您还可以拦截 API 请求并为 API 使用模拟数据。端到端测试不推荐模拟。

API 响应的示例代码:

import { RequestLogger } from 'testcafe'

const logger= RequestLogger(['Domain URLs Array'], {
 logResponseHeader: true,
 logResponseBody: true
})

fixture(`description`).page('app url').requestHooks(logger)

test('description', async () => {

 const domain1Res = logger.requests[0].response
 const domain1ResStr = domain1Res.body.toString()
 const domain1ResJson = domain1ResStr.length ? JSON.parse(domain1ResStr) : null

 // domain1ResJson is the API response object, use it in assertions

})