• 关于 Cypress 的启动和 cy.visit 的调试


    npx 是 npm 自带的命令行工具:

    在项目根目录下,使用命令行 npx cypress open 启动:

    也可以利用 yarn 启动:yarn run cypress open

    或者是直接执行 node_modules bin 文件夹下的二进制命令。

    这样就可以成功启动 Cypress Launchpad:

    package.json 添加如下的 script:

    "scripts": {
        "cypress:open": "cypress open"
      }
    
    • 1
    • 2
    • 3

    然后可以用如下的命令启动:

    npm run cypress:open

    npm install 安装完毕后,工程目录下有个 cypress\integration 文件夹,里面有很多 sample 文件。

    每个 test 一开始都是一个 blank state,因此需要在 beforeEach 函数调用里进行初始化。

    在这个 spec 执行的时候,cy 为什么就可用了?

    单步调试 todo.spec.js,在 webpack:// 文件夹下:

    具体的实现位置?

    为什么会在这个 url 下面?https://example.cypress.io/__cypress/runner/cypress_runner.js

    https://example.cypress.io/todo

    这是一个开发好的 web 应用:

    cy 的方法都是 generic 注入进去的:

    记住这个文件名:cypress_runner.js

    运行队列:

    队列是一个 object:

    Promise 似乎不是原生的 Promise:

    如何才能看到 cy.visit 访问网站的准确动作?

    cy.visit 会立即返回,而不会同步的去访问网站:

    it('let me debug like a fiend', () => {
      cy.visit('/my/page/path')
    
      cy.get('[data-testid="selector-in-question"]')
    
      debugger // Doesn't work
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    这里是 Cypress 统一处理 action 的地方:

    action(eventName, ...args) {
                    // normalizes all the various ways
                    // other objects communicate intent
                    // and 'action' to Cypress
                    debug(eventName);
    
                    switch (eventName) {
                    case 'recorder:frame':
                        return this.emit('recorder:frame', args[0]);
    
                    case 'cypress:stop':
                        return this.emit('stop');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    使用 onBeforeLoad 钩子,我们可以在 Web 应用的主页,加载之前,注入一些数据给它:

    it('can modify window._bootstrappedData', function () {
          // in this solution we use cy.visit({onBeforeLoad: ...})
          // to modify the window._bootstrappedData global so that
          // it's passed into our App.start() method
    
          const data = {
            env: 'test',
            api: 'https://test-api.company.com',
          }
    
          cy.visit('/bootstrap.html', {
            onBeforeLoad: (win) => {
              win._bootstrappedData = data
            },
          })
    
          cy.get('pre')
          .invoke('text')
          .should('eq', JSON.stringify(data))
        })
      })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    数学建模学习视频及资料集(2022.08.10)
    Springboot美食商城1n3t9计算机毕业设计-课程设计-期末作业-毕设程序代做
    使用groupby统计不同组的缺失值
    【精讲】vue框架 vue脚手架多种方法安装(教程)及注意事项、系统安装时操作代码
    Perl连接mysql数据库
    Golang实现逻辑编排解释引擎
    springboot使用的设计模式
    解密Prompt系列72. 多模态大模型进化史:从"翻译官"到"原生双语大脑"
    <计算机网络自顶向下> TCP拥塞
    RocketMQ是如何发送和消费消息的
  • 原文地址:https://blog.csdn.net/i042416/article/details/126507608