根据使用 cypress 提供的键从 json 中检索值
Retrieve value from json based on key provided using cypress
let expectedKey = 'Student';
cy.readFile('cypress/fixtures/applicationDetails.json').then((appDetails) => {
if(expectedKey === 'Student'){
cy.get('app-screen').find('#code-details').should('have.text', appDetails.studentCode);
}
if(expectedDKey === 'Department'){
cy.get('app-screen').find('#code-details').should('have.text', appDetails.departmentCode);
}
if(expectedKey === 'Paper'){
cy.get('app-screen').find('#code-details').should('have.text', appDetails.paperCode);
}
if(expectedKey === 'Results'){
cy.get('app-screen').find('#code-details').should('have.text', appDetails.resultsCode);
}
}
我不想使用这么多 if 块,因为将来会有更多的键。相反,我必须根据 expectedKey 从 JSON 中为 studentCode、departmentCode、paperCode 或 resultsCode 选择所需的值。有什么帮助吗?
假设你在cy.readFile()
里面有expectedKey
,你可以这样做:
- 在
cypress/support/commands.js
创建自定义命令:
Cypress.Commands.add('codeDetailsText', (expectedKey, appDetails) => {
expectedKeyCode = expectedKey.toLowerCase() + 'Code'
cy.get('app-screen')
.find('#code-details')
.should('have.text', appDetails[expectedKeyCode])
})
- 在你的测试中只写:
cy.readFile('cypress/fixtures/applicationDetails.json').then((appDetails) => {
//Assuming expectedKey value is available here
cy.codeDetailsText(expectedKey, appDetails)
})
您可以通过点表示法 (foo.bar
) 或括号表示法 (foo['bar']
) 访问对象属性。在您的情况下,您必须确保 expectedKey
将对象中的有效键与 cy 命令之前的断言相匹配。
let expectedKey = 'studentCode';
cy.readFile('cypress/fixtures/applicationDetails.json').then((appDetails) => {
expect(appDetails, 'valid key').to.have.property(expectedKey)
cy.get('app-screen').find('#code-details').should('have.text', appDetails[expectedKey]);
}
let expectedKey = 'Student';
cy.readFile('cypress/fixtures/applicationDetails.json').then((appDetails) => {
if(expectedKey === 'Student'){
cy.get('app-screen').find('#code-details').should('have.text', appDetails.studentCode);
}
if(expectedDKey === 'Department'){
cy.get('app-screen').find('#code-details').should('have.text', appDetails.departmentCode);
}
if(expectedKey === 'Paper'){
cy.get('app-screen').find('#code-details').should('have.text', appDetails.paperCode);
}
if(expectedKey === 'Results'){
cy.get('app-screen').find('#code-details').should('have.text', appDetails.resultsCode);
}
}
我不想使用这么多 if 块,因为将来会有更多的键。相反,我必须根据 expectedKey 从 JSON 中为 studentCode、departmentCode、paperCode 或 resultsCode 选择所需的值。有什么帮助吗?
假设你在cy.readFile()
里面有expectedKey
,你可以这样做:
- 在
cypress/support/commands.js
创建自定义命令:
Cypress.Commands.add('codeDetailsText', (expectedKey, appDetails) => {
expectedKeyCode = expectedKey.toLowerCase() + 'Code'
cy.get('app-screen')
.find('#code-details')
.should('have.text', appDetails[expectedKeyCode])
})
- 在你的测试中只写:
cy.readFile('cypress/fixtures/applicationDetails.json').then((appDetails) => {
//Assuming expectedKey value is available here
cy.codeDetailsText(expectedKey, appDetails)
})
您可以通过点表示法 (foo.bar
) 或括号表示法 (foo['bar']
) 访问对象属性。在您的情况下,您必须确保 expectedKey
将对象中的有效键与 cy 命令之前的断言相匹配。
let expectedKey = 'studentCode';
cy.readFile('cypress/fixtures/applicationDetails.json').then((appDetails) => {
expect(appDetails, 'valid key').to.have.property(expectedKey)
cy.get('app-screen').find('#code-details').should('have.text', appDetails[expectedKey]);
}