英文:
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.
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();
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论