Testcafe-在运行所有夹具后如何运行代码

时间:2018-11-12 21:47:42

标签: sql-server automated-tests e2e-testing testcafe

我想创建SQL Server数据库的快照,然后在“所有固定装置”运行之后将其还原。

我可以在每个固定装置之后通过钩住固定装置后进行操作。但是,这在运行测试时会引起问题,因为还原后数据库可能仍处于过渡状态。所以我宁愿在所有固定装置之后做。

2 个答案:

答案 0 :(得分:1)

I have found a workaround for now. The workaround is:

  1. Add dependency for ts-node. I also had to add tsconfig.json with compiler options' lib property set to es2015 and dom. If not, it was complaining about Promise.
  2. I created a file called create-snapshot and created the snapshot there.
  3. I created another file called restore-snapshot and restored the snapshot in that file.
  4. I added two entries in package.json's scripts like "create-ss": "ts-node ./create-snapshot.ts", "restore-ss": "ts-node ./restore-snapshot.ts"
  5. Now from Powershell, I run the tests with the command: npm run create-ss;npm run test-chrome-hl;npm run restore-ss. This runs the commands sequentially in Powershell. In other terminals you may need to use && or something else instead of ;.

I can avoid "npm run create-ss" by using the .before hook of the fixture by keeping track of a variable to ensure it runs only once. However I cannot do a similar approach when the last test gets executed.

Its a bit of a pain to remember the three commands but I dont see another way so far.

答案 1 :(得分:1)

您还可以使用TestCafe Programming Interface API。 TestCafe Runner class返回Promise对象。完成所有测试/固定后,您可以使用此对象运行自定义清理代码。

这是一个例子:

const createTestCafe = require('testcafe');
let testcafe         = null;

createTestCafe('localhost', 1337, 1338)
    .then(tc => {
        testcafe     = tc;
        const runner = testcafe.createRunner();

        return runner
            .src(['tests/fixture1.js', 'tests/fixture2.js', 'tests/fixture3.js'])
            .browsers(['chrome', 'safari'])
            .run();
    })
    .then(failedCount => {
        // Clean up your database here...
        testcafe.close();
    });