【问题标题】:How do i use/print the value inside a generic attribute in TestCafe如何在 TestCafe 的通用属性中使用/打印值
【发布时间】:2019-01-24 12:52:39
【问题描述】:

我要做的是打印/使用Polyline 属性的值。

<Polyline points="x,y x,y x,y x,y">

我试过用这些方法来获取它们:

这是一个实用函数

export const getPointAttribute = async () => {
  const polyline = s.polyline;
  const polylineData = ClientFunction(() => polyline().attributes, {
    dependencies: { polyline }
  });
  return polylineData 
}

这是在测试脚本里面

test('', async (t) => {
   console.log(u.getPointAttribute())
}

test('', async (t) => {
   console.log(s.polyline.getAttribute('points'));
}

我将我的选择器包含在外部

import * as s from '../utilities/selectors';

但我得到的只是控制台日志中的输出承诺

承诺{ }

ReExecutablePromise { _then: [], _fn: [Function], _taskPromise: null }

感谢任何帮助!

【问题讨论】:

    标签: javascript reactjs testing e2e-testing testcafe


    【解决方案1】:

    您应该在 console.log 中等待调用:

    test('', async (t) => {
       console.log(await s.polyline.getAttribute('points'));
    }
    

    test('', async (t) => {
       console.log(await s.polyline.getAttribute('points'));
    }
    

    【讨论】:

      【解决方案2】:

      您的getPointAttribute 函数返回polylineData 对象,它是ClientFunction type 的一个实例(反过来,它基于Promises)。这就是为什么当您登录 u.getPointAttribute() 时,您会收到这些消息。您需要做的就是在调用 ClientFunction 之前使用 await 关键字。请看以下代码:

      const polylineData = ClientFunction(() => polyline().attributes, {
          dependencies: { polyline }
      });
      await polylineData();
      

      参考以下文章获取更多信息https://devexpress.github.io/testcafe/documentation/test-api/obtaining-data-from-the-client/

      我还想提一下,您不需要在 getPointAttribute 函数中使用 async

      【讨论】:

      • 感谢您的回复!异步是一个草率的副本,感谢您的注意,它已被删除。不幸的是,该函数仍然返回: Promise { }
      【解决方案3】:

      我已经设法让它与感兴趣的人一起使用的实用功能。

      export function getPoints(object: Selector) : Promise<string> {
        return object.getAttribute('points');
      }
      

      这使得处理数据变得更容易、更清晰。

      import * as u from '../utilities/functions';
      import * as s from '../utilities/selectors';
      console.log(await u.getPoints( s.polyline ));
      

      感谢大家的帮助!

      【讨论】:

        猜你喜欢
        • 2016-03-30
        • 1970-01-01
        • 2022-01-24
        • 2019-07-17
        • 2019-09-08
        • 2019-03-19
        • 2019-01-21
        • 2011-09-05
        • 1970-01-01
        相关资源
        最近更新 更多