【问题标题】:How can I access the value defined inside the cy.get() in cypress/Typescript?如何访问 cypress/Typescript 中 cy.get() 中定义的值?
【发布时间】:2020-10-09 14:51:22
【问题描述】:

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

【问题讨论】:

    标签: typescript cypress


    【解决方案1】:
    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 );
    })
    

    【讨论】:

      【解决方案2】:

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

      所以不妨试试:

      cy.get(selector).then((listItem) => {
         return listItem.length;
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-25
        • 1970-01-01
        • 2019-03-29
        • 2012-11-15
        • 2015-03-26
        • 1970-01-01
        • 2019-11-11
        • 1970-01-01
        相关资源
        最近更新 更多