如何访问cypress/Typescript中的cy.get()中定义的值?

如何访问cypress/Typescript中的cy.get()中定义的值?,typescript,cypress,Typescript,Cypress,我需要访问getLength函数的值。它正在返回未定义的值。我如何访问此处的值 我的代码是: const verifyValue = () => { const selector = 'nz-option-container nz-option-item'; const myActualLength = getLength(selector); console.log('I need to access this value :' + myActualLength); } c

我需要访问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;
}

根据我对cypress和javascript回调的理解,您需要从内部返回值。然后。如果尝试从该函数外部访问该值,它将返回undefined

因此,不妨尝试:

cy.get(selector).then((listItem) => {
   return listItem.length;
})
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 );
})