LoginSignup
7
7

More than 5 years have passed since last update.

Selenium(Geb)でChromeの拡張機能を使用する

Posted at

FireMobileSimulator を例にしてますが他の拡張機能でも手順は同じです。

FireMobileSimulator for Google Chrome
https://chrome.google.com/webstore/detail/firemobilesimulator-for-g/mkihbloiacgiofaejgagokalpeflnmbe

まず、拡張機能をパッケージ化(*.crx)する

拡張機能がインストールされているフォルダを特定する

OSXの場合
~/Library/Application\ Support/Google/Chrome/Default/Extensions

Chromeのアドレスバーに下記を入力して拡張機能画面を表示

chrome://extensions/

画面右上の デベロッパーモード チェックをONにすると各拡張機能のIDが表示されるので、FireMobileSimulatorのID(mkihbloiacgiofaejgagokalpeflnmbe)を覚えておく

拡張機能のパッケージ化... ボタンを押下

拡張機能のルート ディレクトリで、FireMobileSimulatorがインストールされたディレクトリを選択し 拡張機能のパッケージ化 ボタンを押下

拡張機能のルート ディレクトリ: /Users/<ユーザ名>/Library/Application Support/Google/Chrome/Default/Extensions/mkihbloiacgiofaejgagokalpeflnmbe/<バージョン名>
秘密鍵ファイル(省略可能): 空欄

しばらくすると *.crx が作成される

続いて、拡張機能の設定ファイルを取得する

拡張機能で必要なオプション設定があれば済ませておく
(ex. FireMobileSimulatorの場合は端末登録やUIDの設定など)

拡張機能の設定は下記ファイルに保存される

~/Library/Application\ Support/Google/Chrome/Default/Local\ Storage/chrome-extension_mkihbloiacgiofaejgagokalpeflnmbe_0.localstorage

Gebでブラウザ起動時に拡張機能をロードする

さて実際にGebで拡張機能をロードする方法ですが、下記の手順で行います。

  1. 一時的に使用するChrome Profileフォルダを作成(システムのTempフォルダに作成)
  2. 作成したProfileフォルダにlocalstorageファイルをコピー
  3. ChromeOptionsのaddExtensionsに拡張機能をセット
  4. ChromeOptionsのaddArgumentsにProfileのパスをセット
  5. ChromeOptionsを指定してChromeDriverを生成

※あらかじめソースコードの下記ディレクトリに *.crx*.localstorage を格納しておくこと

/extension/chrome
 chrome-extension_mkihbloiacgiofaejgagokalpeflnmbe_0.localstorage
 firemobilesimulator.crx

GebConfig.groovyに記載する場合のサンプルは以下の通りです。

/src/test/resources/GebConfig.groovy
...省略...

environments {
  'mobile' {
    driver = {
      ChromeOptions options = new ChromeOptions()
      String extensionId = "mkihbloiacgiofaejgagokalpeflnmbe"
      String extensionName = "firemobilesimulator.crx"
      String extensionDir = System.getProperty("user.dir") + "/extension/chrome"

      // 一時ディレクトリにProfile用のフォルダを作成
      String profilePath = System.getProperty("java.io.tmpdir") + "/chrome-profile"
      String localstoragePath = profilePath + "/Default/Local Storage"
      new File(localstoragePath).mkdirs()
      println "ProfileDir: " + profilePath

      // コピー元のlocalstorageパス
      String localstrageName = "chrome-extension_" + extensionId + "_0.localstorage"
      Path sourcePath = Paths.get(extensionDir + "/" + localstrageName)

      // コピー先のlocalstorageパス
      Path targetPath = Paths.get(localstoragePath + "/" + localstrageName)

      // localstorageをProfileフォルダにコピー
      Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING)

      // 拡張機能とProfileディレクトリの設定
      File extension = new File(extensionDir + "/" + extensionName)
      options.addExtensions(extension)
      options.addArguments("user-data-dir=" + profilePath)

      // Driverを生成
      DesiredCapabilities capabilities = DesiredCapabilities.chrome()
      capabilities.setCapability(ChromeOptions.CAPABILITY, options)
      chromeDriver = new ChromeDriver(capabilities)
      BrowserInstance.instance.driver = chromeDriver
      return chromeDriver
    }
  }
}

...省略...
7
7
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
7
7