• 关于 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
  • 相关阅读:
    CalBioreagents ID蛋白抗体的功能和应用实例
    hdfsClient_java对hdfs进行上传、下载、删除、移动、打印文件信息尚硅谷大海哥
    【2-Docker安装部署ElasticSearch和Kibanan详细步骤】
    驱动学习之字符设备
    《TCP/IP网络编程》阅读笔记--Timewait状态和Nagle算法
    软件项目和安全项目案例(承接软件和安全项目合作)
    Java Stream API详解:高效处理集合数据的利器
    Worthington木瓜蛋白酶化学性质和特异性
    IO和进程(进程)
    Linux 系统性能瓶颈分析(超详细)
  • 原文地址:https://blog.csdn.net/i042416/article/details/126507608