All files / src/infra BrowserPage.ts

0% Statements 0/21
0% Branches 0/2
0% Functions 0/3
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                                                           
import playwright from "playwright-core";
import path from "path";
 
export class BrowserPage {
  readonly instance: playwright.Page;
  readonly screenshotDir: string;
  readonly screenshotSubDir: string;
  readonly screenshotPrefix: string;
 
  private screenshotNo = 0;
 
  constructor(
    page: playwright.Page,
    screenshotDir: string,
    screenshotSubDir: string,
    screenshotPrefix: string
  ) {
    this.instance = page;
    this.screenshotDir = screenshotDir;
    this.screenshotSubDir = screenshotSubDir;
    this.screenshotPrefix = screenshotPrefix;
  }
 
  async screenshot(name: string, subdir: string = ""): Promise<void> {
    const padded = String(this.screenshotNo + 1).padStart(2, "0"); // 0埋め
    const fileName = `${this.screenshotPrefix}${padded}-${name}`;
    const filePath = path.join(
      this.screenshotDir,
      this.screenshotSubDir,
      subdir,
      fileName
    );
    await this.instance.screenshot({
      path: filePath,
      fullPage: true,
    });
    this.screenshotNo++;
  }
 
  async screenshotForError(subdir: string = ""): Promise<void> {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, "0");
    const day = String(now.getDate()).padStart(2, "0");
    const hours = String(now.getHours()).padStart(2, "0");
    const minutes = String(now.getMinutes()).padStart(2, "0");
    const seconds = String(now.getSeconds()).padStart(2, "0");
 
    const fileName = `error-${year}${month}${day}-${hours}${minutes}${seconds}.png`;
 
    await this.screenshot(fileName, subdir);
  }
}