【问题标题】:Root Hook Plugin to Run Before All Tests with Mocha and Playwright在 Mocha 和 Playwright 的所有测试之前运行的 Root Hook 插件
【发布时间】:2021-07-04 07:00:44
【问题描述】:

我通过将this plugin 添加到我的项目中,为我的 Vuejs 项目连接了一些自动化测试。

Vuejs 应用是使用 vue-cli 创建的。

一切正常,Playwright 似乎是一个非常强大的自动化库。

所以我正在尝试编写一个 Mocha 根挂钩插件,该插件在所有测试之前运行一次。看起来像这样:

hooks.js

const playwright = require("playwright");

export const mochaHooks = {
  beforeAll() {
    const browser = await playwright["chromium"].launch({
      headless: false
    });

    const context = await browser.newContext();

    await page.goto("https://localhost:44392/");

    await page.fill("#Username", "demokitchenadmin");
    await page.fill("#Password", "Test1234)");

    await page.click("button.btn-primary");

    await context.storageState({ path: 'state.json' });

    await page.close();
    await context.close();
    await browser.close();
  }
};

我不确定如何将它与我的 Vuejs 项目集成。 我试图添加--require 标志(doco),以便测试命令看起来像这样(在package.json 中):

"scripts": {
    ...
    "test:e2e": "vue-cli-service test:e2e --require './tests/e2e/hooks.js",
},

很遗憾,这并没有成功运行 hooks.js 文件中的代码。

谁能把我推向正确的方向。 干杯

【问题讨论】:

    标签: vue.js automated-tests vue-test-utils playwright


    【解决方案1】:

    好的。所以我很接近。
    我不得不使用 CommonJS 语法。
    我想我需要继续努力,因为我想让它在 Typescript 中工作。

    无论如何,对于其他希望解决此问题的人,我将我的 hooks.js 文件更改如下:

    const playwright = require("playwright");
    
    
    exports.mochaHooks = {
      async beforeAll() {
    
        const browser1 = await playwright["chromium"].launch({
          headless: false
          //devtools: true
        });
    
        const context1 = await browser1.newContext();
        const page = await context1.newPage();
    
        await page.goto("https://localhost:44392/account/login");
    
    
        await page.fill("#Username", "demokitchenadmin");
        await page.fill("#Password", "Test1234)");
    
        await page.click("button.btn-primary");
    
        await context1.storageState({ path: 'state.json' });
    
        await page.close();
        await context1.close();
        await browser1.close();
      }
    };
    

    在我的package.json:

    "scripts": {
        ...
        "test:e2e": "vue-cli-service test:e2e -r ./tests/e2e/hooks",
    },
    

    这行得通,并且该身份验证代码在我的所有测试之前运行一次。

    【讨论】:

    • 请接受您自己的答案,以便让未来的读者清楚它是有效的。谢谢
    【解决方案2】:

    使用可以在 playwright.config.ts 中定义的全局设置函数 (https://playwright.dev/docs/test-advanced#global-setup-and-teardown)。

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 2019-10-14
      • 2020-03-04
      • 1970-01-01
      • 2015-01-01
      • 2023-04-11
      • 2012-05-26
      • 2019-07-19
      • 1970-01-01
      相关资源
      最近更新 更多