【问题标题】:How to target an attribute of a self defined component in cypress?如何在柏树中定位自定义组件的属性?
【发布时间】:2019-05-08 16:14:22
【问题描述】:

我对 cypress 非常陌生,我想测试如果我点击我的标签,那么视频应该会改变,但我不知道如何在我的 app-video 组件中定位视频属性:

it('button next should show next video', function() {
  cy.visit('/carousel/videos');
  cy.get('[data-cy=videoComponent]') //i want to see the current value
  cy.get('[data-cy=buttonNext]').click();
  // I want to see the next value
});

包含视频对象的属性是@Output() public currentVid: Video;

我可以定位这个并询问它的价值以检查它是否改变了吗?

<div class="body">
  <h3>B-roll & Timelapse</h3>
  <div class="container">
    <div *ngIf="videos$ | async as videos; else loadingOrError">
      <div class="vidDiv">
        <app-video [video]="currentVid" data-cy="videoComponent"></app-video>
        <div class="videoControls">
          <a
            mat-raised-button
            (click)="previous()"
            class="controls"
            data-cy="buttonPrevious"
          >
            <i class="material-icons">keyboard_arrow_left</i>
          </a>
          <a
            mat-raised-button
            (click)="next()"
            class="controls"
            data-cy="buttonNext"
          >
            <i class="material-icons">keyboard_arrow_right</i>
          </a>
        </div>
      </div>
    </div>
  </div>
</div>
<ng-template #loadingOrError>
  <mat-card>
    <mat-error
      *ngIf="loadingErrors$ | async as errorMessage; else loading"
      data-cy="appError"
    >
      Error loading the recipe list: {{ errorMessage }}. <br />
      Please try again later.
    </mat-error>
    <ng-template #loading>
      <mat-spinner class="spinner"></mat-spinner>
    </ng-template>
  </mat-card>
</ng-template>

【问题讨论】:

  • 您可以添加页面该部分的 HTML 吗?您需要使用它而不是原始(程序员)javascript 代码。

标签: angular cypress


【解决方案1】:

你应该可以使用这个语法:

it('button next should show next video', function() {
  cy.visit('/carousel/videos');
  cy.get('[data-cy=videoComponent]')
    .should('have.attr', '[video]', 'currentVid')
  cy.get('[data-cy=buttonNext]').click();
  cy.get('[data-cy=videoComponent]')
    .should('have.attr', '[video]', 'nextVid')
});

这是使用 Chai 的链接器“attr(name, [value])”,此处解释:https://docs.cypress.io/guides/references/assertions.html#Chai-jQuery

【讨论】:

  • 我一直在检查您的代码,这绝对是朝着正确方向迈出的一步,但是对象没有被重命名,对象的内容发生了变化(新的 id 和新的 url),我'正在尝试检查 currentVid 中对象的 id 是否更改
  • 检查一下,我对 HTML 中的那些对象完全没有经验。但我认为使用its() 将是最后的推动力。这里描述了它的工作原理:docs.cypress.io/api/commands/its.html#Syntax
猜你喜欢
  • 1970-01-01
  • 2022-08-05
  • 2017-11-07
  • 1970-01-01
  • 2011-02-05
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多