英文:
How to log the previous subject in custom commands in Cypress
问题
Sure, here's the translated code:
我想要在Cypress中为我的自定义命令添加日志,例如
<!-- 语言: js -->
    Cypress.Commands.add('shouldBeVisible',
        {
            prevSubject: 'element',
        },
        (subject) => {
            var getText = cy.wrap(subject);
            // cy.wrap(subject).invoke('val').then((element) =>{
            //     cy.log('element:: ' + cy.wrap(subject).get(subject))
            // });
            const log = Cypress.log({
                name: "shouldBeVisible",
                displayName: "SHOULD BE VISIBLE",
                message: [
                  `👁️ (元素: `+subject+` 应该可见)`,
                ],
                // @ts-ignore
                autoEnd: false,
                consoleProps() {
                  return subject
                },
              })
            cy.wrap(subject).should('be.visible');
        }
    )
当我像这样调用它时:
```js
  cy.visit('https://docs.cypress.io/guides/overview/why-cypress');
  cy.get('.headerWrapper_tu51').shouldBeVisible();
我希望它记录调用函数或调用元素:
![输入图像说明][1]
英文:
I want to add logs to my custom commands in Cypress,
for example
<!-- language: js -->
Cypress.Commands.add('shouldBeVisible',
    {
        prevSubject: 'element',
    },
    (subject) => {
        var getText = cy.wrap(subject);
        // cy.wrap(subject).invoke('val').then((element) =>{
        //     cy.log('element:: ' + cy.wrap(subject).get(subject))
        // });
        const log = Cypress.log({
            name: "shouldBeVisible",
            displayName: "SHOULD BE VISIBLE",
            message: [
              `👁️ (element: `+subject+` should be visible)`,
            ],
            // @ts-ignore
            autoEnd: false,
            consoleProps() {
              return subject
            },
          })
        cy.wrap(subject).should('be.visible');
    }
)
when I call it like this:
  cy.visit('https://docs.cypress.io/guides/overview/why-cypress');
  cy.get('.headerWrapper_tu51').shouldBeVisible();
I want it to log the calling function or calling element:

答案1
得分: 4
"message" 属性需要一个纯字符串,但 subject 是一个 jQuery 对象,无法优雅地进行字符串化。
Cypress 在内部使用 Cypress.utils.stringifyActual(),你也可以在自定义命令中使用它,将 jQuery 对象转换为适合记录的字符串。
这个测试演示了用法:
cy.visit('https://docs.cypress.io/examples/applications');
cy.get('h2').eq(0)
  .then(subject => {
    expect(Cypress.utils.stringifyActual(subject))
      .to.eq('<h2#Kitchen-Sink.anchor.anchorWithStickyNavbar_LWe7>')
  })
所以在你的日志调用中:
const log = Cypress.log({
  name: "shouldBeVisible",
  displayName: "SHOULD BE VISIBLE",
  message: `👁️ (element: ${Cypress.utils.stringifyActual(subject)} should be visible)`,
  ...
英文:
It's expecting a pure string for the message property, but subject is a jQuery object and it does not stringify elegantly.
Cypress internally uses Cypress.utils.stringifyActual() which you can also use in custom commands, to convert the jQuery object into a log-friendly string.
This test illustrates the usage,
cy.visit('https://docs.cypress.io/examples/applications');
cy.get('h2').eq(0)
  .then(subject => {
    expect(Cypress.utils.stringifyActual(subject))
      .to.eq('<h2#Kitchen-Sink.anchor.anchorWithStickyNavbar_LWe7>')
  })
so in your log call
const log = Cypress.log({
  name: "shouldBeVisible",
  displayName: "SHOULD BE VISIBLE",
  message: `👁️ (element: ${Cypress.utils.stringifyActual(subject)} should be visible)`,
  ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论