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

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

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

  1. Cypress.Commands.add('checkHeader',() => {
  2. //* FAVICON
  3. it('Check Favicon 1', () => {
  4. cy.get('head link[type="image/png"]')
  5. .should('have.attr', 'href')
  6. .and('eq', '/favicon-32x32.png?v=26696447bf4055aecabfb5be2539b037');
  7. });
  8. });

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

  1. describe('Header', () => {
  2. //* VISIT THE WEBPAGE
  3. it('Visit the website', () => {
  4. cy.visit(Cypress.env('baseUrl'));
  5. });
  6. it('Check Header', () => {
  7. cy.checkHeader();
  8. });
  9. });

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

  1. Cypress.Commands.add('checkHeader',() => {
  2. //* FAVICON
  3. it('Check Favicon 1', () => {
  4. cy.get('head link[type="image/png"]')
  5. .should('have.attr', 'href')
  6. .and('eq', '/favicon-32x32.png?v=26696447bf4055aecabfb5be2539b037');
  7. });
  8. });

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

  1. describe('Header', () => {
  2. //* VISIT THE WEBPAGE
  3. it('Visit the website', () => {
  4. cy.visit(Cypress.env('baseUrl'));
  5. });
  6. it('Check Header', () => {
  7. cy.checkHeader();
  8. });
  9. });

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

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

How can i fix this?

答案1

得分: 5

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

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

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

  1. Cypress.Commands.add('checkHeader', () => {
  2. cy.log('Check Favicon 1')
  3. cy.get('head link...
  4. });
  5. it('Check Header', () => {
  6. cy.checkHeader();
  7. })
英文:

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.

  1. it('Check Header', () => {
  2. //cy.checkHeader();
  3. it('Check Favicon 1', () => {
  4. cy.get(...
  5. })

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

  1. Cypress.Commands.add('checkHeader',() => {
  2. cy.log('Check Favicon 1')
  3. cy.get('head link...
  4. });
  5. it('Check Header', () => {
  6. cy.checkHeader();
  7. })

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:

确定