【问题标题】:How to fix the Error "TypeError: cy.[custom command] is not a function"?如何修复错误“TypeError: cy.[custom command] is not a function”?
【发布时间】:2019-09-11 11:47:50
【问题描述】:

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

功能:

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

预期:函数将被调用
实际:TypeError:cy.FillAddCaseDetails 不是函数

【问题讨论】:

  • 您可以edit您的问题,请不要在cmets中发布代码

标签: javascript typescript cypress


【解决方案1】:

在我的情况下,解决方案是重启 cypress 测试运行器。

【讨论】:

    【解决方案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}]`)
    })
    

    然后,您可以通过在您的 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>
      }
    }
    

    【讨论】:

      【解决方案3】:

      cy.xpath("//div[@class='c-navigatorItem-faceplate ng-scope ng-isolate-scope']").click(); 使用它是否有效,因为我收到 TypeError cy.xpath is not a function

      【讨论】:

      • 嗨!欢迎来到 StackOverflow!在询问之前,请务必阅读我们的文章:How do I write a good answer?。确保将 Answer 字段用于 Answers,将 Comment 字段用于 cmets。希望对您有所帮助!
      猜你喜欢
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2015-11-01
      • 2019-07-20
      相关资源
      最近更新 更多