1
0

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 1 year has passed since last update.

テストが面倒なのでplaywrightを使ってみる

1
Last updated at Posted at 2025-05-19

準備

  • 適当なディレクトリを用意してそこで以下を実行
npm init playwright@latest

色々聞かれるが答えていくと良い感じにインストールが始まる(素敵!)
image.png

インストール後はこんな感じでサンプルプログラムも作ってくれている
image.png

動かしてみる

  • script.tsを作成
script.ts
// script.ts
import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch({ headless: false });
  const context = await browser.newContext();
  const page = await context.newPage();

  // 任意のURLを開く
  await page.goto('https://google.com');

  // 1秒待つ
  await new Promise((resolve) => setTimeout(resolve, 1000));

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

  • 実行
npx tsx script.ts
  • google開いたー!あとは色々命令書くだけ

プロキシ接続が必要な場合

script.ts

// script.ts
import { chromium } from 'playwright';

const URL = 'https://hogehoge';
const PROXY = {
  server: 'socks5://127.0.0.1:10080'  // SSHのSOCKSプロキシ設定(ここは各自の環境に合わせる)
};

(async () => {
  const browser = await chromium.launch({
    headless: false,
    proxy: PROXY
  });
  const context = await browser.newContext();
  const page = await context.newPage();

  // 任意のURLを開く
  await page.goto(URL);

  // 1秒待つ
  await new Promise((resolve) => setTimeout(resolve, 1000));

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


1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?