有人在运行cypress测试时使用excel来存储测试数据吗?我正在尝试使其工作,但由于无法使用browserify访问文件系统,因此无法读取excel测试数据文件。有人在工作吗?您是否有任何有关如何使其工作的链接/代码?
谢谢。
答案 0 :(得分:0)
我意识到,由于您使用的是browserfiy,因此普通的excel处理程序包无法与cypress和typescript一起使用。 browserify将阻止您执行文件读/写操作。
作为一种解决方法,我只在浏览器对其进行浏览之前(在plugins / index.js中)读取excel文件,然后将其转换为json文件并将其保存在Fixtures文件夹中。
然后在supportAll / index.js中的beforeAll挂钩中,我读取json文件作为fixture,并将其保存到别名变量中。现在,我可以从别名变量中解析数据,并在整个测试中的赛普拉斯上下文中按需使用它。这对我有用。
答案 1 :(得分:0)
以下是有关如何将excel用作赛普拉斯测试https://medium.com/@you54f/dynamically-generate-data-in-cypress-from-csv-xlsx-7805961eff55
的说明首先,您需要使用Xlsx将xlsx文件转换为json
import { writeFileSync } from "fs"; import * as XLSX from "xlsx"; try { const workBook = XLSX.readFile("./testData/testData.xlsx"); const jsonData = XLSX.utils.sheet_to_json(workBook.Sheets.testData); writeFileSync( "./cypress/fixtures/testData.json", JSON.stringify(jsonData, null, 4), "utf-8" ); } catch (e) { throw Error(e); }
然后导入json文件并遍历每一行,并以所需的方式使用数据。在此示例中,它尝试登录到系统。
import { login } from "../support/pageObjects/login.page"; const testData = require("../fixtures/testData.json"); describe("Dynamically Generated Tests", () => { testData.forEach((testDataRow: any) => { const data = { username: testDataRow.username, password: testDataRow.password }; context(`Generating a test for ${data.username}`, () => { it("should fail to login for the specified details", () => { login.visit(); login.username.type(data.username); login.password.type(`${data.password}{enter}`); login.errorMsg.contains("Your username is invalid!"); login.logOutButton.should("not.exist"); }); }); }); });