当手动测试我的Nuxt应用程序时,点击Switch元素会起作用,并且它会更改我的全局变量.但在Cypress测试中,即使Click事件成功运行,switch 仍保持关闭状态.

Test in cypress CLI

我的测试看起来像这样:

it.only('check if creeate button is visible', () => {
    const pagesToCheck = [
      '/encyclopedia/topics?page=1&language=cs',
      '/encyclopedia/questions?page=1&language=cs',
      '/encyclopedia/questions?kind=interactive_video&page=1&language=cs',
    ];


    pagesToCheck.forEach((pageUrl) => {
      cy.visit(pageUrl);

      cy.getData("edit-mode-switch").eq(1).click({ force: true });

      cy.getData("create-button").should('be.visible');
    });
  })

eq(1) is for targeting a particular switch. (There are 3)
getData() is custom command

交换机组件:

<div
        class="form-check form-switch form-check-custom form-check-warning form-check-solid"
      >
        <el-switch
          v-model="editModeStore.editMode"
          :before-change="changeSwitch"
          size="large"
          label="Edit mode switch"
          aria-label="Edit mode switch"
          style="--el-switch-on-color: #ffc700"
          data-cy="edit-mode-switch"

        />
        <span
          class="edit-mode-label"
          :class="{ 'is-active': editModeStore.editMode }"
          @click="changeSwitch"
        >
          {{ t('user.edit_mode') }}
        </span>
      </div>

I tried many ways of implementing the click event:
cy.getData("edit-mode-switch").eq(1).click('top', { force: true });

cy.getData("edit-mode-switch").eq(1).trigger('mousedown'); cy.getData("edit-mode-switch").eq(1).trigger('mouseup');

cy.get('selector-for-your-element').dblclick();

I also made Cypress click on the span next to the switch, but with no effect.
I appreciate any help.

推荐答案

Select 属性data-cy="edit-mode-switch"<el-switch>上,但是在sibling 元素<span @click="changeSwitch">上有一个点击处理程序.

也许:before-change="changeSwitch"也实现了点击,或者你依赖于事件起泡.

即便如此,还是值得直接针对跨度

cy.getData("edit-mode-switch").eq(1)
  .next('span')
  .click()

Javascript相关问答推荐

获取加载失败:获取[.]添加时try 将文档添加到Firerestore,Nuxt 3

在分区内迭代分区

如何判断属于多个元素的属性是否具有多个值之一

当在select中 Select 选项时,我必须禁用不匹配的 Select

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

使用JavaScript重新排序行

将自定义排序应用于角形数字数组

VUE 3捕获错误并呈现另一个组件

WP Bootstrap NavWaker:下拉菜单一次打开所有下拉菜单

MarkLogic-earch.suggest不返回任何值

如何在FastAPI中为通过file:/URL加载的本地HTML文件启用CORS?

如何限制显示在分页中的可见页面的数量

使用父标签中的Find函数查找元素

如何根据查询结果重新排列日期

使用Library chart.js在一个带有两个y轴的图表中绘制两个数据集

React数组查找不读取变量

如何处理不带参数的redux thunk payloadCreator回调函数?

如何在函数组件中保留对计时器的引用

如何在单击Search按钮时更新Reaction组件?

Js问题:有没有办法将数据从标记表转换成图表?