如何修复错误“TypeError:cy.[自定义命令]不是函数”?

Vai*_*kar 10 javascript typescript cypress

我在commands.js 文件中编写了一些函数用于cypress 自动化测试,其中我只能调用一个(即“登录”),但无法调用另一个.js 文件中的其他函数。赛普拉斯测试运行程序显示

“类型错误:cy.FillAddCaseDetails不是一个函数”

describe('Adding a Case on CSS Poratal ', function() {

  before(function () {
    cy.login()    // calling login function successfully
  })

  it('open add case',function(){
    cy.wait(9000)
    cy.hash().should('contains','#/home')
    cy.wait(['@GETcontentLoad']);
    cy.wait(['@POSTcontentLoad']);
    cy.get('[uib-tooltip="Add Case"]').click({force:true})
    cy.log('clicked on Add case')
    cy.wait(3000) 
    cy.get('[ng-click="lookup.cancel()"]').click({force: true})
    cy.get('[ng-click="lookup.closeAddCase()"]').click({force: true})
    cy.get('[uib-tooltip="Add Case"]').click({force:true}) 
    cy.wait(3000)
    cy.get('[ng-model="lookup.selectedPartner"]',{force:true})
      .type(AddJob.JobData.Partner,{force: true}) 
    cy.xpath('//input[@ng-model="lookup.selectedPartner"]')
      .should('be.visible').then(() => {
        cy.FillAddCaseDetails()   // unable to call   
        cy.FillCustomerDetails()  // unable to call 
      })
Run Code Online (Sandbox Code Playgroud)

功能:

Cypress.Commands.add("FillCustomerDetails", () => {

  cy.get('[ng-model="lookup.firstName"]')
    .type(AddJob.JobData.FirstName, { force: true}) 

  cy.get('[ng-model="lookup.lastName"]')
    .type(AddJob.JobData.LastName, { force: true })

  cy.get('[ng-model="lookup.customerPhone"]')
    .type(AddJob.JobData.CustomerPhone, { force: true })

  cy.get('[value="NEXT"]').click({ force: true })
})
Run Code Online (Sandbox Code Playgroud)

预期:函数将被调用
实际:类型错误:cy.FillAddCaseDetails不是函数

Nab*_*bel 11

这是此错误的最高结果,因此我想添加我所做的修复操作。这与版本 >=10 和 typescript 相关。问题最终是supportFile设置cypress.config.ts被设置为false;我将我的配置更改为:

import cypress, { defineConfig } from 'cypress'

export default defineConfig({
  
  e2e: {
    'baseUrl': 'http://localhost:4200',
    supportFile: 'cypress/support/e2e.ts'
  },
  
  
})
Run Code Online (Sandbox Code Playgroud)

我在中创建了自定义命令commands.ts

declare namespace Cypress {
  interface Chainable<Subject = any> {
    /**
     * Custom command to select DOM element by data-cy attribute.
     * @example cy.dataCy('greeting')
    */
     clearIndexedDB(): Promise<void>
  }
}

Cypress.Commands.add('clearIndexedDB', async () => {
  const databases = await window.indexedDB.databases();

  await Promise.all(
    databases.map(
      ({ name }) => {
        if (!name) return
        return new Promise((resolve, reject) => {
          const request = window.indexedDB.deleteDatabase(name);

          request.addEventListener('success', resolve);
          request.addEventListener('blocked', resolve);
          request.addEventListener('error', reject);
        })
      },
    ),
  );
});
Run Code Online (Sandbox Code Playgroud)

然后我在 e2e.ts 文件中取消注释此行

import './commands';
Run Code Online (Sandbox Code Playgroud)


bku*_*era -2

来自赛普拉斯文档:https://on.cypress.io/typescript#Types-for-custom-commands

如果将命令 cy.dataCy 添加到 supportFile 中,如下所示:

// cypress/support/index.js
Cypress.Commands.add('dataCy', (value) => {
  return cy.get(`[data-cy=${value}]`)
})
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过在 supportFile 旁边创建一个新的 TypeScript 定义文件(本例中为 cypress/support/index.d.ts),将 dataCy 命令添加到全局 Cypress Chainable 接口(之所以这样称呼是因为命令链接在一起)。

// in cypress/support/index.d.ts
// load type definitions that come with Cypress module
/// <reference types="cypress" />

declare namespace Cypress {
  interface Chainable {
    /**
     * Custom command to select DOM element by data-cy attribute.
     * @example cy.dataCy('greeting')
    */
    dataCy(value: string): Chainable<Element>
  }
}
Run Code Online (Sandbox Code Playgroud)