SeleniumRCのclient driver(java)を試してみる

とりあえずできました。

準備

  1. Selenium-RCのzipをDLして解凍
  2. 解凍してできたselenium-remote-control-0.8.1\javaフォルダを適当なところに移動してリネーム(javaってフォルダ名はわかりにくいので)
  3. Eclipseにインポート(既存プロジェクトをワークスペースへ)
  4. そうすると、selenium-java-client-driver-easyとselenium-java-client-driver-tests-easyというプロジェクトが作成される。
  5. selenium-java-client-driver-tests-easyプロジェクトはビルドパスのエラーが発生している。ビルドパスに「selenium-java-client-driver」プロジェクトが必要となっているため。そこでselenium-java-client-driver-easyのプロジェクト名を変更する。

試験実施

テストケース

  1. プロジェクトのjava\com\thoughtworks\seleniumフォルダ配下にテストケースがある。
  2. テストケースはJUnitのTestCaseを継承していて、Eclipse上で「右クリック→実行→JUnitテスト」で実行できる。
  3. GoogleTest.javaで実行してみる。

と、「selenium-server has start ?」というエラーが発生するので、selenium-serverを起動する。

  1. コマンドプロンプト起動
  2. selenium-java-client-driver-tests-easyプロジェクト直下にcd
  3. javaコマンドでselenium-serverを起動。必要に応じてプロキシ指定する。

java -Dhttp.proxyHost=proxy.XXXX.com -Dhttp.proxyPort=8080 -jar selenium-server.jar
改めてテスト実行。GoogleTest.javaがうまくいかないので社内のリモートサーバに対するテストケースを作成して実行してみる。
GrueTest.java

   public void setUp() throws Exception {
        String url = "http://xxxxx.co.jp";   ←リモートサーバのドメインを指定
       selenium = new DefaultSelenium("localhost", SeleniumServer.DEFAULT_PORT, "*iexplore", url); 
    ↑IEの起動を指定
       selenium.start();
    }

   public void testGoogleTestSearch() throws Throwable {
    selenium.open("http://xxxxx.co.jp/grue/");  ←ログインページのURL
    assertEquals("GRUE Portal -", selenium.getTitle());
    
    selenium.type("uname", "0xxxxx");
    selenium.type("pass", "xxxxxxx");
    selenium.click("//input[@value='ログイン']");
    selenium.waitForPageToLoad("5000");
    selenium.click("link=ここ");
    selenium.waitForPageToLoad("5000");
    
    assertEquals("GRUE Portal -", selenium.getTitle());
    assertTrue(selenium.isTextPresent("About GRUE"));
  }

↓テスト中のブラウザ画面

結果&考察

  • 上記のテストケースで無事試験成功。

  • でも、ブラウザ(TestRunner)の動きが早すぎて何やってるかよく見えない。
  • 動きが早いのは-htmlSuite指定で起動した場合も同じだけど、その場合はテスト結果レポートが作成される。こっちの場合は結果ファイルは残らない。
  • 試験結果はEclipseJUnitビューで見ることになる。エラーの場合、検証NGになった行を表示&ジャンプはできる。
  • 検証は、JUnitのassertEqualsやassertTrueを使用する。したがってSelence(HTML)のverifyコマンドのように検証NGでもテスト続行とか(多分)できない。
  • テストケースを継承させたりすることで、定型処理を書かなくて済むとかのメリットはありそう。でもコピペした方が早いかも?
  • このjava(JUnit)っぽい書式は、第三者的にはわかりにくそう。HTMLはテーブルで構造が決まってるため分かり易い。

GoogleTest.javaのエラー状況(一応メモ)

Firefox(デフォルト):ブラウザが一瞬現れてすぐ消える。EclipseJUnitビューを見てみると、GoogleTest.javaのブラウザオープンするところselenium.open("http://www.google.com");でNGになって終了している模様。↓JUnitのトレースコピー

com.thoughtworks.selenium.SeleniumException: Failure
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:73)
at com.thoughtworks.selenium.DefaultSelenium.open(DefaultSelenium.java:138)
at com.thoughtworks.selenium.GoogleTest.testGoogleTestSearch(GoogleTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

FirefoxIEに変えて実行すると結果がかわった。ちらっとGoogleのHPが見える。IEの方がマシっぽい。JUnitビューのトレース↓

com.thoughtworks.selenium.SeleniumException: 書き込みできません。
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:73)
at com.thoughtworks.selenium.DefaultSelenium.open(DefaultSelenium.java:138)
at com.thoughtworks.selenium.GoogleTest.testGoogleTestSearch(GoogleTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)