Selenium webdriver を Firefox から Chrome に乗り換えたときのメモ

## したい事
* 某システムにおけるデイリーの画面ポチポチ作業を自動化したい
* 以前は Selenium で Firefox のドライバを使って動かしていた
* ところが Selenium さんは最近の Firefox と仲が悪いようで、Firefox ４８以降だと動かない（アプリが起動されるものの、指定したページへアクセスされない）※参考記事参照
* Firefox のバージョンを下げるのもアレだったので、Chrome に乗り換えた

## TL;DR
以下の設定を追加して `bundle install`

```ruby:Gemfile
gem "selenium-webdriver"
gem "pkg-config", "~> 1.1.7" #追加
gem "chromedriver-helper" #追加
```
```~/.bundle/config
BUNDLE_BUILD__NOKOGIRI: "--use-system-libraries --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2"
```

あとは、driver 名を変えるだけ

```ruby:pochipochi.rb
-    @driver = Selenium::WebDriver.for :firefox
+    @driver = Selenium::WebDriver.for :chrome
```

## ハマった所

### とりあえず `bundle install` した結果

```
$ bundle install

Installing nokogiri 1.8.0 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/white_aspara/.rbenv/versions/2.2.4/bin/ruby -r ./siteconf20170608-23331-ak4o88.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
Using mini_portile version 2.2.0
checking for iconv.h... yes
checking for gzdopen() in -lz... yes
checking for iconv... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.4
with the following patches applied:
	- 0001-Fix-comparison-with-root-node-in-xmlXPathCmpNodes.patch
	- 0002-Fix-XPointer-paths-beginning-with-range-to.patch
	- 0003-Disallow-namespace-nodes-in-XPointer-ranges.patch

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.4.tar.gz into tmp/x86_64-apple-darwin15.2.0/ports/libxml2/2.9.4... OK
Running git apply with /Users/white_aspara24/src/pochipochi/vendor/bundle/ruby/2.2.0/gems/nokogiri-1.8.0/patches/libxml2/0001-Fix-comparison-with-root-node-in-xmlXPathCmpNodes.patch... OK
Running git apply with /Users/white_aspara24/src/pochipochi/vendor/bundle/ruby/2.2.0/gems/nokogiri-1.8.0/patches/libxml2/0002-Fix-XPointer-paths-beginning-with-range-to.patch... OK
Running git apply with /Users/white_aspara24/src/pochipochi/vendor/bundle/ruby/2.2.0/gems/nokogiri-1.8.0/patches/libxml2/0003-Disallow-namespace-nodes-in-XPointer-ranges.patch... OK
Running 'configure' for libxml2 2.9.4... OK
Running 'compile' for libxml2 2.9.4... ERROR, review '/Users/white_aspara24/src/pochipochi/vendor/bundle/ruby/2.2.0/gems/nokogiri-1.8.0/ext/nokogiri/tmp/x86_64-apple-darwin15.2.0/ports/libxml2/2.9.4/compile.log' to see what happened. Last lines are:
========================================================================
    unsigned short* in = (unsigned short*) inb;
                         ^~~~~~~~~~~~~~~~~~~~~
encoding.c:815:27: warning: cast from 'unsigned char *' to 'unsigned short *' increases required alignment from 1 to 2 [-Wcast-align]
    unsigned short* out = (unsigned short*) outb;
                          ^~~~~~~~~~~~~~~~~~~~~~
4 warnings generated.
  CC       error.lo
  CC       parserInternals.lo
  CC       parser.lo
  CC       tree.lo
  CC       hash.lo
  CC       list.lo
  CC       xmlIO.lo
xmlIO.c:1450:52: error: use of undeclared identifier 'LZMA_OK'
    ret =  (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1;
                                                   ^
1 error generated.
make[2]: *** [xmlIO.lo] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
========================================================================
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

(中略その２)

Gem files will remain installed in /Users/white_aspara24/src/pochipochi/vendor/bundle/ruby/2.2.0/gems/nokogiri-1.8.0 for inspection.
Results logged to /Users/white_aspara24/src/pochipochi/vendor/bundle/ruby/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/nokogiri-1.8.0/gem_make.out
Using selenium-webdriver 2.48.1
An error occurred while installing nokogiri (1.8.0), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.8.0'` succeeds before bundling.
```

`LZMA_OK` がないとか何とか怒られる... 

### `--use-system-libraries` を追加してみた結果

```
$ bundle config build.nokogiri --use-system-libraries
$ bundle install

Installing nokogiri 1.8.0 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/white_aspara24/.rbenv/versions/2.2.4/bin/ruby -r ./siteconf20170608-27485-15cyt5d.rb extconf.rb --use-system-libraries
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/Users/white_aspara24/.rbenv/versions/2.2.4/bin/$(RUBY_BASE_NAME)

（後略）
```

`libxml2` がない？？

```
$ brew list | grep libxml2
=> nothing
```
ふむ、確かにいない。

### `libxml2` 入れてみる
```
$ brew install libxml2
```
ところがどっこい

```
Installing nokogiri 1.8.0 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/white_aspara24/.rbenv/versions/2.2.4/bin/ruby -r ./siteconf20170608-35974-19am61q.rb extconf.rb --use-system-libraries
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
Using pkg-config gem version 1.1.9
checking for libxml-2.0... no
checking for libxslt... no
checking for libexslt... no
ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
（後略）
```

完全に空振り。 `libxml2` が相変わらず読めていない模様。
結論関係なかった。

### よくよく最初のエラーを見てみると

```
Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]
```

なるほど、 `bundle` のオプションでパスを指定できるのね。

### ちなみに

他の記事を見ていると

```
$ brew link --force libxml2
```

で解決！と書いてる人が何人かいたものの、 `brew install libxml2` した時のメッセージがこんなん↓で

```
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/libxml2/lib
    CPPFLAGS: -I/usr/local/opt/libxml2/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/libxml2/lib/pkgconfig
```

`link --force` はちょっと不安が残ったので不採用。 ※この辺まだ良くわかってない

## 参考にした記事
* [Firefox 48以降でselenium-webdriverが動作しない問題とその回避策](http://qiita.com/jnchito/items/ab23c925a6a78345d737)
* [OSX への nokogiri 1.6.8.rc3 の install でハマって解決したメモ](http://qiita.com/south37/items/63c938474bf35b30c988)
* [nokogiri -> ERROR: cannot discover where libxml2 is located on your system](https://gist.github.com/sobstel/f6a490d854a2e5a214c3f2cd9c366032)
* [Homebrew使い方まとめ](http://qiita.com/vintersnow/items/fca0be79cdc28bd2f5e4)