Skip to content

Commit

Permalink
ログインに失敗したときのテストも作成する
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuma-naka committed Jan 5, 2025
1 parent cb21196 commit 55e48be
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions e2e/login_then_logout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,53 @@ import dotenv from "dotenv";

dotenv.config({ path: ".env.local" });

test("テストユーザーでログインのテストをしてログアウトをする", async ({
page,
}) => {
const email =
test.describe("ユーザー認証のテスト", () => {
const validEmail =
process.env.EMAIL ??
(() => {
throw new Error("EMAIL is not defined");
})();
const password =
const validPassword =
process.env.PASSWORD ??
(() => {
throw new Error("PASSWORD is not defined");
})();

await page.goto("http://localhost:3000/");
await page.getByRole("link", { name: "ログイン" }).click();
await page.getByPlaceholder("[email protected]").click();
await page.getByPlaceholder("[email protected]").fill(email);
await page.getByLabel("Password").click();
await page.getByLabel("Password").fill(password);
await page.getByRole("button", { name: "ログイン" }).click();
test("有効なユーザー情報でログインし、ログアウトできる", async ({ page }) => {
await page.goto("http://localhost:3000/");
await page.getByRole("link", { name: "ログイン" }).click();

const logoutButton = page.getByRole("link", { name: "ログアウト" });
await expect(logoutButton).toBeVisible();
await page.getByPlaceholder("[email protected]").fill(validEmail);
await page.getByLabel("Password").fill(validPassword);
await page.getByRole("button", { name: "ログイン" }).click();

await page.getByRole("link", { name: "ログアウト" }).click();
await page.goto("http://localhost:3000/");
const logoutButton = page.getByRole("link", { name: "ログアウト" });
await expect(logoutButton).toBeVisible();

/** 現在リロードしないと画面が更新されずログイン、サインインが表示されない **/
await page.reload();
/** 現在リロードしないと画面が更新されずログイン、サインインが表示されない **/
await logoutButton.click();
await page.waitForTimeout(1500);

const loginButton = page.getByRole("link", { name: "ログイン" });
const signInButton = page.getByRole("link", { name: "ユーザー登録" });
await expect(loginButton).toBeVisible();
await expect(signInButton).toBeVisible();
await page.reload();

const loginButton = page.getByRole("link", { name: "ログイン" });
const signInButton = page.getByRole("link", { name: "ユーザー登録" });

await expect(loginButton).toBeVisible();
await expect(signInButton).toBeVisible();
});

test("無効なユーザー情報でログインに失敗する", async ({ page }) => {
await page.goto("http://localhost:3000/");
await page.getByRole("link", { name: "ログイン" }).click();

await page.getByPlaceholder("[email protected]").fill("[email protected]");
await page.getByLabel("Password").fill("invalidpassword");
await page.getByRole("button", { name: "ログイン" }).click();

await expect(page).toHaveURL("http://localhost:3000/error");

const loginButton = page.getByRole("button", { name: "ログイン" });
await expect(loginButton).toBeVisible();
});
});

0 comments on commit 55e48be

Please sign in to comment.