LoginSignup
1
1

More than 3 years have passed since last update.

【Capybara】Docker環境で擬似的に「save_and_open_page」を使用する

Posted at

Dockerの開発環境へ移行した結果、Rspecでよく使っていたsave_and_open_pageに以下のエラーが発生するようになってしまいました。

Failure/Error: save_and_open_page

Launchy::CommandNotFoundError:
  Unable to find a browser command. If this is unexpected, Please rerun with environment variable LAUNCHY_DEBUG=true or th
e '-d' commandline option and file a bug at https://github.com/copiousfreetime/launchy/issues/new

htmlファイル自体は保存されるので、それを手動で開けば見れるのですが、毎回このエラーが出るのは都合が悪い…
ので、擬似的にsave_and_open_pageと同様の動きをする方法を考えてみました。

前提

save_and_open_pageで保存されるhtmlファイルがローカル環境であること。
docker-compose.ymlでローカルのディレクトリをマウントしていること)

docker-compose.yml
services:
  rails:
    build:
      context: .
      dockerfile: ./docker/rails/Dockerfile
    volumes:
      - .:/app

1.fswatchのインストール

ファイルを監視するためにfswatchローカル環境でインストールしてください。
Macの体で記載しますが、Windows、Linuxでも導入可能だそうです。

$ brew install fswatch

2.監視コマンドの実行

以下のコマンドを実行してください。

$ while true; do fswatch -e ".*" -i ".*\.html$" --event Created -1 <HTML保存先パス> | xargs open -a <ブラウザ>; done

-e:監視対象から除外
-i:監視対象のファイルを指定 ←-eのオプションとの併用が必要
--event:監視対象のイベントを指定、Createdでファイルが作成されたときのみコマンドを実行させる。
-1:1回のみ監視 ←whileの記述で繰り返し監視する

  • -1を省いたfswatch〜<ブラウザ>のみのコマンドだと、htmlファイルを何回も開く挙動となってしまったため、-1をつけ繰り返す形としました。
  • 終了するときはctrl + c
$ while true; do fswatch -e ".*" -i ".*\.html$" --event Created -1 ~/app/tmp/capybara/ | xargs open -a "Google Chrome"; done

3.save_pageの挿入

キャプチャを確認したい場所でsave_and_open_pageではなく、save_pageを挿入してください。
openしないので、エラーは発生しなくなります。

これでrspecをDocker環境上で実行しても、ローカル環境のブラウザが自動で開き、ページを確認できるようにできます。

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