Automated tests 模拟API根据请求URL';s动态部分
我想模拟API调用,以便 请求Automated tests 模拟API根据请求URL';s动态部分,automated-tests,httprequest,e2e-testing,web-testing,testcafe,Automated Tests,Httprequest,E2e Testing,Web Testing,Testcafe,我想模拟API调用,以便 请求 http://localhost:8080/api/test/ 答复如下: {date:,data:'my cool data'} 其中不是固定的(该请求在过去7天内发出7次) 如何在TestCafé中为此创建一个模拟?请注意,响应数据取决于请求URL。将index.html和index.js文件放在同一文件夹中。然后在终端中运行testcafe chrome test.js命令 index.html 页 发送请求 var sendRequestBtn=doc
http://localhost:8080/api/test/
答复如下:
{date:,data:'my cool data'}
其中
不是固定的(该请求在过去7天内发出7次)
如何在TestCafé中为此创建一个模拟?请注意,响应数据取决于请求URL。将
index.html
和index.js
文件放在同一文件夹中。然后在终端中运行testcafe chrome test.js
命令
index.html
页
发送请求
var sendRequestBtn=document.getElementById('sendRequestBtn');
var responseData=document.getElementById('response');
sendRequestBtn.addEventListener('click',函数(){
取('http://localhost:8080/api/test/2019-07-12')
。然后(响应=>{
返回response.json();
})
。然后(json=>{
responseData.textContent=JSON.stringify(JSON,null,4);
})
.catch(e=>console.error(e));
});
test.js
从“testcafe”导入{RequestMock};
const mock=RequestMock()
.onRequestTo(/http:\/\/localhost:8080\/api\/test\/.*/)
.响应((请求、回复)=>{
res.headers['access-control-allow-origin']='*';//这是必需的,因为在本例中TestCafe是通过文件协议加载页面的。
const dateUrlPart=req.path.replace('/api/test/','');
res.setBody({
日期:dateUrlPart,
数据:“我的酷数据”
});
});
固定装置`
.page(“./index.html”)
.1(模拟);
测试('test',异步t=>{
等待t.click(“#sendRequestBtn”)。等待(1000);
});
为什么不使用一个支持动态响应的在线模拟工具?就像你可以使用在线工具来模拟API一样。比如: