RT, 目前有一个url地址,http://test.com, 该地址需要用post请求的方式在浏览器中自动打开
目前我尝试了两种方案:
方案一:
利用open库或者opn库,该方案只能发起get请求,无法发起post请求,不满足要求
open('http://test.com', {app: {name: 'google chrome', arguments: '--incognito'}});
方案二:
利用Puppeteer来模拟浏览器发起post请求,Puppeteer我查阅文档,也是只有get请求,没有post请求
const browser = await puppeteer.launch({
timeout: 5000, // 超时时间,单位为毫秒
headless: true,
args: [],
});
const page = await browser.newPage();
await page.goto('http://test.com', {
waitUntil: "load",
});
Puppeteer模似的是用户的操作,而用户也是没有办法直接在浏览器里输入地址后使用post方法来打开这个地址的,如果用户输入地址来打开某个页面,则必然是get方法。
但用户在浏览器中点击form标签,或是触发ajax请求就不一样了。
所以测试post方法只需要多一个页面即可。
先在这个页面中写好发起post请求的代码,比如增加一个请求方式为post的form,然后在Puppeteer中模拟点击提交按钮即可。