如何在每次测试之间完美隔离和清除环境? [英] How to perfectly isolate and clear environments between each test?

查看:155
本文介绍了如何在每次测试之间完美隔离和清除环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CasperJS连接到SoundCloud.有趣的是,一旦您登录并稍后重新运行登录功能,以前的登录仍处于活动状态.在继续之前,这里是代码:

I'm trying to connect to SoundCloud using CasperJS. What is interesting is once you signed in and rerun the login feature later, the previous login is still active. Before going any further, here is the code:

casper.thenOpen('https://soundcloud.com/', function() {
  casper.click('.header__login');

  popup = /soundcloud\.com\/connect/;

  casper.waitForPopup(popup, function() {
    casper.withPopup(popup, function() {
      selectors = {
        '#username': username,
        '#password': password
      };

      casper.fillSelectors('form.log-in', selectors, false);

      casper.click('#authorize');
    });
  });
});

如果您至少运行两次此代码,您应该看到出现以下错误:

If you run this code at least twice, you should see the following error appears:

CasperError: Cannot dispatch mousedown event on nonexistent selector: .header__login

如果您分析日志,您将看到第二次,您将被重定向到 https://soundcloud.com/stream 表示您已经登录.

If you analyse the logs you will see that the second time, you were redirected to https://soundcloud.com/stream meaning that you were already logged in.

我做了一些研究,以清除每次测试之间的环境,但是以下几行似乎无法解决问题.

I did some research to clear environments between each test but it seems that the following lines don't solve the problem.

phantom.clearCookies()
casper.clear()
localStorage.clear()
sessionStorage.clear()

从技术上讲,我真的很想了解这里发生的事情.也许SoundCloud构建了一个系统,还可以在服务器端存储一些变量.在这种情况下,我必须先注销才能登录.但是我的问题是,如何才能完美隔离并清除每个测试之间的所有内容?有人知道如何在每次测试之间使环境变得无符号吗?

Technically, I'm really interested about understanding what is happening here. Maybe SoundCloud built a system to also store some variables server-side. In this case, I would have to log out before login. But my question is how can I perfectly isolate and clear everything between each test? Does someone know how to make the environment unsigned between each test?

推荐答案

要清除服务器端会话缓存,请调用:phantom.clearCookies();对我有用.这清除了我在测试文件之间的会话. 此处的示例:

To clear server-side session cache, calling: phantom.clearCookies(); did the trick for me. This cleared my session between test files. Example here:

casper.test.begin("Test", {
  test: function(test) {
    casper.start(
      "http://example.com",
      function() {
        ... //Some testing here
      }
    );
    casper.run(function() {
      test.done();
    });
  }, 
  tearDown: function(test) {
    phantom.clearCookies();
  }
});

如果仍然有问题,请检查执行测试的方式.

If you're still having issues, check the way you are executing your tests.

这篇关于如何在每次测试之间完美隔离和清除环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆