如何在Cypress的自定义命令中记录前一个主题

huangapple go评论48阅读模式
英文:

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(&#39;shouldBeVisible&#39;,
    {
        prevSubject: &#39;element&#39;,
    },
    (subject) =&gt; {
        var getText = cy.wrap(subject);
        // cy.wrap(subject).invoke(&#39;val&#39;).then((element) =&gt;{
        //     cy.log(&#39;element:: &#39; + cy.wrap(subject).get(subject))
        // });

        const log = Cypress.log({
            name: &quot;shouldBeVisible&quot;,
            displayName: &quot;SHOULD BE VISIBLE&quot;,
            message: [
              `&#128065;️ (element: `+subject+` should be visible)`,
            ],
            // @ts-ignore
            autoEnd: false,
            consoleProps() {
              return subject
            },
          })

        cy.wrap(subject).should(&#39;be.visible&#39;);
    }
)

when I call it like this:

  cy.visit(&#39;https://docs.cypress.io/guides/overview/why-cypress&#39;);
  cy.get(&#39;.headerWrapper_tu51&#39;).shouldBeVisible();

I want it to log the calling function or calling element:

如何在Cypress的自定义命令中记录前一个主题

答案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(&#39;https://docs.cypress.io/examples/applications&#39;);

cy.get(&#39;h2&#39;).eq(0)
  .then(subject =&gt; {
    expect(Cypress.utils.stringifyActual(subject))
      .to.eq(&#39;&lt;h2#Kitchen-Sink.anchor.anchorWithStickyNavbar_LWe7&gt;&#39;)
  })

so in your log call

const log = Cypress.log({
  name: &quot;shouldBeVisible&quot;,
  displayName: &quot;SHOULD BE VISIBLE&quot;,
  message: `&#128065;️ (element: ${Cypress.utils.stringifyActual(subject)} should be visible)`,
  ...

huangapple
  • 本文由 发表于 2023年6月29日 22:19:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定