51Testing软件测试论坛
标题: 如何在WEB自动化中对数据驱动? [打印本页]
作者: 海鸥一飞 时间: 2022-10-8 13:29
标题: 如何在WEB自动化中对数据驱动?
1 数据驱动 数据驱动是测试框架中一个非常好的功能,使用数据驱动,可以在不增加代码量的情况下生成不同的测试策略。下面我们来看看在Cypress中的数据驱动使用方法。
1 数据在文件中 在前面已经使用很多次,示例如下所示:
- <font size="3"><font size="3">[
- {
- "ID": "Data-1",
- "name": "Surpass",
- "age": 28
- },
- {
- "ID": "Data-2",
- "name": "Kevin",
- "age": 29
- }
- ]
- </font></font>
复制代码 示例代码如下所示:
- <font size="3"><font size="3">/// <reference types="cypress" />
- import Data from "./user.json"
- describe('数据在文件中', () => {
- Data.forEach(item => {
- it(item.ID, () => {
- cy.log(`name is ${item.name},age is ${item.age}`);
- });
- });
- });
- </font></font>
复制代码 1.2 使用fixture Cypress中fixture默认位于cypress\fixtures\example.json文件中。操作步骤如下所示:
- 在cypress\fixtures\example.json,创建user.json,并添加以下的数据:
- <font size="3">[
- {
- "ID": "Data-1",
- "name": "Surpass",
- "age": 28
- },
- {
- "ID": "Data-2",
- "name": "Kevin",
- "age": 29
- }
- ]
- </font>
复制代码- <font size="3">/// <reference types="cypress" />
- describe('数据在fixture中', () => {
- it('测试fixture用法-1', () => {
- // 由于使用默认目录,则路径可以省略,否则需要添加文件所在路径
- cy.fixture("user.json", "utf8").as("userData");
- cy.get("@userData").each((item) => {
- cy.log(`name is ${item.name},age is ${item.age}`);
- });
- });
- it('测试fixture用法-2', () => {
- // 由于使用默认目录,则路径可以省略,否则需要添加文件所在路径
- cy.fixture("user.json", "utf8").then((element) => {
- let data = element;
- data.forEach(item => {
- cy.log(`name is ${item.name},age is ${item.age}`);
- });
- });
- });
- });
- </font>
复制代码
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) |
Powered by Discuz! X3.2 |