Selenium が ARM CPU だと Selenium::WebDriver::Error::SessionNotCreatedError が出る問題
Rails の rspec によるE2Eテスト(system spec)で
Selenium を使うことがあると思うのですが、
selenium/standalone-chrome を使うと、以下のようなエラーが出て system spec が落ちてしまいます。
Selenium::WebDriver::Error::SessionNotCreatedError: Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: crashed. (chrome not reachable) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Host info: host: '7c17dddfa7ee', ip: '172.19.0.3' Build info: version: '4.10.0', revision: 'c14d967899' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.19' Driver info: driver.version: unknown Build info: version: '4.10.0', revision: 'c14d967899' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.19' Driver info: driver.version: unknown
Selenium 4 でなく Selenium 3 の場合は、
Selenium::WebDriver::Error::SessionNotCreatedError
でなく
Selenium::WebDriver::Error::UnknownError
なのでエラー理由がいっそうわかりにくいです。
Selenium::WebDriver::Error::UnknownError: unknown error: Chrome failed to start: crashed. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53' System info: host: '48673dc69a4e', ip: '172.19.0.3', os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '1.8.0_292' Driver info: driver.version: unknown remote stacktrace: #0 0x004000a2a8f3 <unknown> #1 0x00400050fba8 <unknown> #2 0x004000532e33 <unknown> #3 0x00400052e9ef <unknown>
この問題は、記事タイトルにあるように Mac M1 などの ARM CPU で起こります。
解決方法
https://github.com/SeleniumHQ/docker-selenium の README に記載されています。
For experimental docker container images, which run on platforms such as the Mac M1 or Raspberry Pi, see the community driven repository hosted at seleniumhq-community/docker-seleniarm. These images are built for three separate architectures: linux/arm64 (aarch64), linux/arm/v7 (armhf), and linux/amd64.
( https://github.com/SeleniumHQ/docker-selenium#experimental-mult-arch-aarch64armhfamd64-images )
そうです、selenium(セレニウム)ならぬ seleniarm (セレニアーム)というのが
コミュニティ主導でメンテナンスされているのです。
standalone-chrome でなく standalone-chromium であること、
厳密には公式でないことといった気になることはありますが、
現時点では、ARM CPU で動かすためには seleniarm を使わざるを得なさそうです。
開発者全員が ARM CPU を使っていれば困りませんが、混在するとなかなか難しいですね。
仮に揃っていても、本番環境やCI環境が Intel チップなのに
開発環境だけ ARM CPU という場合も、開発環境として理想的とは言えません。
Mac M1, M2 などのチップが普及するとともに、
開発環境をクラウドに置くことがより求められるようになったと感じますね。