CasperJs在开始之前做些什么?
问题描述:
我使用JWT来管理登录状态,所以我需要在运行casper.start
之前清除本地存储。这怎么可能?CasperJs在开始之前做些什么?
喜欢的东西:
casper.then(function() {
casper.evaluate(function() {
localStorage.clear()
})
})
casper.start('http://localhost:3000', function() {
test.assertUrlMatch('http://localhost:3000')
})
答
您可以拨打casper.start
不带任何参数初始化内部数据,然后做你的东西:
casper.start()
.then(function() {
casper.evaluate(function() {
localStorage.clear()
})
})
.thenOpen('http://localhost:3000', function() {
test.assertUrlMatch('http://localhost:3000')
})
的问题是,如果你调用casper.start
没有任何网址,当您尝试清除localStorage
时,页面将保留大约:空白。基本上有两种解决方案:
- 使用
fs
模块PhantomJS的删除localStorage的数据库是在temporary files directory for PhantomJS。 -
打开目标页面,清除localStorage并再次打开目标页面。
var url = "..."; casper.start(url, function() { this.evaluate(function() { localStorage.clear() }) }) .thenOpen(url, function() { test.assertUrlMatch('http://localhost:3000') })