- Cypress 教程
- Cypress - 主页
- Cypress - 介绍
- Cypress - 架构和环境设置
- Cypress - 测试运行器
- Cypress - 构建第一个测试
- Cypress - 支持的浏览器
- Cypress - 基本命令
- Cypress - 变量
- Cypress - 别名
- Cypress - 定位器
- Cypress - 断言
- Cypress - 文本验证
- Cypress - 异步行为
- Cypress - 使用 XHR
- Cypress - jQuery
- Cypress - 复选框
- Cypress - 选项卡
- Cypress - 下拉菜单
- Cypress - 提醒
- Cypress - 子窗口
- Cypress - 隐藏元素
- Cypress - 框架
- Cypress - 网页表格
- Cypress - 鼠标操作
- Cypress - Cookie
- Cypress - 获取和写入
- Cypress - 文件上传
- Cypress - 数据驱动测试
- Cypress - 提示弹出窗口
- Cypress - 仪表盘
- Cypress - 屏幕截图和视频
- Cypress - 调试
- Cypress - 自定义命令
- Cypress - 固定装置
- Cypress - 环境变量
- Cypress - 挂钩
- Cypress - JSON 文件配置
- Cypress - 报告
- Cypress - 插件
- Cypress - GitHub
- Cypress 实用资源
- Cypress - 快速指南
- Cypress - 实用资源
- Cypress - 讨论
Cypress - 文本验证
可以使用 text 方法获取网路元素的文本。还可以添加断言来验证文本内容。
使用 text() 实现
以下是关于验证的 text() 命令实现 −
// test suite
describe('Tutorialspoint', function () {
// it function to identify test
it('Scenario 1', function (){
// test step to launch a URL
cy.visit("https://#")
// identify element
cy.get('h1#headingText').find('span').then(function(e){
//method text to obtain text content
const t = e.text()
expect(t).to.contains('Sign')
})
})
})
执行结果
输出如下 −
输出日志显示使用 text 方法获取的文本为登录。
使用文本断言实现
我们还可以使用以下命令实现网页元素文本断言 −
// test suite
describe('Tutorialspoint', function () {
// it function to identify test
it('Scenario 1', function (){
// test step to launch a URL
cy.visit("https://#")
// verify text with have.text
cy.get('h1#headingText').find('span').should('have.text','Sign in')
})
})
执行结果
输出如下 −
输出日志显示使用 should 断言完成文本验证。
广告