3

我正在构建一个用户上传文件的系统。我已经设法使用量角器测试文件上传,但现在我需要验证从服务器请求原始文件时响应与上传文件相同。

用户通过单击链接来下载文件,这会触发对该文件的正常 GET 请求。由于我处理的是纯文本文件,因此下载的文件不会作为附件提供,而是显示在浏览器中。相关的PHP代码是:

header('Content-Type: text/plain');
header('Content-Length: ' . $file->getSize());
header('Content-Disposition: filename="file.txt"');

$file->openFile('rb')->fpassthru();

我有两个问题:

  1. 如何让量角器等到整个文件下载完成?
  2. 如何将下载的文件与我上传的文件进行比较?

这是我到目前为止得到的:

var path = require('path');

function uploadFile(filename) {
    var absolutPath = path.resolve(__dirname, filename);
    $('input#fileupload').sendKeys(absolutPath);
}

describe('Download', function() {

    it('should be exactly the same as an uploaded text file', function() {
        uploadFile('data/2.txt');

        browser.wait(function() {
            return element(by.id('download-button')).isPresent();
        });

        element(by.id('download-button')).click();

        // Wait until page is loaded
        // Compare uploaded file with downloaded file
    });
});
4

1 回答 1

1

sendKeys因为大多数其他量角器 API 调用返回一个promise。因此,正确的方法是让您的uploadFile辅助方法返回该承诺,然后用于then执行下一步。

我还没有尝试过,但这里有一些可能有效的代码:

function uploadFile(filename) {
  var absolutPath = path.resolve(__dirname, filename);
  return $('input#fileupload').sendKeys(absolutPath);
}

describe('Download', function() {

  it('should be exactly the same as an uploaded text file', function() {
    uploadFile('data/2.txt').then(function () {
      // add test code here if you want to
      ...
      element(by.id('download-button')).click().then(function () {
        // now you should be all set to do your test
        ...
    });
  });
});
于 2014-12-30T12:32:34.873 回答