首页 > 解决方案 > cypress - TypeError 无法设置未定义的属性“状态”

问题描述

每次我TypeError cannot set property 'status' of undefined 在不同的地方出错时,我都会尝试在我的 UI 测试中使用 cypress

describe('cats app', () => {
    beforeEach(() => {
      // Cypress starts out with a blank slate for each test
      // so we must tell it to visit our website with the `cy.visit()` command.
      // Since we want to visit the same URL at the start of all our tests,
      // we include it in our beforeEach function so that it runs before each test
      cy.visit(Cypress.env('host'))
    })
  
    it('login to cats', () => {
    // clear local storage 
    cy.clearLocalStorage()

    cy.get('#email').focus().type(Cypress.env('email'))
    .should('have.value', Cypress.env('email'))
    cy.screenshot("login-email")
    cy.get('#password').focus().type(Cypress.env('password'))
    .should('have.value', Cypress.env('password'))
    cy.screenshot("login-password")

    cy.get("#login").click({force: true})

     // to ensure login is working 
    cy.get("#TicketsNav").should('be.visible')
    cy.screenshot("TicketsNav")

      
    })
  

  })

我试图通过添加和使用这些命令来处理这个问题,因为它大部分时间都在打字时发生

Cypress.Commands.add(
    'paste',
    {
      prevSubject: true,
      element: true,
    },
    ($element, text) => {
      const subString = text.substr(0, text.length - 1);
      const lastChar = text.slice(-1);
  
      cy.get($element)
        .click()
        .then(() => {
          $element.text(subString);
          $element.val(subString);
          cy.get($element).type(lastChar);
        });
    }
  );

  Cypress.Commands.overwrite('type', (originalFn, subject, text, options = {}) => {
    options.delay = 20
  
    return originalFn(subject, text, options)
  })

另外,我试图强制使用 chrome 浏览器

cypress run --browser chrome

但如果不同的地方,我仍然会收到该错误

在此处输入图像描述

标签: reactjscypresscircleci

解决方案


推荐阅读