Cypress初体验

发布时间 2023-04-16 22:24:45作者: 俊king

Cypress初体验

一个最简单的CypressTestCase

import cypress = require("cypress");

describe('TestLogin', () => {
    beforeEach('After All', () => {
        cy.visit('https://xxx/login');
    });

    it('Login by username and password', () => {
        let username = 'xxx';
        let password = 'xxx';

        cy.get('ul>li:nth-child(2)')
        .click();
        cy.get('input[id=name]')
        .type(username);
        cy.get('input[id=password]')
        .type(password);
        cy.get('input[id=agree]')
        .click();
        cy.get('button[type=submit]')
        .as('submitBtn');
        cy.get('@submitBtn')
        .click();
        
        // check
        cy.url().should('include', '/profile');
    });
})

Cypress调试测试用例

Cypress会记录测试运行时发生的特殊页面事件,包括:

  • 网络XHR请求
  • URL哈希更改
  • 页面加载
  • 表格提交

暂停和Debug操作:

  1. cy.pause()方法

  2. cy.debug()方法

import cypress = require("cypress");

describe('TestLogin', () => {
    beforeEach('After All', () => {
        cy.visit('https://console-pre.raylink.live/login');
    });

    it('Login by username and password', () => {
        let username = '18878912237';
        let password = 'Jw123456';

        cy.get('ul>li:nth-child(2)')
        .click();
        cy.get('input[id=name]')
        .type(username);
        cy.get('input[id=password]')
        .type(password);
        cy.get('input[id=agree]')
        .click();
        // cy.pause(); // 设置以后要到浏览器手动的执行下一步操作
        cy.get('button[type=submit]')
        .as('submitBtn');
        cy.get('@submitBtn')
        .debug() // 可以继续运行和跳到下一个函数,在浏览器上方有具体的图标
        .click();
        
        // check
        cy.url().should('include', '/profile');
    });
})