You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Cypress链式命令封装为自定义命令selected_value()

封装Cypress自定义命令获取Ember Power Select选中值

实现步骤

  • 在cypress/support/commands.js文件中添加如下自定义命令:
Cypress.Commands.add('selected_value', { prevSubject: true }, (subject) => {
  return cy.wrap(subject)
    .find('span.ember-power-select-selected-item')
    .invoke('text')
    .then(text => text.trim())
})

关键说明

  • { prevSubject: true }是核心配置,它指定这个命令支持链式调用,会接收前一个命令(比如cy.get(selector))返回的DOM元素作为参数subject
  • cy.wrap(subject)将传入的DOM元素包装为Cypress链式对象,确保后续的find、invoke等命令能正常链式执行
  • 后续逻辑和你原本的长链式命令完全一致,最终返回修剪后的选中文本,直接对接.should('eq', expected_value)断言

使用方式

cy.get(selector).selected_value().should('eq', expected_value)

内容的提问来源于stack exchange,提问作者edhel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 06:30:53