如何覆盖标准的testcafe错误? [英] How to override standard testcafe errors?

查看:100
本文介绍了如何覆盖标准的testcafe错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过在其中添加选择器的输入定位器来覆盖标准的testcafe错误消息-这样,我将了解更多关于失败的信息,并且可以至少通过手动检查是否可以找到选择器来手动调试失败.

I need to override standard testcafe error messages by adding there Selector's input locator - this way I will know more about failure and can debug that manually at least by manually checking ability for selector to be found.

当我尝试通过此代码捕获错误时:

When I try just to catch error by this code:

  try {
    const selector = Selector('basdf')
    await t.click(selector);
  }
  catch (e) {
    console.log(e);
  }

我得到了这个物体:

{
  code: 'E24',
  isTestCafeError: true,
  callsite: CallsiteRecord {
    filename: '/Users/myPath/helper_test.ts',
    lineNum: 37,
    callsiteFrameIdx: 6,
    stackFrames: [
      [CallSite], [CallSite],
      [CallSite], [CallSite],
      [CallSite], [CallSite],
      [CallSite], CallSite {},
      [CallSite], [CallSite],
      [CallSite]
    ],
    isV8Frames: true
  },
  apiFnChain: [ "Selector('basdf')" ],
  apiFnIndex: 0
}

如果我只留下这样的代码:

And if I'll just leave code like this:

const selector = Selector('basdf')
await t.click(selector);

我会得到我所需要的-

 ✖ Edits auto on propositions

   1) The specified selector does not match any element in the DOM tree.
      
       > | Selector('basdf')

      Browser: Chrome 83.0.4103.106 / macOS 10.15.5

         33 |
         34 |  // const someVar = 1;
         35 |
         36 |  // try {
         37 |    const selector = Selector('basdf')
       > 38 |    await t.click(selector);
         39 |  // }
         40 |  // catch (e) {
         41 |  //   console.log(e);
         42 |  // }
         43 |});

         at <anonymous> (/Users/orkhan.mamedov/WebstormProjects/osago/tests/e2e/helper_test.ts:38:13)
         at fulfilled (/Users/orkhan.mamedov/WebstormProjects/osago/tests/e2e/helper_test.ts:5:58)



 1/1 failed (18s)

是否可以将自定义信息添加到该行:

Is there any option to add custom info to that line:

指定的选择器与DOM树中的任何元素都不匹配.

The specified selector does not match any element in the DOM tree.

所以会是这样:

指定的带有定位符的选择器:选择器的定位器"与DOM树中的任何元素都不匹配.

The specified selector with locator: 'locator of a selector' does not match any element in the DOM tree.

UPD 1

我找到了一种猴子修补的方法.它即将为异常对象(例如"locator")分配一些道具,然后只修复/testcafe/lib/errors/test-run/templates.js文件:

I've found a monkey-patchey way to do that. Its about to assign some prop to exception object, for example 'locator', and then just fix /testcafe/lib/errors/test-run/templates.js file:

  const locator = 'basdf'
  try {
    const selector = Selector(locator)
    await t.click(selector);
  }
  catch (e) {
    console.log(e);
    Object.assign(e,{ locator });
    throw e;
  }

...
[types_1.TEST_RUN_ERRORS.actionElementNotFoundError]: (err, viewportWidth) => `
        The specified selector with locator value: '${err.locator}' does not match any element in the DOM tree.

        ${utils_1.formatSelectorCallstack(err.apiFnChain, err.apiFnIndex, viewportWidth)}
    `,
...

UPD 2

我的问题是,当我以这种方式编写了自定义实现的xpath选择器时:

My problem is when I have custom implemented xpath selector written in this way:

// xpath-selector.js
import { Selector } from 'testcafe';

const elementByXPath = Selector((xpath) => {
  const iterator = document.evaluate(
    xpath,
    document,
    null,
    XPathResult.UNORDERED_NODE_ITERATOR_TYPE,
    null
  );
  const items = [];

  let item = iterator.iterateNext();

  while (item) {
    items.push(item);
    item = iterator.iterateNext();
  }

  return items;
});

export default function (locator: string, timeout?: number) {
  // @ts-ignore
  if (!timeout) timeout = 9000;

  return Object.assign(
    Selector(elementByXPath(locator), { timeout: timeout }),
    { locator }
  );
}

我得到了:

 ✖ Edits auto on propositions

   1) The specified selector does not match any element in the DOM tree.
      
       > | Selector([function])

UPD 3 这完全取决于这些代码行:

UPD 3 It's all up to those lines of code:

 if (!this.options.apiFnChain) {
            const fnType = typeof this.fn;
            let item = fnType === 'string' ? `'${this.fn}'` : `[${fnType}]`;
            item = `Selector(${item})`;
            this.options.apiFn = item;
            this.options.apiFnChain = [item];
        }

在selector-builder.js上,现在我试图弄清楚如何修补它以便能够获得客户端的xpath函数.有什么建议吗?

on selector-builder.js, and for now I'm trying to figure out how to patch this to be able to get client-side xpath function. Any suggestions?

推荐答案

昨天我遇到了同样的问题.这不会覆盖您的错误消息,但可以帮助我抛出更有用的错误消息.

I had the same issue yesterday. This will not override your error message but it helped me to throw a more helpful error message.

try {
    await t.hover(selector)
} catch (error) {
    const selectorFnChain = selector[Object.getOwnPropertySymbols(selector)[0]].options.apiFnChain
//  use selectorFnChain in your error message
}

您不必了解整个链条,但这对我最有帮助.

You don't have to get the entire chain but this was the most helpful for me.

这篇关于如何覆盖标准的testcafe错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆