Cypress在自定义命令上无法正常工作。

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

Cypress is not working on custom commands

问题

I create a new file under support/header.js to reuse for checking headers i also import it to e2e.js

header.js

Cypress.Commands.add('checkHeader',() => {

    //* FAVICON
    it('Check Favicon 1', () => {
        cy.get('head link[type="image/png"]') 
          .should('have.attr', 'href') 
          .and('eq', '/favicon-32x32.png?v=26696447bf4055aecabfb5be2539b037');
    });
});

and i'm importing it to a test e2e/test-header.cy.js

describe('Header', () => {

    //* VISIT THE WEBPAGE
    it('Visit the website', () => {
        cy.visit(Cypress.env('baseUrl'));
        
    });

    it('Check Header', () => {
        cy.checkHeader();
    });
});

When i'm running the test it's doing nothing.

英文:

I create a new file under support/header.js to reuse for checking headers i also import it to e2e.js

header.js

Cypress.Commands.add('checkHeader',() => {

    //* FAVICON
    it('Check Favicon 1', () => {
        cy.get('head link[type="image/png"]') 
          .should('have.attr', 'href') 
          .and('eq', '/favicon-32x32.png?v=26696447bf4055aecabfb5be2539b037');
    });
});

and i'm importing it to a test e2e/test-header.cy.js

describe('Header', () => {

    //* VISIT THE WEBPAGE
    it('Visit the website', () => {
        cy.visit(Cypress.env('baseUrl'));
        
    });

    it('Check Header', () => {
        cy.checkHeader();
    });
});

When i'm running the test it's doing nothing.

Cypress在自定义命令上无法正常工作。

How can i fix this?

答案1

得分: 5

你不能将it()放在一个命令内部。命令是构建it()的基本组成部分,而不是反过来。

使用这种结构,你试图构建嵌套的it(),这也是不可能的。

只需删除内部的it(),它就会完美工作。

Cypress.Commands.add('checkHeader', () => {
   cy.log('Check Favicon 1')
   cy.get('head link...
});

it('Check Header', () => {
  cy.checkHeader();
})
英文:

You can't put an it() inside a command. Commands are the building blocks of it() not the other way round.

With that structure, you are trying to build nested it() which is also not possible.

it('Check Header', () => {

   //cy.checkHeader();
   it('Check Favicon 1', () => {
     cy.get(...

})

Just remove that inner it() and it will work perfectly.

Cypress.Commands.add('checkHeader',() => {
   cy.log('Check Favicon 1')
   cy.get('head link...
});

it('Check Header', () => {
  cy.checkHeader();
})

huangapple
  • 本文由 发表于 2023年5月30日 09:15:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361062.html
匿名

发表评论

匿名网友

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

确定