Cypress - 将对象传递给 cy.type 或 cy.contains 命令

发布于 2025-01-11 06:28:18 字数 1704 浏览 0 评论 0原文

我正在生成姓名,我想在几个 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}`)

})

Testing

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2025-01-18 06:28:19

您可以这样做:

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() 中,然后在整个测试过程中使用它:

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)
  })
})

You can do like this:

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)
  })
})

Or, You can have the fixture file in the beforeEach() and then use it throughout the test:

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)
  })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文