简体   繁体   English

使用带有 cypress 的 styled-components 或自定义组件

[英]using styled-components or custom component with cypress

Cypress selector is easy, just do cy.get('.myComp') and <input className="myComp" /> will be selected but with styled-component赛普拉斯选择器很简单,只需执行cy.get('.myComp')<input className="myComp" />将被选中,但使用 styled-component

maybe we need to use custom attribute like cy-data, cy-testid etc. I guess there's no other shortcut than flood our component with those custom attribute right?也许我们需要使用自定义属性,如 cy-data、cy-testid 等。我想除了用这些自定义属性淹没我们的组件之外,没有其他捷径吧?

The other hassle using cypress is when you use css module, where the class is generated differently every build, and imagine your component is, then you need to pass your the custom attribute all the way down使用 cypress 的另一个麻烦是当您使用 css 模块时,每个构建都会生成不同的 class ,并且想象您的组件是,那么您需要一直传递您的自定义属性

<Custom cy-data="btn1" />

const Custom = ({cy-data}) => <button cy-data={cy-data} />

any workaround to avoid this pain?有什么办法可以避免这种痛苦吗?

We use data-test-target attribute and write in manually in JSX.我们使用data-test-target属性并在 JSX 中手动写入。 In simple version it's all that you need.在简单版本中,这就是您所需要的。 But if you have complex cases like two forms on the same page with the same fields you need to distinct them.但是,如果您在同一页面上有两个 forms 之类的复杂案例,并且具有相同的字段,则需要区分它们。 So that we do this:所以我们这样做:

Target can be built by 3 parameters:目标可以通过 3 个参数构建:

  • block: required块:需要
  • element: optional元素:可选
  • context: optional上下文:可选

Imagine you have a React component and want to set test targets.想象一下,你有一个 React 组件并且想要设置测试目标。 For example you have 2 buttons in the component: remove and edit, so it would look like例如,您在组件中有 2 个按钮:删除和编辑,所以它看起来像

<button data-test-target="component-name:remove">Remove</button>
<button data-test-target="component-name">Edit</button>

If you have more then one this component on the page you should pass a context in props:如果你在页面上有多个这个组件,你应该在 props 中传递一个上下文:

<button data-test-target="component-name:remove::todo-list">Remove</button>

Helper that I use to follow this idea:我用来遵循这个想法的助手:

import dashify from 'dashify';

export const createTestAttribute = ({
    block: blockPart,
    element,
    context,
}) => {
    const elementPart = element ? `:${dashify(element)}` : '';
    const contextPart = context ? `::${dashify(context)}` : '';

    return `${blockPart}${elementPart}${contextPart}`;
};

Usage:用法:

<button
  data-test-target={createTestAttribute({
    block: 'component-name',
    element: 'remove',
    context: props.testContext,
  })}
>
  Remove
</button>

Using it tests will be stable and they won't depend on your markup structure and class names使用它测试将是稳定的,它们不会依赖于您的标记结构和 class 名称

Well you can create function that will return test attribute only for some environments.好吧,您可以创建 function,它只会在某些环境下返回测试属性。 For development and for staging for example.例如,用于开发和分期。

const dataTestTarget = (v) => {return !process.env.PRODUCTION ? { 'data-test-target': v } : ''}

Something like this could work in general.像这样的东西通常可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM