javascript - 如何使用 Cypress 为 Vue Tailwind CSS 应用程序编写 2e2 测试?

标签 javascript vue.js testing cypress tailwind-css

当我查看 Cypress.io 文档时,有一些有关如何编写测试的示例,并且它们经常使用类选择器。我的问题是,我的 TailwindCSS 应用程序实际上并没有这些类型的类,而是有许多小类,这些类对于测试目标来说非常脆弱。为 Tailwind 应用程序编写 e2e 测试的好解决方案是什么?

文档中的示例:

it('adds todos', () => {
  cy.visit('https://todo.app.com')

  cy.get('.new-input').type('write code{enter}').type('write tests{enter}')

  cy.get('li.todo').should('have.length', 2)

  cy.get('.action-email').type('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="197f78727c597c74787075377a7674" rel="noreferrer noopener nofollow">[email protected]</a>').should('have.value', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c7a7d77795c79717d7570327f7371" rel="noreferrer noopener nofollow">[email protected]</a>')
})

但我的应用程序看起来一点也不像,我没有这样的类选择器:

<div class="relative flex min-h-screen flex-col justify-center overflow-hidden bg-gray-50 py-6 sm:py-12">
  <span class="absolute inset-0 bg-center"></span>

  <div class="relative bg-white px-6 pt-10 pb-8 shadow-xl ring-1 ring-gray-900/5 sm:mx-auto sm:max-w-lg sm:rounded-lg sm:px-10">
    <div class="mx-auto max-w-md">
      <img v-if="showLogo" src="logo.svg" class="h-6" />
      
      <div class="divide-y divide-gray-300/50">
        <div class="space-y-6 py-8 text-base leading-7 text-gray-600">
          My todo app
        </div>

        <button @click="createTodo" class="bg-white rouned-full px-2 py-4 border border-gray-200">
          Create a new todo
        </button>
      </div>
    </div>
  </div>
</div>

尝试针对像这样的许多类不是愚蠢且脆弱吗?有更好的选择吗?

it('adds todos', () => {
  cy.visit('https://todo.app.com')

  cy.get('.bg-white.rouned-full.px-2.py-4.border.border-gray-200').first().click()
  cy.get('.space-y-6.py-8.text-base.leading-7.text-gray-600').should('have.value', 'fake todo')
})

最佳答案

考虑使用data-cy attributes这是 Cypress 的推荐。它很实用,因为您确切地知道哪些元素被标记,但可能会耗费大量人力。

// Example - cypress.io

<div class="container">
  <h1 class="Hero-TagLine mt-0" data-cy="tag-line" style="font-size:5.6rem;line-height:7rem">
    <div>The web has evolved.<br>Finally, testing has too.</div>
  </h1>
  <h2 class="Hero-ByLine mb-0" data-cy="by-line">Fast, easy and reliable testing...

来自Testing Library的推荐是使用 Angular 色文本和 aria 属性。

还可以考虑使用traversal commands从关键元素进行导航。

关于javascript - 如何使用 Cypress 为 Vue Tailwind CSS 应用程序编写 2e2 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72283577/

相关文章:

vue.js - 如何将 Vue slot-scope 与嵌套组件一起使用

javascript - 传递到组件中的 Prop 不呈现

java - 从 jython 调用具有可变长度参数(varags)的 Java 方法

javascript - Next Js 结合外部 REST API 身份验证和授权

javascript - 如何计算每月最高还款额

JavaScript 对象和文字符号

testing - 如何从 testNG.xml 文件中的测试套件中排除测试文件夹

javascript - 即时更新 Google 路线

javascript - 如何在 Vue.js 中的两个组件之间共享方法?

c# - XAML View /控件的 GUI 测试框架