【问题标题】:CasperJs do something before start?CasperJs 在开始之前会做些什么?
【发布时间】:2016-04-29 10:17:08
【问题描述】:

我使用 JWT 来管理登录状态,所以我需要在运行 casper.start 之前清除本地存储。这怎么可能?

类似:

casper.then(function() {
  casper.evaluate(function() {
    localStorage.clear()
  })
})

casper.start('http://localhost:3000', function() {
  test.assertUrlMatch('http://localhost:3000')
})

【问题讨论】:

    标签: javascript testing casperjs functional-testing


    【解决方案1】:

    您可以不带任何参数调用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')
      })
      

    【讨论】:

      猜你喜欢
      • 2013-06-04
      • 1970-01-01
      • 2015-07-11
      • 2017-05-14
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      相关资源
      最近更新 更多