Angular 从Cypress测试中调用组件函数
我有一个角度7应用程序。我正在使用cypress测试一些画布/地图组件。我需要调用组件中的函数来验证地图上显示的geojson 在chrome中,我通过控制台调用Angular 从Cypress测试中调用组件函数,angular,e2e-testing,cypress,Angular,E2e Testing,Cypress,我有一个角度7应用程序。我正在使用cypress测试一些画布/地图组件。我需要调用组件中的函数来验证地图上显示的geojson 在chrome中,我通过控制台调用ng.probe($0).componentInstance.draw.getAll(),我的数据被记录到控制台中,但在cypress测试中进行相同调用时: cy.window().then((win) => { const res = win.ng.probe($0).componentInstance.draw.get
ng.probe($0).componentInstance.draw.getAll()
,我的数据被记录到控制台中,但在cypress测试中进行相同调用时:
cy.window().then((win) => {
const res = win.ng.probe($0).componentInstance.draw.getAll();
console.log(res);
})
我得到ReferenceError:$0未定义
如何在cypress中调用角度函数?变量
$0
仅存在于chrome devtools
中。它是对您在“元素”面板中选择的元素的引用,如下所示(请注意==0
):
您需要通过cy.get
获取对实际元素的引用
cy.get('.some ng元素')。然后($el)=>{
const el=$el[0]//从jquery元素获取DOM元素
const win=el.ownerDocument.defaultView//从DOM元素获取窗口
const component=win.ng.probe(el.componentInstance)
})