In TestCafe is there a way to know if the test passed or failed in after hook?

只愿长相守 提交于 2019-12-23 21:27:52

问题


I am trying to mark tests as pass/failed through a rest API (Zephyr) while my testcafe tests are running. I was wondering if it's possible in the after or afterEach hook to know if the test passed/failed so that I can run some script based on the result.

Something like:

test(...)
.after(async t => {
  if(testFailed === true) { callApi('my test failed'); }
})

回答1:


I see two ways in which to solve your task. First, do not subscribe to the after hook, but create your own reporter or modify the existing reporter. Please refer to the following article: https://devexpress.github.io/testcafe/documentation/extending-testcafe/reporter-plugin/#implementing-the-reporter   The most interesting method there is reportTestDone because it accepts errs as a parameter, so you can add your custom logic there.

The second approach is using sharing variables between hooks and test code

You can write your test in the following manner:

test(`test`, async t => {
    await t.click('#non-existing-element');

    t.ctx.passed = true;
}).after(async t => {
    if (t.ctx.passed)
        throw new Error('not passed');
});

Here I am using the shared passed variable between the test code and hook. If the test fails, the variable will not be set to true, and I'll get an error in the after hook.



来源:https://stackoverflow.com/questions/53656728/in-testcafe-is-there-a-way-to-know-if-the-test-passed-or-failed-in-after-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!