背景
拼多多接口升级后,请求增加了校验字段,看了源代码,破解需要花费较多时间,于是决定与数据部门合作,顺便问了下他们的实现方式,谈到了PhantomJS实现的方案,所以回来打算研究一下。
关键词介绍
无头浏览器(PhantomJS) - 通过JS在后台运行有关浏览器的一切操作省去可视化浏览器的操作,提高自动化的兼容性稳定性
有头浏览器(selenium框架)
安装
两种方式
- npm 这边有两个相关的npm包 一个是phantomjs-prebuilt 一个是phantom-node
看了下phantom-node的api更接近官网的用法 所以我们直接选择npm install phantom –save(node v6+)
- 官网下压缩包+改路径
官方guide/应用场景
- quick start: webpage render exit
- 页面自动化操作 - page automation: 获取html,dom
- 屏幕捕获 - screen capture: 将访问的页面生成图片
- 无需浏览器的Web测试 - Headless Testing with PhantomJS: 基于PhantomJS的headless测试
- Casper.js is useful to build scripted navigation and testing
- Lotte adds jQuery-like methods, chaining, and more assertion logic
- WebSpecter is a BDD-style acceptance test framework for web applications
- 利用Jenkins+Phantomas进行前端性能自动化测试
- 网络监控 - Network Monitoring with PhantomJS: 使用onResourceRequested 和 onResourceReceived追踪
netlog
- 更多例子
API
- 实例化 instance = phantom.create() page = instance.createPage()
- 打开一个页面 page.open()
- 将打开的页面存储为pdf page.render()
- 为当前页面导入其他js page.includeJs()
- 退出 instance.exit()
比较新的替代插件
Puppeteer
功能差不多,api名称有所差异
launch newPage
goto
screenshot