如何解决错误“ typeError:cy。[自定义命令]不是函数”?

问题描述 投票:0回答:5
i在命令中写了一些函数。JS文件以进行柏树自动化测试,其中我只能调用一个功能,即“登录”,但无法调用其他功能构成另一个.js文件。柏树测试跑者显示

“ TypeError:

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

不是函数

javascript typescript cypress
5个回答
12
投票
这是此错误的首要结果,因此我想添加我为解决它所做的工作。 这与版本> = 10和打字稿有关。 问题最终是将

supportFile

设置为
cypress.config.ts
;  我更改了配置:
false

I在
import cypress, { defineConfig } from 'cypress'

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

中创建了自定义命令

commands.ts

然后我在我的e2e.ts file
中删除了这一行
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); }) }, ), ); });

如果您将自定义命令添加到

3
投票
文件中,则需要从

support/commands.js

文件导入该文件。创建
support/index.js
,如果不可用,并将行添加到其中。
    
在我的案例解决方案中,是柏树测试跑者的重新启动。
    

您可以通过添加以下行尝试来修改cypress.config.ts:

1
投票

如下所示:


0
投票

support/index.js

},

});

从柏树文档:
https://on.cypress.io/typescript#types-for-custom-commands

如果您将命令cy.datacy添加到您的支持file中:


-3
投票

然后,您可以通过创建supportfile旁边的新的打字稿定义文件来将数据命令添加到全局柏树可链接口(因此称为命令被链接在一起),在这种情况下为cypress/support/support/index.d.ts. supportFile: 'cypress/support/index.ts', setupNodeEvents(on, config) { // implement node event listeners here },

Cy.xpath(“ // Div [@class ='C-navigatorItem-faceplate ng-scope ng-scope ng-sely-sely-scope']”)。单击();
使用是有效的,因为我得到了typeerror cy.xpath不是函数

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.