tanamonの稀に良く書く日記

KEEP CALM AND DRINK BEER

SeleniumRCを使ってFirefoxで画面キャプチャを撮る方法

SeleniumRCを使ってIEで画面キャプチャを撮る方法 - tanamonの日記の続き。
今回はFirefoxです。


Firefoxの場合は、SeleniumRC用のプロファイルを作成する必要があります。


最初に、ファイル名を指定して実行から、以下のコマンドを実行します。

firefox -profilemanager


するとプロファイルマネージャが起動するので、新しいプロファイルを作成を選択します。

  • プロファイルの名前: selenium
  • ユーザデータの保存先: C:\selenium

プロファイルの名前は何でもよいです。
保存先はデフォルトでも使えますが、パスがやたら長いので変更することをおすすめします。

  • デフォルトの保存先の例: C:\\Documents and Settings\\Administrator\\Application Data\\Mozilla\\Firefox\\Profiles\\tq5bdm4c.selenium


次に、作成したプロファイルを選択してFirefoxを起動を選択します。


IEの場合と同じくAdd-Onが必要になるので、Screengrab!というAdd-Onをインストールします。
https://addons.mozilla.org/en-US/firefox/addon/1146


インストールが終わったらブラウザ側の準備は完了するのでFirefoxを終了させます。


最後に、作成したプロファイルの場所を設定として追加します。

RemoteControlConfiguration config = server.getConfiguration();
config.setFirefoxProfileTemplate(new File("C:\\selenium"));

また、実行ブラウザがFirefoxになるため、ブラウザ指定も*firefoxに変更しておきます。

Selenium client = new DefaultSelenium("localhost", PORT, "*firefox", baseUrl);

これを実行すればIEの場合と同じようにキャプチャが撮れます。

SeleniumFirefoxTest.java

package example.test;

import java.io.File;

import junit.framework.TestCase;

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class SeleniumFirefoxTest extends TestCase {
	public static int PORT = 4444;
	static {
		// テスト対象サーバへの接続にProxyが必要な場合のみ設定
		System.setProperty("http.proxyHost", "proxy.tanamon.jp");
		System.setProperty("http.proxyPort", "8080");
	}

	private SeleniumServer server;

	public void setUp() throws Exception {
		SeleniumServer.setAvoidProxy(true);
		server = new SeleniumServer(false);
		RemoteControlConfiguration config = server.getConfiguration();
		config.setMultiWindow(true);
		config.setPort(PORT);
		config.setFirefoxProfileTemplate(new File("C:\\selenium"));
		server.start();
	}

	public void testFirefoxClient() {
		String baseUrl = "http://www.google.co.jp";
		Selenium client = new DefaultSelenium("localhost", PORT, "*firefox", baseUrl);
		client.start();

		// 検索画面で検索
		client.open(baseUrl + "/webhp?hl=ja");
		client.type("q", "selenium");
		client.click("btnG");
		client.waitForPageToLoad("30000");
		assertEquals("selenium - Google 検索", client.getTitle());

		// 画面キャプチャを撮る
		client.captureEntirePageScreenshot("C:\\capture2.png", "");

		client.stop();
	}

	public void tearDown() {
		server.stop();
	}
}