我们的应用程序在React + NextJS中。框架就像(浏览器代码->(调用)->下一个JS Api->(调用)->外部Rest API)。我想使用Testcafe执行端到端测试,在这里我将仅模拟外部API调用。我们怎样才能达到同样的目的?
是的,您可以使用testcafe编写E2E测试。它模拟实际的用户体验并与您的网站进行交互。它不依赖于基础技术堆栈。您只需要托管您的应用程序并提供testcafe配置的URL。使用常规的CSS选择器选择页面元素并进行断言。您还可以拦截API请求并为API使用模拟数据。不建议在E2E测试中使用模拟功能。
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
})