プレイライトと冗談に基づく自動テスト

長い間、Seleniumはテスト自動化の主要なツールでした。ただし、サイプレス、パペティア、プレイライトなど、現在市場に出回っている適切な代替品がいくつかありますこの記事で検討するプレイライト。



Playwrightは、さまざまなブラウザ(Chromium、Firefox、およびWebKit)用の単一のAPIを備えたノードjsテスト自動化ライブラリです。マイクロソフトによって開発されました。私の意見では、Playwrightの主な利点は、ブラウザーとの緊密な統合と、Seleniumがアクセスできないレベルでブラウザーと対話できることです。



オープンソース製品のKanboardは、テストオブジェクトとして展開されます



テストには、ノードjs、playwright、jest、jest-playwright-preset、およびjest-html-reportersを使用します。Playwrightを使用してブラウザを操作します。Jestをテストランナーとして使用します。HTMLレポートを生成するには、Jest-html-reportersが必要です。



最初のステップは、ノードプロジェクトを作成し、必要なすべての依存関係をインストールすることです。



npm init

npm i -D playwright

npm install --save-dev jest

npm install -D jest-playwright-preset

npm install jest-html-reporters --save-dev



これらのコマンドを実行した後、必要な依存関係を持つpackage.jsonとnode_modulesを取得します。レポートとテストを含むフォルダーを設定するために、jestのpackage.jsonに変更を加えます。



{
  "name": "kb-playwright-tests",
  "version": "1.0.0",
  "description": "An automation test framework which is based on playwright.",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "jest": {
    "testMatch": [
      "**/tests/**/*.[jt]s?(x)",
      "**/?(*.)+(spec|test).[jt]s?(x)"
    ],
    "reporters": [
      "default",
      "jest-html-reporters"
    ]
  },
  "devDependencies": {
    "jest": "^26.6.3",
    "jest-html-reporters": "^2.1.0",
    "jest-playwright-preset": "^1.4.0",
    "playwright": "^1.6.1"
  }
}


次のステップは、ページオブジェクトを作成することです。



const { DashboardPage } = require("./DashboardPage");
var config = require('../config');

class SignInPage {
  constructor(page) {
    this.page = page;
  }

  async openSignInPage() {
    await this.page.goto(config.web.url);
  }

  async signInAs(user) {
    await this.page.fill("css=#form-username", user.username);
    await this.page.fill("css=#form-password", user.password);
    await this.page.click("css=button[type='submit']");
    return new DashboardPage(this.page);
  }
}
module.exports = { SignInPage };


 class DashboardPage {
  constructor(page) {
    this.page = page;
  }
}
module.exports = { DashboardPage };


テストは次のようになります。



const { chromium } = require("playwright");
const { SignInPage } = require("../pageobjectmodels/SignInPage");
const { roles } = require("../enums/roles");
const assert = require("assert");
var config = require("../config");
let browser;
let page;

beforeAll(async () => {
  console.log("headless : " + config.web.headless);
  console.log("sloMo : " + config.web.sloMo);
  browser = await chromium.launch({
    headless: config.web.headless == "true",
    slowMo: parseInt(config.web.sloMo, 10),
  });
});
afterAll(async () => {
  await browser.close();
});
beforeEach(async () => {
  page = await browser.newPage();
  if (config.web.networkSubscription) {
    page.on("request", (request) =>
      console.log(">>", request.method(), request.url())
    );
    page.on("response", (response) =>
      console.log("<<", response.status(), response.url())
    );
  }
});
afterEach(async () => {
  await page.close();
});

test("An admin is able to see a dashboard", async () => {
  const signInPage = new SignInPage(page);
  await signInPage.openSignInPage();
  const dashboardPage = await signInPage.signInAs(roles.ADMIN);
  const dashboard = await dashboardPage.page.$("#dashboard");
  assert(dashboard);
});


このラインでbrowser = await chromium.launch({headless: config.web.headless == "true",slowMo: parseInt(config.web.sloMo, 10),});は、ヘッドレスモードと遅延を構成できます。



コードブロックをbeforeEach使用すると、次のようなネットワークエントリを設定できます。



>> GET http://localhost/kanboard/
<< 302 http://localhost/kanboard/
>> GET http://localhost/kanboard/?controller=AuthController&action=login
<< 200 http://localhost/kanboard/?controller=AuthController&action=login
>> GET http://localhost/kanboard/assets/css/vendor.min.css?1576454976
>> GET http://localhost/kanboard/assets/css/app.min.css?1576454976
>> GET http://localhost/kanboard/assets/js/vendor.min.js?1576454976
....


これらのパラメータを制御できるようにするには、config.jsを追加します



var config = {};
config.web = {};

config.web.url = process.env.URL || "http://localhost/kanboard/";
config.web.headless = process.env.HEADLESS || false;
config.web.sloMo = process.env.SLOMO || 50;
config.web.networkSubscription = process.env.NETWORK;

module.exports = config;


これで、次のコマンドを使用してテストを実行できます。



npm run test デフォルト値でテスト実行(ヘッドレスfalse、sloMo 50、ネットワークオフ)



NETWORK = 'on' npm run test 値を使用したテスト実行Headlessfalse、sloMo 50、networking on



HEADLESS = 'true' npm run test 値を使用してテストを実行ヘッドレスtrue、sloMo 50、ネットワーキングオフ



テストの実行後、レポートが生成されます-jest_html_reporters.html



画像



ご清聴ありがとうございました。この記事がお役に立てば幸いです。




All Articles