Cypress测试中如何连接SFTP服务器?

问题描述 投票:0回答:2

我正在尝试使用 ssh2-sftp-client 连接到 SFTP 服务器 我的 Cypress 测试中的 NPM 包。

这是我目前的测试

describe('example to-do app', () => {
    it('displays two todo items by default', () => {
        let Client = require('ssh2-sftp-client');
        let sftp = new Client();
        
        sftp.connect({
          host: 'myHost',
          port: 'myPort',
          username: 'myUsername',
          password: 'myPassword'
        }).then(() => {
          return sftp.list('/reports');
        }).then(data => {
          console.log(data, 'the data info');
        }).catch(err => {
          console.log(err, 'catch error');
        });
    })
})

目前,当我运行测试时,出现此错误:

Cannot read properties of undefined (reading 'DEFLATE')
node_modules/ssh2/lib/protocol/zlib.js:7:1
   5 |   createInflate,
   6 |   constants: {
>  7 |     DEFLATE,
     | ^
   8 |     INFLATE,
   9 |     Z_DEFAULT_CHUNK,
  10 |     Z_DEFAULT_COMPRESSION,

有人可以告诉我如何解决这个问题吗?

cypress ssh2-sftp ssh2-sftp-client
2个回答
3
投票

在测试中建立这样的连接是行不通的。这是因为 cypress 不与主机提供的 Node.js 进程通信。在 cypress 中,如果我们需要运行节点代码,我们需要使用他们所谓的

cy.task
这是他们文档的链接 - https://docs.cypress.io/api/commands/task#Examples

这就是为什么您需要在任务内的

cypress/plugins/index.js
文件中建立此连接,然后在测试中使用此任务。

这里是使用 ssh 连接 mysql 的示例 - 如何通过 ssh 隧道连接 mysql 与 cypress?


0
投票

我正在研究一个类似的问题,OP 和@YuliaP 提供了一些有用的信息。由于我使用的是最新版本的 Cypress (13.6.6),我想我应该发布我的解决方案以提供一些详细信息。我确实安装了 ssh2-sftp-client,https://www.npmjs.com/package/ssh2-sftp-client。然后,我将以下内容添加到 cypress.config.js(信用:很多代码来自上一个链接):

const { defineConfig } = require("cypress");
const sftpClient = require('ssh2-sftp-client');

module.exports = defineConfig({
  e2e: {
    specPattern: 'cypress/e2e/**/*.{js,jsx,ts,tsx}',
    setupNodeEvents(on, config) {
      const username = process.env.SFTP_USERNAME;
      const password = process.env.SFTP_PASSWORD;
      console.log('sftp username: ', username);
      const localFilePath = '/Work/(research)/Angular/cypress-sftp/git/sftp-app/src/';
      const hostnm = 'mysftp.domain.org';
      // implement node event listeners here
      on('task', {
        'sftp:put': (filename) => {
          let client = new sftpClient();
          client.connect({
            host: hostnm,
            port: '2222',
            username: username,
            password: password
          }).then(() => {
            // always good to start path with a slash /
              return client.put(localFilePath + filename, '/download/' + filename);
          }).then(data => {
            console.log(data);
            client.end();
          }).catch(err => {
            console.log(err);
          });
          return null;
        }
      })

      on('task', {
        sftplist(folder) {
          let client = new sftpClient();
          client.connect({
            host: hostnm,
            port: '2222',
            username: username,
            password: password
          }).then(() => {
              return client.list(folder);
          }).then(fileInfo => {
            console.log(fileInfo);
            client.end();
          }).catch(err => {
            console.log(err);
          });
          return null;
        }
      })

    }
  }
});

就我而言,无需在插件文件夹中创建index.js。这种方法适用于传统的赛普拉斯。这是一个演示测试:

describe('Connect to SFTP and perform operations', () => {
 it('Put file to fstp server', () => {
  cy.task('sftp:put', 'sftp-data.txt');
 })
 it('Read folder on fstp server', () => {
    cy.task('sftplist', '/download');
 })
})

这只是一个示例,因此可能可以进一步调整。享受

© www.soinside.com 2019 - 2024. All rights reserved.