Cypress - 基本命令
Cypress - 常用命令概览
Section titled “Cypress - 常用命令概览”Cypress 提供了一系列丰富的命令来与你的 Web 应用进行交互。以下是一些最常用的命令:
.and()
Section titled “.and()”将断言链接到上一个断言。它是 .should() 命令的别名,在对同一主题进行多个断言时可以提高可读性。
用法:
// 元素应该是可见的 AND 可用cy.get('#submit-button').should('be.visible').and('be.enabled');
// 具有特定值的复选框应该被选中cy.get('input[type="checkbox"][value="newsletter"]').should('be.checked');为一个主题(例如,DOM 元素、网络请求、测试数据)分配一个别名,以便在测试中后续引用。使用 cy.get('@aliasName') 来访问别名。
用法:
// 将第一个列表项起别名为 'firstItem'cy.get('ul#my-list > li').first().as('firstItem');// 在测试的后面...cy.get('@firstItem').should('have.class', 'active');.blur()
Section titled “.blur()”从当前具有焦点的 DOM 元素上移除焦点。
用法:
// 在输入框中输入文本,然后使其失去焦点cy.get('#username').type('jane.doe').blur();.check()
Section titled “.check()”选中单选按钮或复选框。适用于 <input type="radio"> 或 <input type="checkbox"> 元素。
用法:
// 选中类为 'terms-checkbox' 的复选框cy.get('.terms-checkbox').check();
// 选中具有特定值的单选按钮cy.get('input[type="radio"][value="female"]').check();.children()
Section titled “.children()”获取一个或一组 DOM 元素的子元素。可以选择提供一个选择器来过滤子元素。
用法:
// 获取 <ul> 的所有直接子元素 <li>cy.get('ul#main-menu').children('li');.clear()
Section titled “.clear()”清除输入字段或文本区域的值。
用法:
// 在输入框中输入,然后清除它cy.get('#search-box').type('initial text').clear().should('have.value', '');cy.clearCookie()
Section titled “cy.clearCookie()”按名称清除特定的浏览器 Cookie。
用法:
// 清除 'session_id' Cookiecy.clearCookie('session_id');cy.clearCookies()
Section titled “cy.clearCookies()”清除当前域名和子域名的所有浏览器 Cookie。
用法:
// 在测试前清除所有 Cookiecy.clearCookies();cy.clearLocalStorage()
Section titled “cy.clearLocalStorage()”清除当前源(origin)的所有 localStorage 数据。如果提供了键名,也可以清除指定的键。
用法:
// 清除所有 local storage 数据cy.clearLocalStorage();
// 清除 local storage 中的特定项cy.clearLocalStorage('userPreferences');.click()
Section titled “.click()”点击一个 DOM 元素。Cypress 会确保元素是可操作的(actionable)之后再执行点击。
用法:
// 点击 ID 为 'login-btn' 的按钮cy.get('#login-btn').click();cy.contains()
Section titled “cy.contains()”获取包含指定文本的 DOM 元素。如果先提供了选择器,它会在这些元素中搜索。
用法:
// 获取包含文本 'Welcome' 的元素cy.contains('Welcome');
// 在 ID 为 'user-form' 的表单中获取包含文本 'Submit' 的按钮cy.get('form#user-form').contains('button', 'Submit');.dblclick()
Section titled “.dblclick()”双击一个 DOM 元素。
用法:
// 双击 ID 为 'edit-icon' 的元素cy.get('#edit-icon').dblclick();.debug()
Section titled “.debug()”暂停测试并打开浏览器开发者工具,同时记录当前的主题(subject)。对于调试非常有用。
用法:
// 获取元素后暂停并调试cy.get('#user-profile').debug();cy.document()
Section titled “cy.document()”获取当前活动页面的 window.document 对象。
用法:
cy.document().should('have.property', 'charset').and('eq', 'UTF-8');.each()
Section titled “.each()”遍历一个类数组的主题(例如,DOM 元素集合)。
用法:
// 遍历每个列表项并检查其文本cy.get('ul#todo-list > li').each(($li, index, $list) => { cy.log(`Item ${index}: ${$li.text()}`);});.end()
Section titled “.end()”结束当前的命令链。这很少需要,因为 Cypress 命令通常会自然地衔接。如果你暂时更改了主题(例如,在 .within() 块中,尽管 .within() 会自动处理),可以使用它来恢复到之前的主题。
用法:
// 示例:(虽然通常不需要)cy.get('form').within(() => { cy.get('input[name="username"]').type('test');}).end() // 主题恢复为 'form'.should('have.class', 'submitted');获取元素集合中特定索引处的 DOM 元素。
用法:
// 获取表格行中的第三个 <td> 元素cy.get('table#data-table tr:first-child td').eq(2).should('contain', 'Status');cy.exec()
Section titled “cy.exec()”从项目根目录运行一个系统命令。对于诸如初始化数据库或运行脚本等任务很有用。
用法:
// 示例:运行脚本重置数据库状态cy.exec('npm run db:reset');.find()
Section titled “.find()”获取当前主题的后代 DOM 元素,并按选择器进行过滤。
用法:
// 在特定的表格行中查找所有 <td> 元素cy.get('tr#user-row-123').find('td');.first()
Section titled “.first()”获取元素集合中的第一个 DOM 元素。
用法:
// 获取每行表格中的第一个 <td>cy.get('table#data-table tr td').first();cy.get()
Section titled “cy.get()”通过选择器或别名获取一个或多个 DOM 元素。这是最基础的命令之一。
用法:
// 按 ID 获取元素cy.get('#main-content');// 获取所有类为 'item' 的元素cy.get('.item');// 获取别名元素cy.get('@myAlias');cy.getCookie()
Section titled “cy.getCookie()”按名称获取特定的浏览器 Cookie。
用法:
cy.getCookie('user_session').should('have.property', 'value', 'abcdef12345');cy.getCookies()
Section titled “cy.getCookies()”获取所有浏览器 Cookie。
用法:
cy.getCookies().should('not.be.empty');cy.go()
Section titled “cy.go()”在浏览器的历史记录中向前或向后导航。
用法:
// 返回cy.go('back');// 前进cy.go('forward');// 后退 2 页cy.go(-2);cy.visit()
Section titled “cy.visit()”访问一个远程 URL 或本地 HTML 文件。
用法:
cy.visit('https://www.example.com');cy.visit('/about-us'); // 相对于 cypress.config.js 中的 baseUrlcy.visit('app/index.html'); // 本地文件.next()
Section titled “.next()”获取当前主题的紧随其后的同级 DOM 元素。
用法:
// 获取下一个列表项同级元素cy.get('li.active-item').next().should('have.class', 'next-item');.parent()
Section titled “.parent()”获取当前主题的父级 DOM 元素。
用法:
// 获取类为 'child-element' 的元素的父级 <div>cy.get('.child-element').parent('div.parent-container');.should()
Section titled “.should()”创建一个断言。Cypress 会重试此断言直到它通过或超时。
用法:
// 断言元素可见cy.get('#error-message').should('be.visible');// 断言输入框具有特定值cy.get('#name-input').should('have.value', 'John Doe');cy.wait()
Section titled “cy.wait()”暂停测试指定毫秒数,或等待别名资源(如网络请求)完成。通常不建议为了任意暂停而使用 cy.wait(milliseconds);更推荐使用会重试的断言或等待别名路由(aliased routes)。
用法:
// 等待 1 秒(谨慎使用)cy.wait(1000);
// 等待一个别名网络请求cy.intercept('GET', '/api/users').as('getUsers');cy.visit('/users');cy.wait('@getUsers').its('response.statusCode').should('eq', 200);cy.title()
Section titled “cy.title()”获取当前活动页面的 document.title。
用法:
cy.title().should('eq', 'My Application - Dashboard');cy.viewport()
Section titled “cy.viewport()”控制测试期间的屏幕尺寸和方向。可以接受宽/高的数值或预设名称(例如,‘iphone-6’)。
用法:
// 设置视口宽度为 1280px,高度为 720pxcy.viewport(1280, 720);
// 使用预设的视口大小cy.viewport('macbook-15');cy.log()
Section titled “cy.log()”在 Cypress 命令日志(Command Log)中打印消息。对于调试或在测试执行期间提供上下文信息很有用。
用法:
cy.log('Starting login sequence...');cy.get('#username').type('user');cy.log('Username entered.');cy.reload()
Section titled “cy.reload()”重新加载当前页面,类似于浏览器的刷新按钮。
用法:
cy.reload();// 不使用缓存重新加载cy.reload(true);