storyBook play学习

场景

在官方给出的案例中,
Page.stories.js

import {
    within, userEvent } from '@storybook/testing-library'
import MyPage from './Page.vue'

export default {
   
  title: 'Example/Page',
  component: MyPage,
  parameters: {
   
    // More on how to position stories at: https://storybook.js.org/docs/vue/configure/story-layout
    layout: 'fullscreen'
  }
}

export const LoggedOut = {
   }

// More on interaction testing: https://storybook.js.org/docs/vue/writing-tests/interaction-testing
export const LoggedIn = {
   
  render: () => ({
   
    components: {
   
      MyPage
    },
    template: '<my-page />'
  }),
  play: async ({
     canvasElement }) => {
   
    const canvas = within(canvasElement)
    const loginButton = await canvas.getByRole('button', {
   
      name: /Log in/i
    })
    await userEvent.click(loginButton)
  }
}

这段代码是一个测试脚本,用于模拟用户登录行为并点击登录按钮。

  • const canvas = within(canvasElement)within 是一个测试工具函数,它可以让你在给定的 canvasElement 内部进行 DOM 查询操作。在这段代码中,它将传递给它的 canvasElement 封装成一个可用于操作的对象 canvas

  • const loginButton = await canvas.getByRole('button', { name: /Log in/i })getByRole 是一个用于根据元素角色获取元素的函数。在这里,它将查找一个角色为 'button' 且名称匹配正则表达式 /Log in/i 的按钮元素,并将结果赋值给变量 loginButton/Log in/i 表示不区分大小写匹配字符串 'Log in'

  • await userEvent.click(loginButton)userEvent.click 是一个模拟用户点击事件的函数。在这里,它会模拟用户点击 loginButton 元素,执行点击操作。

这段代码的作用是在一个渲染的页面中找到名称为 “Log in” 的按钮,并模拟用户点击该按钮。这样就可以测试用户登录的交互行为。

相关推荐

  1. 学习 学习

    2023-12-28 22:04:02       66 阅读
  2. 学期学习计划

    2023-12-28 22:04:02       45 阅读
  3. 学习笔记:机器学习

    2023-12-28 22:04:02       79 阅读
  4. C++学习-List学习

    2023-12-28 22:04:02       51 阅读
  5. opencv学习 机器学习

    2023-12-28 22:04:02       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 22:04:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 22:04:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 22:04:02       82 阅读
  4. Python语言-面向对象

    2023-12-28 22:04:02       91 阅读

热门阅读

  1. LeetCode——动态规划

    2023-12-28 22:04:02       42 阅读
  2. 前端---css 元素溢出

    2023-12-28 22:04:02       57 阅读
  3. 2024 Android保活总结

    2023-12-28 22:04:02       57 阅读
  4. 鸿蒙开发ArkTS基础学习-开发准备工具配置

    2023-12-28 22:04:02       51 阅读
  5. C++ 之map

    2023-12-28 22:04:02       57 阅读
  6. 编程艺术C代码学习注释

    2023-12-28 22:04:02       61 阅读
  7. 如何培养孩子的编程思维,学习Scratch少儿编程

    2023-12-28 22:04:02       49 阅读
  8. 二次分配问题(遗传算法求解)

    2023-12-28 22:04:02       62 阅读