量角器单击链接并将服务器响应与文件进行比较

Protractor click link and compare server response with file

本文关键字:文件 比较 响应 服务器 单击 链接 量角器      更新时间:2023-09-26

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

用户通过单击链接下载文件,这将触发对该文件的正常 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
    });
});

sendKeys大多数其他量角器 API 调用返回一个承诺。因此,正确的方法是让您的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
        ...
    });
  });
});