我使用 JWT 来管理登录状态,所以我需要在运行 casper.start
之前清除本地存储。这怎么可能?
类似于:
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')
})
问题是,如果您在没有任何 URL 的情况下调用 casper.start
,当您尝试清除 localStorage
时,页面将停留在 about:blank 状态。基本上有两种解决方案:
- 使用PhantomJS 的
fs
模块删除temporary files directory for PhantomJS 中的localStorage 数据库。 . 打开目标页面,清空localStorage,重新打开目标页面。
var url = "..."; casper.start(url, function() { this.evaluate(function() { localStorage.clear() }) }) .thenOpen(url, function() { test.assertUrlMatch('http://localhost:3000') })
关于javascript - CasperJs 在开始之前做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36935693/