Cypress - 将对象传递给 cy.type 或 cy.contains 命令
我正在生成姓名,我想在几个 IT 测试中使用它们。使用描述块,我将它们保存到 JSON 文件中。然后我想在 cy.type 或 cy.contains 中使用它们。
describe("Constants", function () {
const uuid = () => Cypress._.random(0, 1e3)
const suid = () => Cypress._.random(0, 1e3)
const id = uuid()
const sid = suid()
const Firstname = `Test${id}`
const Surname = `Patient${sid}`
it("Copy constants", function () {
cy.writeFile('cypress/fixtures/constants.json', { "Firstname" : Firstname, "Surname" : Surname})
})
})
当我在测试中使用这两个变量时,它们表示为对象(参见图片)
it('Treatments', function() {
cy.visit('/')
cy.fixture("constants.json").then(ime => {
cy.log(ime.Firstname)
cy.fixture("constants.json").then(priimek => {
cy.log(priimek.Surname)
cy.get('a.ng-tns-c80-4').click()
cy.get('path[d="M19 11h-6V5a1 1 0 0 0-2 0v6H5a1 1 0 0 0 0 2h6v6a1 1 0 0 0 2 0v-6h6a1 1 0 0 0 0-2z"]').click()
cy.get('.d-flex > .appearance-filled').should('be.visible').and('contain','Create').click()
//Patient info
cy.get('[translate="treatmentsPage.patientInformationTitle"]').should('be.visible').and('contain','Patient information')
cy.get('[translate="treatmentsPage.errors.firstNameIsRequired"]').should('be.visible').and('contain','First name is required!')
cy.get('#firstName').should('be.visible').clear().type(`${ime}`)
cy.get('[translate="treatmentsPage.errors.lastNameIsRequired"]').should('be.visible').and('contain','Last name is required!')
cy.get('#lastName').should('be.visible').clear().type(`${priimek}`)
})
我做错了什么?
I'm generating name and surname, which I want to use them in several it tests. With describe block I save them into JSON file. Then I want to use them in cy.type or cy.contains.
describe("Constants", function () {
const uuid = () => Cypress._.random(0, 1e3)
const suid = () => Cypress._.random(0, 1e3)
const id = uuid()
const sid = suid()
const Firstname = `Test${id}`
const Surname = `Patient${sid}`
it("Copy constants", function () {
cy.writeFile('cypress/fixtures/constants.json', { "Firstname" : Firstname, "Surname" : Surname})
})
})
When I use both variables in it test, they're represented as object (see picture)
it('Treatments', function() {
cy.visit('/')
cy.fixture("constants.json").then(ime => {
cy.log(ime.Firstname)
cy.fixture("constants.json").then(priimek => {
cy.log(priimek.Surname)
cy.get('a.ng-tns-c80-4').click()
cy.get('path[d="M19 11h-6V5a1 1 0 0 0-2 0v6H5a1 1 0 0 0 0 2h6v6a1 1 0 0 0 2 0v-6h6a1 1 0 0 0 0-2z"]').click()
cy.get('.d-flex > .appearance-filled').should('be.visible').and('contain','Create').click()
//Patient info
cy.get('[translate="treatmentsPage.patientInformationTitle"]').should('be.visible').and('contain','Patient information')
cy.get('[translate="treatmentsPage.errors.firstNameIsRequired"]').should('be.visible').and('contain','First name is required!')
cy.get('#firstName').should('be.visible').clear().type(`${ime}`)
cy.get('[translate="treatmentsPage.errors.lastNameIsRequired"]').should('be.visible').and('contain','Last name is required!')
cy.get('#lastName').should('be.visible').clear().type(`${priimek}`)
})
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样做:
或者,您可以将固定文件放在
beforeEach()
中,然后在整个测试过程中使用它:You can do like this:
Or, You can have the fixture file in the
beforeEach()
and then use it throughout the test: