使用Nightwatch.js测试经过身份验证/未经身份验证的下载链接

时间:2015-02-03 16:28:54

标签: node.js selenium nightwatch.js

我知道这里有一个非常相似的问题:Testing download links with Nightwatch.js,但这种情况略有不同。

我也试图使用夜间测试来测试下载,但由于浏览器的行为向用户提供弹出窗口,因此并不是那么简单。

我要做的是只使用节点下载文件,而不是使用selenium(如此python版本:https://stackoverflow.com/a/24998532/447074

所以我做了这个自定义断言:

downloadFile.js

var util = require('util');
var http = require('http');
var url = require('url');

exports.assertion = function(options, expected_status, msg) {
  this.message = msg || util.format('Testing if authenticated file download works');
  this.file_url = options.file_url;
  this.cookie_content = options.cookie_content;
  this.expected = expected_status;
  this.pass = function(value) {
    return this.expected == value
  };

  this.value = function(response) {
    return response.statusCode;
  };

  this.command = function(callback) {
    var options = url.parse(this.file_url);
    options.headers = {
      'Cookie': this.cookie_content
    };
    http.get(options, callback);
    return true;
  };

};

在这样的测试用例中使用它:

// I would not use this here, but I added this for this post...
"Get cookie": function(browser) {
  var settings = browser.globals;
  var state = browser.globals.state;

  browser
    .getCookies(function(response) {
      state.sessionid = "sessionid=" + response['value'].filter(function(cookie_value){
        return cookie_value['name'] == 'sessionid';
      })[0]['value'];
    })
},


"An authenticated user can download the file": function(browser) {
  var settings = browser.globals;
  var state = browser.globals.state;

  browser
    .assert.downloadFile({file_url: "http://example.com/somefile.pdf", cookie_content: state.sessionid}, 200, "Testing Authenticated download");
},

"An unauthenticated user can not download the file": function(browser) {
  var settings = browser.globals;
  var state = browser.globals.state;

  browser
    .assert.downloadFile({file_url: "http://example.com/somefile.pdf", cookie_content: "sessionid= wrong-session;"}, 302, "Testing Unauthenticated dowload");

},

它“几乎”正在工作,但是在第一次测试之后,在第一次downloadFile断言之后,测试“停止”。

以下是控制台的结果

nightwatch -c ./nightwatch.json --test tests/documents/upload_test.js

[Upload Test] Test Suite
========================
Setting up...

Running:  Get cookie 


No assertions ran.

Running:  An authenticated user can download the file 

✔  Testing Authenticated dowload

可以在夜班表中做出这种断言吗? 为什么它会停止测试?

谢谢!

1 个答案:

答案 0 :(得分:1)

好的,我通过再次阅读自定义断言文档找到了解决方案:http://nightwatchjs.org/guide#custom-assertions

断言的command函数需要返回this http://nightwatchjs.org/guide#custom-assertions

这是 downloadFile.js 断言:

var util = require('util');
var http = require('http');
var url = require('url');

exports.assertion = function(options, expected_status, msg) {
  this.message = msg || util.format('Testing if authenticated file download works');
  this.file_url = options.file_url;
  this.cookie_content = options.cookie_content;
  this.expected = expected_status;
  this.pass = function(value) {
    return this.expected == value
  };

  this.value = function(response) {
    return response.statusCode;
  };

  this.command = function(callback) {
    var options = url.parse(this.file_url);
    options.headers = {
      'Cookie': this.cookie_content
    };
    http.get(options, callback);
    return this;
  };

};
相关问题