【问题标题】:How to connect to SFTP server in Cypress test?如何在 Cypress 测试中连接到 SFTP 服务器?
【发布时间】:2022-01-23 04:02:40
【问题描述】:

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

这是我目前的测试

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


    【解决方案1】:

    在测试中建立这样的连接是行不通的。这是因为 cypress 不与主机提供的 Node.js 进程通信。在 cypress 中,如果我们需要运行节点代码,我们需要使用他们所谓的 cy.task 这是他们文档的链接 - https://docs.cypress.io/api/commands/task#Examples

    这就是为什么您需要在任务内的cypress/plugins/index.js 文件中建立此连接,然后在测试中使用此任务。

    这里是一个使用 ssh 连接 mysql 的例子 - How do I connect mysql with cypress through ssh tunneling?

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 2020-10-22
      • 2013-10-17
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多