|
Selenium RCのダウンロード
まずは、JUnitで動かすためにSelenium RCが必要なのでダウンロードします。
seleniumのメインページからダウンロードページに行って、「Selenium RC」のダウンロードボタンを押下します。

特に、インストールはいらないので適当なフォルダに解凍しておいてください。
Eclipseの準備
それでは、解凍したSelenium RCを使用してEclipseの設定を行います。
メニューの「ファイル→新規→Javaプロジェクト」を選択し、新しいプロジェクトを作成します。

プロジェクト名を入力したら、「次へ」を押下します。

Javaビルド設定が開いたら、ライブラリータブに移動し、「ライブラリーの追加」を押下します。

JUnitを選択し・・・

JUnit3を選択して「完了」ボタンを押下します。
では、続いてSeleniumの外部JARを追加します。
再びJavaビルド設定で「外部JARの追加」を押下し、

先ほど解凍したSelenium RC内の「selenium-java-client-driver.jar」を追加します。

これで、設定は終了です。
それでは、まずは通常のJUnitテストケースを作成します。
プロジェクトを右クリックし、「新規→JUnitテストケース」を選択。

クラス名とパッケージは任意で構いません。ここでは、exampleTestパッケージ、TestAcroクラスにしてあります。

テストコードの作成
次に、前回作成したソースをJavaのテストコードに変換します。
Selenium IDEの「オプション→フォーマット→java(JUnit)」を選択すれば変換できます。
相変わらず簡単ですね。
おそらく以下のようなソースができたはずです。
package com.example.tests;
import com.thoughtworks.selenium.*; import java.util.regex.Pattern;
public class Untitled extends SeleneseTestCase { public void setUp() throws Exception { setUp("http://change-this-to-the-site-you-are-testing/", "*chrome"); } public void testUntitled() throws Exception { selenium.open("/"); selenium.type("q", "Acrotech"); selenium.click("btnG"); selenium.waitForPageToLoad("30000"); assertTrue(selenium.isTextPresent("Acrotech")); selenium.click("//div[@id='res']/div/ol/li[1]/h3/a/em"); selenium.waitForPageToLoad("30000"); assertEquals("Acrotech", selenium.getTitle()); } }
これを先ほど作成したクラスへ、パッケージ名・クラス名・setUpメソッドを修正しコピぺします。
具体的にはこんな感じになりました。
package exampleTest;
import com.thoughtworks.selenium.*; import java.util.regex.Pattern;
public class TestAcro extends SeleneseTestCase { public void setUp() throws Exception { setUp("http://www.google.co.jp","*iexplore"); } public void testUntitled() throws Exception { selenium.open("/"); selenium.type("q", "Acrotech"); selenium.click("btnG"); selenium.waitForPageToLoad("30000"); assertTrue(selenium.isTextPresent("Acrotech")); selenium.click("//div[@id='res']/div/ol/li[1]/h3/a/em"); selenium.waitForPageToLoad("30000"); assertEquals("Acrotech", selenium.getTitle()); } }
Selenium serverの起動
やっと最後の作業です。Selenium実行のため、リモートのサーバを起動します。
Selenium RC内の以下のPathにSelenium serverがありますので、コマンドプロンプトで実行します。
以下のPathにSelenium-serverがあります。
\selenium-remote-control-1.0.1\selenium-server-1.0.1\selenium-server.jar
まずはコマンドプロンプトを開いてselenium-remote-controlのフォルダへ移動します。

実行コマンドは、こんな感じになります。
java -jar selenium-server.jar
以下のようなメッセージが出れば起動は成功です。

JUnitの実行
それでは、Eclipseに戻って作成したテストを実行します。
右クリックし
勝手にIEが動いて、いつものように緑バーがでればテスト成功です。

これでJUnit上でSeleniumを動かすことができました。
ここまでで、簡単なSeleniumの使用方法の説明は終了になります。
また、使用しながらの問題点などを発見した際には追記を行いたいと思います。
|