javascript - CasperJs 在开始之前做了什么?

标签 javascript testing casperjs functional-testing

我使用 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/

相关文章:

javascript - 在映射函数中的 .then 内部分配变量

c# - Selenium IWebElement 匹配选择器

selenium - 无法通过 type 属性或 xpath 定位登录框

ruby-on-rails - capybara :如何断言给定数量的元素存在

javascript - Casper.js 获取网页并显示

Javascript 正则表达式替换第 6 次出现的 ; (分号)与 ;\n(分号和换行符)

Javascript DOM 鼠标事件不适用于 IE 和 Mozilla

javascript - 使用 JavaScript 链接到 Gmail 消息版本的弹出窗口

phantomjs - 通过 PhantomJS 调用时找不到模块 'casper'

javascript - 使用casperjs时如何等待页面加载?