51Testing软件测试论坛

标题: 如何在WEB自动化中对数据驱动? [打印本页]

作者: 海鸥一飞    时间: 2022-10-8 13:29
标题: 如何在WEB自动化中对数据驱动?
1 数据驱动
    数据驱动是测试框架中一个非常好的功能,使用数据驱动,可以在不增加代码量的情况下生成不同的测试策略。下面我们来看看在Cypress中的数据驱动使用方法。
1 数据在文件中
    在前面已经使用很多次,示例如下所示:
  1. <font size="3"><font size="3">[
  2.     {
  3.         "ID": "Data-1",
  4.         "name": "Surpass",
  5.         "age": 28
  6.     },
  7.     {
  8.         "ID": "Data-2",
  9.         "name": "Kevin",
  10.         "age": 29
  11.     }
  12. ]
  13. </font></font>
复制代码
示例代码如下所示:
  1. <font size="3"><font size="3">/// <reference types="cypress" />

  2. import Data from "./user.json"

  3. describe('数据在文件中', () => {
  4.    Data.forEach(item => {
  5.     it(item.ID, () => {
  6.         cy.log(`name is ${item.name},age is ${item.age}`);
  7.     });
  8.    });
  9. });
  10. </font></font>
复制代码
1.2 使用fixture
    Cypress中fixture默认位于cypress\fixtures\example.json文件中。操作步骤如下所示:

  1. <font size="3">[
  2.     {
  3.         "ID": "Data-1",
  4.         "name": "Surpass",
  5.         "age": 28
  6.     },
  7.     {
  8.         "ID": "Data-2",
  9.         "name": "Kevin",
  10.         "age": 29
  11.     }
  12. ]
  13. </font>
复制代码
  1. <font size="3">/// <reference types="cypress" />

  2. describe('数据在fixture中', () => {

  3.     it('测试fixture用法-1', () => {
  4.         // 由于使用默认目录,则路径可以省略,否则需要添加文件所在路径
  5.         cy.fixture("user.json", "utf8").as("userData");
  6.         cy.get("@userData").each((item) => {
  7.             cy.log(`name is ${item.name},age is ${item.age}`);
  8.         });
  9.     });

  10.     it('测试fixture用法-2', () => {
  11.         // 由于使用默认目录,则路径可以省略,否则需要添加文件所在路径
  12.         cy.fixture("user.json", "utf8").then((element) => {
  13.             let data = element;
  14.             data.forEach(item => {
  15.                 cy.log(`name is ${item.name},age is ${item.age}`);
  16.             });
  17.         });
  18.     });
  19. });
  20. </font>
复制代码






欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2