【问题标题】:Cypress component testing is not loading CSS while running testcases赛普拉斯组件测试在运行测试用例时未加载 CSS
【发布时间】:2022-01-03 10:36:41
【问题描述】:

我们正在使用模板构建 Web 组件。我们编译模板组件并创建相应的“React 组件”并将它们导入到我们的项目中。

这样做时,我们可以在启动 react 应用程序时按预期查看组件。然而,当我们使用 cypress 安装组件并执行测试用例时,我们观察到这些预构建组件的 CSS 没有被加载。

cypress.json

    {
        "baseUrl": "http://localhost:3000",
        "projectId": "263jf8",
        "component": {
            "componentFolder": "src",
            "testFiles": "**/*spec.{js,jsx,ts,tsx}",
            "viewportHight": 1200,
            "viewportWidth": 1000
        },
        "retries": {
            "runMode": 2,
            "openMode": 0
        }
    } 

示例规范文件

import Header from './header'; 
describe('header', () => {
    beforeEach(() => {
        mount(
            <Header></Header>
        )})
        it('renders as an inline button', () => {
             cy.get('button')
             .should('have.class', 'nexus-btn').and('be.visible')
             cy.get('.nexus-hamburger-icon').should('have.text','Close Menu').and('be.visible')
             cy.get('.nexus-menu > .nexus-btn').should('have.text','Close Menu')
             cy.get('a > .nexus-visually-hidden').contains('Home')
             cy.contains('Home').should('exist')
             cy.get('a > .nexus-icon > svg').should('be.visible')
            })
        })

plugin/cypress.index.js

module.exports = (on, config) => {
  require('cypress-grep/src/plugin')(config)

  if (config.testingType === 'component') {
    require('@cypress/react/plugins/react-scripts')(on, config)
  }
  return config

}

预期

实际

【问题讨论】:

标签: testing automation components cypress


【解决方案1】:

您可以尝试在 index.ts 或 index.js 文件中导入 css,该文件将在 location -> cypress/support/index.ts 中可用

【讨论】:

  • 谢谢@Sivanandhini。这对我有用
猜你喜欢
  • 2023-02-14
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 2023-02-10
  • 2023-01-10
  • 2022-08-14
  • 1970-01-01
相关资源
最近更新 更多