83575


WSL2のUbuntu 20.04 LTS にて以下の手順で実行したが、ブラウザが起動せずにエラーが表示される。

`npx playwright codegen wikipedia.org` を実行したら、ブラウザが起動しコードを生成できるようにしたい。

**再現手順:**

[code]

    npm i -D @playwright/test
    npx playwright install
    npx playwright codegen wikipedia.org
    
[/code]

**エラーメッセージ:**

[code]

    browserType.launch: Browser closed.
    ==================== Browser output: ====================
    <launching> /home/yusuke/.cache/ms-playwright/chromium-930007/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-yVdHID --remote-debugging-pipe --no-startup-window
    <launched> pid=4232
    [pid=4232][err] [4232:4232:1112/033124.605300:FATAL:ozone_platform_x11.cc(238)] Check failed: x11::Connection::Get()->Ready(). Missing X server or $DISPLAY
    [pid=4232][err] #0 0x55d2f7921f99 base::debug::CollectStackTrace()
    [pid=4232][err] #1 0x55d2f788c513 base::debug::StackTrace::StackTrace()
    [pid=4232][err] #2 0x55d2f789ff10 logging::LogMessage::~LogMessage()
    [pid=4232][err] #3 0x55d2f78a0a5e logging::LogMessage::~LogMessage()
    [pid=4232][err] #4 0x55d2f4b27a29 ui::(anonymous namespace)::OzonePlatformX11::InitializeUI()
    [pid=4232][err] #5 0x55d2f4ac5067 ui::OzonePlatform::InitializeForUI()
    [pid=4232][err] #6 0x55d2f8ff7507 aura::Env::Init()
    [pid=4232][err] #7 0x55d2f8ff749a aura::Env::CreateInstance()
    [pid=4232][err] #8 0x55d2f54c109f content::BrowserMainLoop::InitializeToolkit()
    [pid=4232][err] #9 0x55d2f54c1a4d content::BrowserMainRunnerImpl::Initialize()
    [pid=4232][err] #10 0x55d2f54bddfb content::BrowserMain()
    [pid=4232][err] #11 0x55d2f7429692 content::ContentMainRunnerImpl::RunBrowser()
    [pid=4232][err] #12 0x55d2f7429157 content::ContentMainRunnerImpl::Run()
    [pid=4232][err] #13 0x55d2f74269c5 content::RunContentProcess()
    [pid=4232][err] #14 0x55d2f742749e content::ContentMain()
    [pid=4232][err] #15 0x55d2f41e7226 ChromeMain
    [pid=4232][err] #16 0x7f41010f30b3 __libc_start_main
    [pid=4232][err] #17 0x55d2f41e702a _start
    [pid=4232][err] Crash keys:
    [pid=4232][err]   "io_scheduler_async_stack" = "0x55D2F52A3FE4 0x0"
    [pid=4232][err]   "variations" = "19ebe09a-4542122,23a898eb-fc93cf74,5f2c0f7c-3f4a17df,e4a357e9-3f4a17df,"
    [pid=4232][err]   "num-experiments" = "4"
    [pid=4232][err]   "switch-30" = "--enable-crashpad"
    [pid=4232][err]   "switch-29" = "--no-startup-window"
    [pid=4232][err]   "switch-28" = "--remote-debugging-pipe"
    [pid=4232][err]   "switch-27" = "--user-data-dir=/tmp/playwright_chromiumdev_profile-yVdHID"
    [pid=4232][err]   "switch-26" = "--no-sandbox"
    [pid=4232][err]   "switch-25" = "--no-service-autorun"
    [pid=4232][err]   "switch-24" = "--use-mock-keychain"
    [pid=4232][err]   "switch-23" = "--password-store=basic"
    [pid=4232][err]   "switch-22" = "--enable-automation"
    [pid=4232][err]   "switch-21" = "--no-first-run"
    [pid=4232][err]   "switch-20" = "--metrics-recording-only"
    [pid=4232][err]   "switch-19" = "--force-color-profile=srgb"
    [pid=4232][err]   "switch-18" = "--disable-sync"
    [pid=4232][err]   "switch-17" = "--disable-renderer-backgrounding"
    [pid=4232][err]   "switch-16" = "--disable-prompt-on-repost"
    [pid=4232][err]   "switch-15" = "--disable-popup-blocking"
    [pid=4232][err]   "switch-14" = "--disable-ipc-flooding-protection"
    [pid=4232][err]   "switch-13" = "--disable-hang-monitor"
    [pid=4232][err]   "switch-12" = "--allow-pre-commit-input"
    [pid=4232][err]   "switch-11" = "--disable-features=ImprovedCookieControls,LazyFrameLoading,Globa"
    [pid=4232][err]   "switch-10" = "--disable-extensions"
    [pid=4232][err]   "switch-9" = "--disable-dev-shm-usage"
    [pid=4232][err]   "switch-8" = "--disable-default-apps"
    [pid=4232][err]   "switch-7" = "--disable-component-extensions-with-background-pages"
    [pid=4232][err]   "switch-6" = "--disable-client-side-phishing-detection"
    [pid=4232][err]   "switch-5" = "--disable-breakpad"
    [pid=4232][err]   "switch-4" = "--disable-backgrounding-occluded-windows"
    [pid=4232][err]   "switch-3" = "--disable-background-timer-throttling"
    [pid=4232][err]   "switch-2" = "--enable-features=NetworkService,NetworkServiceInProcess"
    [pid=4232][err]   "switch-1" = "--disable-background-networking"
    [pid=4232][err]   "num-switches" = "30"
    [pid=4232][err]   "osarch" = "x86_64"
    [pid=4232][err]   "pid" = "4232"
    [pid=4232][err]   "ptype" = "browser"
    [pid=4232][err]
    [pid=4232][err] [1112/033124.650794:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
    [pid=4232][err] [1112/033124.650846:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
    [pid=4232][err] Received signal 6
    [pid=4232][err] #0 0x55d2f7921f99 base::debug::CollectStackTrace()
    [pid=4232][err] #1 0x55d2f788c513 base::debug::StackTrace::StackTrace()
    [pid=4232][err] #2 0x55d2f7921a71 base::debug::(anonymous namespace)::StackDumpSignalHandler()
    [pid=4232][err] #3 0x7f4101fc43c0 (/usr/lib/x86_64-linux-gnu/libpthread-2.31.so+0x153bf)
    [pid=4232][err] #4 0x7f410111218b gsignal
    [pid=4232][err] #5 0x7f41010f1859 abort
    [pid=4232][err] #6 0x55d2f7920d05 base::debug::BreakDebuggerAsyncSafe()
    [pid=4232][err] #7 0x55d2f78a033f logging::LogMessage::~LogMessage()
    [pid=4232][err] #8 0x55d2f78a0a5e logging::LogMessage::~LogMessage()
    [pid=4232][err] #9 0x55d2f4b27a29 ui::(anonymous namespace)::OzonePlatformX11::InitializeUI()
    [pid=4232][err] #10 0x55d2f4ac5067 ui::OzonePlatform::InitializeForUI()
    [pid=4232][err] #11 0x55d2f8ff7507 aura::Env::Init()
    [pid=4232][err] #12 0x55d2f8ff749a aura::Env::CreateInstance()
    [pid=4232][err] #13 0x55d2f54c109f content::BrowserMainLoop::InitializeToolkit()
    [pid=4232][err] #14 0x55d2f54c1a4d content::BrowserMainRunnerImpl::Initialize()
    [pid=4232][err] #15 0x55d2f54bddfb content::BrowserMain()
    [pid=4232][err] #16 0x55d2f7429692 content::ContentMainRunnerImpl::RunBrowser()
    [pid=4232][err] #17 0x55d2f7429157 content::ContentMainRunnerImpl::Run()
    [pid=4232][err] #18 0x55d2f74269c5 content::RunContentProcess()
    [pid=4232][err] #19 0x55d2f742749e content::ContentMain()
    [pid=4232][err] #20 0x55d2f41e7226 ChromeMain
    [pid=4232][err] #21 0x7f41010f30b3 __libc_start_main
    [pid=4232][err] #22 0x55d2f41e702a _start
    [pid=4232][err]   r8: 0000000000000000  r9: 00007fff5da957c0 r10: 0000000000000008 r11: 0000000000000246
    [pid=4232][err]  r12: 00000e62000c8b40 r13: 00007fff5da95a20 r14: 00000e62000c8b50 r15: aaaaaaaaaaaaaaaa
    [pid=4232][err]   di: 0000000000000002  si: 00007fff5da957c0  bp: 00007fff5da95a10  bx: 00007f40fff30e40
    [pid=4232][err]   dx: 0000000000000000  ax: 0000000000000000  cx: 00007f410111218b  sp: 00007fff5da957c0
    [pid=4232][err]   ip: 00007f410111218b efl: 0000000000000246 cgf: 002b000000000033 erf: 0000000000000000
    [pid=4232][err]  trp: 0000000000000000 msk: 0000000000000000 cr2: 0000000000000000
    [pid=4232][err] [end of stack trace]
    =========================== logs ===========================
    <launching> /home/yusuke/.cache/ms-playwright/chromium-930007/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-yVdHID --remote-debugging-pipe --no-startup-window
    <launched> pid=4232
    [pid=4232][err] [4232:4232:1112/033124.605300:FATAL:ozone_platform_x11.cc(238)] Check failed: x11::Connection::Get()->Ready(). Missing X server or $DISPLAY
    [pid=4232][err] #0 0x55d2f7921f99 base::debug::CollectStackTrace()
    [pid=4232][err] #1 0x55d2f788c513 base::debug::StackTrace::StackTrace()
    [pid=4232][err] #2 0x55d2f789ff10 logging::LogMessage::~LogMessage()
    [pid=4232][err] #3 0x55d2f78a0a5e logging::LogMessage::~LogMessage()
    [pid=4232][err] #4 0x55d2f4b27a29 ui::(anonymous namespace)::OzonePlatformX11::InitializeUI()
    [pid=4232][err] #5 0x55d2f4ac5067 ui::OzonePlatform::InitializeForUI()
    [pid=4232][err] #6 0x55d2f8ff7507 aura::Env::Init()
    [pid=4232][err] #7 0x55d2f8ff749a aura::Env::CreateInstance()
    [pid=4232][err] #8 0x55d2f54c109f content::BrowserMainLoop::InitializeToolkit()
    [pid=4232][err] #9 0x55d2f54c1a4d content::BrowserMainRunnerImpl::Initialize()
    [pid=4232][err] #10 0x55d2f54bddfb content::BrowserMain()
    [pid=4232][err] #11 0x55d2f7429692 content::ContentMainRunnerImpl::RunBrowser()
    [pid=4232][err] #12 0x55d2f7429157 content::ContentMainRunnerImpl::Run()
    [pid=4232][err] #13 0x55d2f74269c5 content::RunContentProcess()
    [pid=4232][err] #14 0x55d2f742749e content::ContentMain()
    [pid=4232][err] #15 0x55d2f41e7226 ChromeMain
    [pid=4232][err] #16 0x7f41010f30b3 __libc_start_main
    [pid=4232][err] #17 0x55d2f41e702a _start
    [pid=4232][err] Crash keys:
    [pid=4232][err]   "io_scheduler_async_stack" = "0x55D2F52A3FE4 0x0"
    [pid=4232][err]   "variations" = "19ebe09a-4542122,23a898eb-fc93cf74,5f2c0f7c-3f4a17df,e4a357e9-3f4a17df,"
    [pid=4232][err]   "num-experiments" = "4"
    [pid=4232][err]   "switch-30" = "--enable-crashpad"
    [pid=4232][err]   "switch-29" = "--no-startup-window"
    [pid=4232][err]   "switch-28" = "--remote-debugging-pipe"
    [pid=4232][err]   "switch-27" = "--user-data-dir=/tmp/playwright_chromiumdev_profile-yVdHID"
    [pid=4232][err]   "switch-26" = "--no-sandbox"
    [pid=4232][err]   "switch-25" = "--no-service-autorun"
    [pid=4232][err]   "switch-24" = "--use-mock-keychain"
    [pid=4232][err]   "switch-23" = "--password-store=basic"
    [pid=4232][err]   "switch-22" = "--enable-automation"
    [pid=4232][err]   "switch-21" = "--no-first-run"
    [pid=4232][err]   "switch-20" = "--metrics-recording-only"
    [pid=4232][err]   "switch-19" = "--force-color-profile=srgb"
    [pid=4232][err]   "switch-18" = "--disable-sync"
    [pid=4232][err]   "switch-17" = "--disable-renderer-backgrounding"
    [pid=4232][err]   "switch-16" = "--disable-prompt-on-repost"
    [pid=4232][err]   "switch-15" = "--disable-popup-blocking"
    [pid=4232][err]   "switch-14" = "--disable-ipc-flooding-protection"
    [pid=4232][err]   "switch-13" = "--disable-hang-monitor"
    [pid=4232][err]   "switch-12" = "--allow-pre-commit-input"
    [pid=4232][err]   "switch-11" = "--disable-features=ImprovedCookieControls,LazyFrameLoading,Globa"
    [pid=4232][err]   "switch-10" = "--disable-extensions"
    [pid=4232][err]   "switch-9" = "--disable-dev-shm-usage"
    [pid=4232][err]   "switch-8" = "--disable-default-apps"
    [pid=4232][err]   "switch-7" = "--disable-component-extensions-with-background-pages"
    [pid=4232][err]   "switch-6" = "--disable-client-side-phishing-detection"
    [pid=4232][err]   "switch-5" = "--disable-breakpad"
    [pid=4232][err]   "switch-4" = "--disable-backgrounding-occluded-windows"
    [pid=4232][err]   "switch-3" = "--disable-background-timer-throttling"
    [pid=4232][err]   "switch-2" = "--enable-features=NetworkService,NetworkServiceInProcess"
    [pid=4232][err]   "switch-1" = "--disable-background-networking"
    [pid=4232][err]   "num-switches" = "30"
    [pid=4232][err]   "osarch" = "x86_64"
    [pid=4232][err]   "pid" = "4232"
    [pid=4232][err]   "ptype" = "browser"
    [pid=4232][err]
    [pid=4232][err] [1112/033124.650794:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
    [pid=4232][err] [1112/033124.650846:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
    [pid=4232][err] Received signal 6
    [pid=4232][err] #0 0x55d2f7921f99 base::debug::CollectStackTrace()
    [pid=4232][err] #1 0x55d2f788c513 base::debug::StackTrace::StackTrace()
    [pid=4232][err] #2 0x55d2f7921a71 base::debug::(anonymous namespace)::StackDumpSignalHandler()
    [pid=4232][err] #3 0x7f4101fc43c0 (/usr/lib/x86_64-linux-gnu/libpthread-2.31.so+0x153bf)
    [pid=4232][err] #4 0x7f410111218b gsignal
    [pid=4232][err] #5 0x7f41010f1859 abort
    [pid=4232][err] #6 0x55d2f7920d05 base::debug::BreakDebuggerAsyncSafe()
    [pid=4232][err] #7 0x55d2f78a033f logging::LogMessage::~LogMessage()
    [pid=4232][err] #8 0x55d2f78a0a5e logging::LogMessage::~LogMessage()
    [pid=4232][err] #9 0x55d2f4b27a29 ui::(anonymous namespace)::OzonePlatformX11::InitializeUI()
    [pid=4232][err] #10 0x55d2f4ac5067 ui::OzonePlatform::InitializeForUI()
    [pid=4232][err] #11 0x55d2f8ff7507 aura::Env::Init()
    [pid=4232][err] #12 0x55d2f8ff749a aura::Env::CreateInstance()
    [pid=4232][err] #13 0x55d2f54c109f content::BrowserMainLoop::InitializeToolkit()
    [pid=4232][err] #14 0x55d2f54c1a4d content::BrowserMainRunnerImpl::Initialize()
    [pid=4232][err] #15 0x55d2f54bddfb content::BrowserMain()
    [pid=4232][err] #16 0x55d2f7429692 content::ContentMainRunnerImpl::RunBrowser()
    [pid=4232][err] #17 0x55d2f7429157 content::ContentMainRunnerImpl::Run()
    [pid=4232][err] #18 0x55d2f74269c5 content::RunContentProcess()
    [pid=4232][err] #19 0x55d2f742749e content::ContentMain()
    [pid=4232][err] #20 0x55d2f41e7226 ChromeMain
    [pid=4232][err] #21 0x7f41010f30b3 __libc_start_main
    [pid=4232][err] #22 0x55d2f41e702a _start
    [pid=4232][err]   r8: 0000000000000000  r9: 00007fff5da957c0 r10: 0000000000000008 r11: 0000000000000246
    [pid=4232][err]  r12: 00000e62000c8b40 r13: 00007fff5da95a20 r14: 00000e62000c8b50 r15: aaaaaaaaaaaaaaaa
    [pid=4232][err]   di: 0000000000000002  si: 00007fff5da957c0  bp: 00007fff5da95a10  bx: 00007f40fff30e40
    [pid=4232][err]   dx: 0000000000000000  ax: 0000000000000000  cx: 00007f410111218b  sp: 00007fff5da957c0
    [pid=4232][err]   ip: 00007f410111218b efl: 0000000000000246 cgf: 002b000000000033 erf: 0000000000000000
    [pid=4232][err]  trp: 0000000000000000 msk: 0000000000000000 cr2: 0000000000000000
    [pid=4232][err] [end of stack trace]
    ============================================================
        at launchContext (/home/yusuke/develop/playwrightStudy/node_modules/@playwright/test/node_modules/playwright-core/lib/cli/cli.js:298:37)
        at Command.listener [as _actionHandler] (/home/yusuke/develop/playwrightStudy/node_modules/commander/lib/command.js:488:17)
        at /home/yusuke/develop/playwrightStudy/node_modules/commander/lib/command.js:1227:65
        at Command._chainOrCall (/home/yusuke/develop/playwrightStudy/node_modules/commander/lib/command.js:1144:12)
        at Command._parseCommand (/home/yusuke/develop/playwrightStudy/node_modules/commander/lib/command.js:1227:27)
        at Command._dispatchSubcommand (/home/yusuke/develop/playwrightStudy/node_modules/commander/lib/command.js:1050:25)
        at Command._parseCommand (/home/yusuke/develop/playwrightStudy/node_modules/commander/lib/command.js:1193:19)
        at Command.parse (/home/yusuke/develop/playwrightStudy/node_modules/commander/lib/command.js:897:10)
        at Object.<anonymous> (/home/yusuke/develop/playwrightStudy/node_modules/@playwright/test/cli.js:17:18) {
      name: 'Error'
    }
    
[/code]


CC BY-SA 4.0
2021-11-11T18:37:07.713
83572
2022-11-24T03:10:38.603
2021-11-12T01:19:58.290
3060
36906
question
javascript
npx playwright codegen wikipedia.org を実行してもブラウザが開かない


更新:  
本文末で補足していたWSLgについて、Windows10でも利用可能になったようです。  
(Microsoft
Store版([参考記事](https://forest.watch.impress.co.jp/docs/news/1458044.html));
[Pre-requisites節](https://github.com/microsoft/wslg/blob/main/README.md#pre-
requisites)参照)

つまり、Microsoft Store版をインストールしたのであれば、ここに記載した手順でなくWSLgのREADMEを参照してください。

* * *

> Check failed: x11::Connection::Get()->Ready(). Missing X server or $DISPLAY

とありますので、X server の設定、 `DISPLAY` 環境変数の設定が必要と思われます。

  * [Can't use X-Server in WSL 2 #4106](https://github.com/microsoft/WSL/issues/4106#issuecomment-917564903)

に設定方法等が記載されています。

Windows側の初期設定:

  1. [VcXsrv](https://sourceforge.net/projects/vcxsrv/) をインストールします
  2. `XLauncher`(`xlaunch.exe`) を起動し、次の設定を行います: 
    1. "Multiple windows" をチェック
    2. "Start no client" をチェック
    3. "Native opengl" のチェックを外す、 **" Disable access control" をチェック**(重要)
    4. "Save configuration" で設定を保存
  3. "完了" を押すと上記の設定で起動します
  4. (`xlaunch.exe` のショートカットを作成して引数に `-ac -run <設定ファイル>` を設定しておくと、次回起動はこのショートカットから行えます([参考](https://sourceforge.net/p/vcxsrv/wiki/Using%20VcXsrv%20Windows%20X%20Server/)))

Ubuntu側の初期設定:

  1. `~/.bashrc` に次を追記: 
[code]    export DISPLAY=$(route.exe print | grep 0.0.0.0 | head -1 | awk
'{print $4}'):0.0

[/code]

  2. `source ~/.bashrc` で上の設定を読み込み

上記設定が完了すれば、`npx playwright codegen wikipedia.org` が実行できるようになります。

* * *

要件を満たしているのであれば(≒ Windows11 であれば) `VcXsrv` の代わりに
[`WSLg`](https://github.com/microsoft/wslg) も利用できそうです。


CC BY-SA 4.0
2021-11-12T02:33:36.627
83575
2022-11-24T03:10:38.603
2022-11-24T03:10:38.603
2808
2808
83572
answer