赛普拉斯 - 将对象传递给 cy.type 或 cy.contains 命令
Cypress - passing object to cy.type or cy.contains command
我正在生成名字和姓氏,我想在几个 it 测试中使用它们。使用 describe 块,我将它们保存到 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}`)
})
Testing
我做错了什么?
你可以这样做:
it('Treatments', function () {
cy.visit('/')
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.fixture('constants.json').then((ime) => {
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.Firstname)
cy.get('[translate="treatmentsPage.errors.lastNameIsRequired"]')
.should('be.visible')
.and('contain', 'Last name is required!')
cy.get('#lastName').should('be.visible').clear().type(ime.Surname)
})
})
或者,您可以在 beforeEach()
中包含 fixture 文件,然后在整个测试过程中使用它:
describe('Test Suite', function () {
beforeEach(function () {
// "this" points at the test context object
cy.fixture('constants.json').then((constants) => {
// "this" is still the test context object
this.constants = constants
})
})
it('Treatments', function () {
cy.visit('/')
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(this.constants.Firstname)
cy.get('[translate="treatmentsPage.errors.lastNameIsRequired"]')
.should('be.visible')
.and('contain', 'Last name is required!')
cy.get('#lastName')
.should('be.visible')
.clear()
.type(this.constants.Surname)
})
})
我正在生成名字和姓氏,我想在几个 it 测试中使用它们。使用 describe 块,我将它们保存到 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}`)
})
Testing
我做错了什么?
你可以这样做:
it('Treatments', function () {
cy.visit('/')
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.fixture('constants.json').then((ime) => {
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.Firstname)
cy.get('[translate="treatmentsPage.errors.lastNameIsRequired"]')
.should('be.visible')
.and('contain', 'Last name is required!')
cy.get('#lastName').should('be.visible').clear().type(ime.Surname)
})
})
或者,您可以在 beforeEach()
中包含 fixture 文件,然后在整个测试过程中使用它:
describe('Test Suite', function () {
beforeEach(function () {
// "this" points at the test context object
cy.fixture('constants.json').then((constants) => {
// "this" is still the test context object
this.constants = constants
})
})
it('Treatments', function () {
cy.visit('/')
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(this.constants.Firstname)
cy.get('[translate="treatmentsPage.errors.lastNameIsRequired"]')
.should('be.visible')
.and('contain', 'Last name is required!')
cy.get('#lastName')
.should('be.visible')
.clear()
.type(this.constants.Surname)
})
})