【问题标题】:TestCafe unable to use testController (t) outside of test run (e.g. as a conditional to skip a test)TestCafe 无法在测试运行之外使用 testController (t)(例如,作为跳过测试的条件)
【发布时间】:2020-12-15 19:53:05
【问题描述】:

我正在尝试检查我们在哪个浏览器上运行测试,然后根据结果跳过测试/夹具(如 TestCafe Issue 中所述)。

import { t } from 'testcafe';

fixture `test`
    .page('https://testcafe.devexpress.com')

if (t.browser.name.includes('Chrome')) {
  test('is Chrome?', async () => {
    console.log(t.browser.name);
    await t.expect(t.browser.name.includes('Chrome').ok();
  });
} else {
  test.skip('is Chrome?')
};

结果...

ERROR Cannot prepare tests due to an error.

Cannot implicitly resolve the test run in the context of which the test controller action should be executed. Use test function's 't' argument instead.

有什么方法可以在测试之外调用 testObject (t)?

【问题讨论】:

    标签: testing automation automated-tests e2e-testing testcafe


    【解决方案1】:

    我没有完全解决您的问题的方法。但是我认为最好稍微不同地做,所以结果会是一样的,但是实现它的手段会有所不同。让我解释一下。

    在我看来,将测试用例包装在 if 语句中并不是一个好主意。它主要是杂乱的测试文件,因此您不仅会在左侧看到testfixture,而且还会看到让您在阅读此类文件时停止的语句。当您只想从上到下快速扫描测试文件时,它会带来更多的复杂性。

    解决方案可能是您将meta data 引入您的测试用例(也可以很好地与夹具配合使用)。

    test
        .meta({
            author: 'pavelsaman',
            creationDate: '16/12/2020',
            browser: 'chrome'
        })
        ('Test for Chrome', async t => {
        // test steps
    });
    

    然后你可以像这样只对 Chrome 执行测试:

    $ testcafe --test-meta browser=chrome chrome
    

    这与您想通过条件实现的目标非常相似,但代码更具可读性。

    如果你想同时为 chrome 和 firefox 执行测试,你可以执行更多命令:

    $ testcafe --test-meta browser=chrome chrome
    $ testcafe --test-meta browser=firefox firefox
    

    或:

    $ testcafe --test-meta browser=chrome chrome && testcafe --test-meta browser=firefox firefox
    

    如果您的测试处于流水线中,则可能分两步完成。

    【讨论】:

    • 很好的建议,有很多细节。我的问题是我需要在所有浏览器上运行所有测试。 TestCafe 的新 multiple-window 功能仅在 Chrome、FireFox 和 Edge 上受支持...所以我想在不受支持的浏览器上运行时跳过使用此功能的测试。 -- 目前没有办法使用元数据过滤掉匹配某个值的测试(即test.meta({ multiWindow: 'true' })
    • 是的,TestCafe 有一些限制。也许其他人会找到方法。至于排除测试,可能有程序化的方式(github.com/DevExpress/testcafe/issues/…),能解决你的问题吗?
    • 我认为这是我需要采取的路线(以编程方式忽略/跳过)。如果您想将其添加为解决方案,我会接受它作为答案。
    【解决方案2】:

    this question 中的一个 cmets 所述,更好的解决方案是在运行测试时使用 runner 对象而不是命令行。与其将浏览器作为 CLI 参数传递,不如将其作为可选参数传递给顶级脚本。

    然后,您可以从脚本参数或 .testcaferc.json 文件中读取浏览器变量。

    您需要使用meta data 使用它们适用的浏览器标记所有测试/装置。

    然后,您使用 Runner.filter 方法添加一个委托,如果元数据中的浏览器等于顶级脚本中的浏览器变量,则该委托返回 true

    var runner = testcafe.createRunner();
    var browser = process.env.npm_package_config_browser || require("./testcaferc.json").browser;
    var runner.filter((testName, fixtureName, fixturePath, testMeta, fixtureMeta) => {
        return fixtureMeta.browser === browser || testMeta.browser === browser ;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      • 2021-08-07
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      相关资源
      最近更新 更多