How to access specific record from fixture/*.json file into sepcific test case in cypress?(如何将Fixture/*.json文件中的特定记录访问到Cypress中特定的测试用例中?)
问题描述
testdata.json
[
{
"case_id": 1,
"case": "Login with valid data as wholesaler",
"username": "admin",
"password": "password",
"result": "pass"
},
{
"case_id": 2,
"case": "Login with valid data as reseller",
"username": "myreseller.admin",
"password": "password",
"result": "pass"
},
{
"case_id": 3,
"case": "Login with valid data as subscriber",
"username": "mytenant.admin",
"password": "password",
"result": "pass"
}
]
login.spec.js
before(() => {
cy.fixture('testdata').then((datajson) => {
testdata = datajson
return testdata
})
})
------it blocks-----
it.only('TC03 - Login with valid data as subscriber', () => {
cy.login(testdata.username, testdata.password);
cy.title().should('equal', 'Home');
cy.logout();
-------more it blocks-------
如何正确使用它,以便第2行返回
{testdata.username=mytenant.admin||testdata.password=password}
具体从第3条记录提取
推荐答案
在Cypress文档中,有两种方法描述了如何处理Fixtures文件夹中的那些JSON文件:https://docs.cypress.io/api/commands/fixture#Examples
1.选项
使用cy.fixture()加载testdata.json:
cy.fixture('testdata.json').as('testdata');
2.选项(我的推荐)
在login.spec.js中,您可以从Fixtures文件夹导入所有相关的json文件:
import testdata from '../fixtures/testdata.json';
然后您可以按如下方式直接使用它们,例如使用第一个用户的用户名和密码:
it.only('TC03 - Login with valid data as subscriber', () => {
cy.login(testdata[0].username, testdata[0].password);
cy.title().should('equal', 'Home');
cy.logout();
})
这篇关于如何将Fixture/*.json文件中的特定记录访问到Cypress中特定的测试用例中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将Fixture/*.json文件中的特定记录访问到Cypress中特定的测试用例中?
基础教程推荐
- Bootstrap 模态出现在背景下 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
