首页 > 解决方案 > 如何访问 cypress/Typescript 中 cy.get() 中定义的值?

问题描述

我需要访问 getLength 函数的值。它返回未定义的值。如何访问此处的值?

我的代码是:

const verifyValue = () => {
  const selector = 'nz-option-container nz-option-item';
  const myActualLength = getLength(selector);
  console.log('I need to access this value :' + myActualLength);
}

const getLength = (selector: string) => {
  let length;
  cy.get(selector).then((listItem) => {
    length = listItem.length;
  })
  return length;
}

标签: typescriptcypress

解决方案


This is how I am able to acces the value:

const getLength = (selector: string) => {
  let length;
  cy.get(selector).then((listItem) => {
    length = listItem.length;
  })
  return cy.wrap(length);
}

getLength(selector).then(value => {
  console.log('I need to access this value :' + value );
})

推荐阅读