前回の記事で、FileSystemAPI入りのChrome拡張機能を作成したが
それをSelenium2.0を使って起動できないか確かめた時のメモ。
■環境
- Windows7 x64
- Google Chromeをデフォルトパスへとインストール済、拡張機能は設定なし
- Visual Studio 2010 SP1
■追加で構築する環境
公式ドキュメント(SeleniumHQ - Selenium 2.0 and WebDriver) *1より、
To include Selenium in your project, simply download the latest selenium-dotnet zip file from https://code.google.com/p/selenium/downloads/list .
とのことなので、リンク先よりダウンロード・解凍しておく。
(2012/1/7現在、selenium-dotnet-2.16.0.zip が最新)
- Chrome Driver
Chrome用のドライバーは同梱されておらず、Chromiumプロジェクトがメンテ・サポートしているとのこと。
Chromium - Chrome Driver
この中のリンクよりWindows用のドライバーをダウンロード・解凍しておく。
(2012/1/7現在、chromedriver_win_18.0.995.0.zip が最新)
■プログラム作成の流れ
大きな流れは、以下を参照。
ledsunの日記 - WebアプリのテストだってC#で書きたい!Selenium2.0を使う。
実際の作業は、以下。
- 新しいプロジェクトで「コンソールアプリケーション」を選択・作成
- プロジェクトの参照設定にダウンロードしたSelenium2.0の全dllを追加
- ソースを書く
■ソース
ハマったポイントは、以下。
プロファイルを指定することで、ファイルのダウンロード先が決まるため調べてみたところ、
DesiredCapabilitiesオブジェクトに「chrome.switches」を追加すればOKだった。
- 環境変数「%USERPROFILE%」の指定不可
確認した限りでは使えなかったため、実際のパスを指定。
- chromedirver.exeのパス指定
Javaでは、以下のように書けば良いらしいが、C#では不可。
System.setProperty("webdriver.chrome.driver","/path/to/where/you/ve/put/chromedriver.exe");
フォーラム*2では、System.Environment.SetEnvironmentVariableを使えばとのコメントもあったが、
Just to clarify, System.Environment.SetEnvironmentVariable could be used in a similar way, but it tends not to be in my experience.
とのこと。
公式ドキュメントを探すと、ChromeDriverクラスのコンストラクタで
ChromeDriver(String, ICapabilities) - Initializes a new instance of the ChromeDriver class using the specified path to the directory containing ChromeDriver.exe and capabilities
を見つけたので、そちらを利用して解決。
- ソース(Program.cs)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FileDownloadByChrome { class Program { static void Main(string[] args) { const string extensionPath = @"<拡張機能のディレクトリパス>"; const string profilePath = @"<プロファイルデータのディレクトリパス>"; const string driverPath = @"<chromedriver.exeのディレクトリパス>"; OpenQA.Selenium.Remote.DesiredCapabilities capabilities = OpenQA.Selenium.Remote.DesiredCapabilities.Chrome(); System.Collections.ArrayList switches = new System.Collections.ArrayList(); switches.Add("--load-extension=" + extensionPath); switches.Add("--user-data-dir=" + profilePath); capabilities.SetCapability("chrome.switches", switches); OpenQA.Selenium.IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(driverPath, capabilities); driver.Url = "http://www.w3.org/"; } } }
以上で、SeleniumからChromeを起動し、Chromeの拡張機能からファイルをダウンロードすることができた。
ChromeDriverのKnown Issuesには、
There are a handful of known issues with ChromeDriver, listed below:
4.HTML 5 API not implemented
とのことだが、拡張機能でHTML5を使う分には関係ないのかも。
■備考
Selenium2.0を知っておくことで、スマホ向けのNative Driverも触れたくなった。
Google Japan Developer Relations Blog - UI テストツール「NativeDriver」の iOS 版をリリースしました
■参考
- 公式ドキュメント
selenium Project - Home
selenium Project - C# docs
ChromeDriver Members
DesiredCapabilities Members
WebDriver: Advanced Usage - Explicit and Implicit Waits
このようにすれば、Waitをかけられる模様。
- まとめ
Selenium Wiki - WebDriver
Peter Beverloo - List of Chromium Command Line Switches
- ブログ
Bear's Droppings - Selenium 2.0 の日本語入門記事があまり検索でヒットしなかったので僕が書いておく
Yamashiro0217の日記 - Selenium2.x で Ajax なWebアプリケーションをテストしよう 〜 Facebook の自動あいさつ返答機能を実装 〜
Halloween STuNs the HuMan - いい感じと評判の selenium2 を触ってみた
ごにょろぐ - WebDriverを統合したSelenium 2を使ってみる