0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TestCafeでE2Eテストを自動化

Last updated at Posted at 2020-09-12

概要

TestCafeにより、E2Eテストを自動化する。

手順

  1. TestCafeをインストール
// インストール
$ npm install -g testcafe

// インストール確認
$ testcafe -v
Using locally installed version of TestCafe.
1.9.2

2. テストファイル(test.ts)を作成。

test.ts
import "testcafe";
import {Selector} from "testcafe";

fixture(" [テストグループ名] ")
    .page("https:[テスト対象のURL] ");

test(" [テスト名] ", async (t) => {
    // 操作するHTMLの要素 (ここでは、クラス名とname属性で要素を指定)
    const emailInputArea = Selector(".form-control").withAttribute("name", "email");
    const passwordInputArea = Selector(".form-control").withAttribute("name", "password");
    const loginButton = Selector(".loginBtn").withAttribute("name", "commit");

    // 実行するテスト(ログインし、指定した要素に田中 創さんという名前の表示があることを検証)
    await t
      .typeText(emailInputArea, " [メールアドレス] ")
      .typeText(passwordInputArea, " [パスワード] ")
      .click(loginButton)
      .expect(Selector(".header__userName").innerText).eql('田中 創さん');
});

3. package.jsonを編集し、テスト実行時のブラウザと実行ファイル(2.)を指定。

package.json
"scripts": {
        (略)
        "testcafe": "testcafe chrome ./test.ts",
        (略)
    },

4. テスト実行

$ npm run testcafe
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?