LoginSignup
3
2

More than 5 years have passed since last update.

Selenium + groovy の動作環境が作りたかった

Last updated at Posted at 2015-05-20

 はじめに

Seleniumの動作する一番簡単だろう環境をつくってみる

なるべく 手軽に作れる環境にしたいので インストール数は少なくしたい
また、groovyを使うが gebはまだ使わない前提

とはいいつつ、JavaSDKはインストール済み(含む環境変数設定)が前提

素材

実行スクリプトは seleniumBuilder で記録したものをエクスポートして作成してみる
groovyはJavaのソースを編集しなくても動くはず

Sauce Labs: Easy Automation with Selenium Builder
https://saucelabs.com/builder

テスト対象画面に遷移後 SeleniumBuilderを 「ツール」-「web開発」-「Launch Selenium Builder」から起動

01.png

「Selenium2」ボタンを押下後
ブラウザ上で 検索ボックスに 「Selenium」を入力

02.png

アサーションコマンドの入力は 「Record a verification」を押下し、ブラウザ上の対象を右クリック

03.png

「Stop recording」押下後、Fileメニューから 「export」を選択 

04.png

テストフレームワークを使用しないため 「Java」を選択し ファイル名「SeleniumBuilder.groovy」で保存

import java.util.concurrent.TimeUnit;
import java.util.Date;
import java.io.File;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.*;
import static org.openqa.selenium.OutputType.*;

public class SeleniumBuilder {
    public static void main(String[] args) throws Exception {
        FirefoxDriver wd;
        wd = new FirefoxDriver();
        wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        wd.get("http://www.yahoo.co.jp/");
        wd.findElement(By.id("srchtxt")).clear();
        wd.findElement(By.id("srchtxt")).sendKeys("selenium");
        wd.findElement(By.id("srchbtn")).click();
        wd.findElement(By.linkText("Selenium - Web Browser Automation")).click();
        wd.quit();
    }

    public static boolean isAlertPresent(FirefoxDriver wd) {
        try {
            wd.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }
}

かなりシンプルなソースが出てきた

groovyで実行

groovy実行時クラスパスの設定は 下記を参考にさせていただきました

Groovyのクラスパス(classpath)
http://npnl.hatenablog.jp/entry/20080827/1219852663

今もなおっていないのか不明なので 個人ディレクトリ配下に手動で保存した

c:> (個人ディレクトリ)\.groovy\lib

selenium関係のライブラリを保存

 公式 ダウンロードページ
http://www.seleniumhq.org/download/

2015/03/05  14:01           345,048 apache-mime4j-0.6.jar
2015/03/05  14:01           242,154 bsh-1.3.0.jar
2015/03/05  14:01           324,238 cglib-nodep-2.1_3.jar
2015/03/05  14:01           263,965 commons-codec-1.9.jar
2015/03/05  14:01           575,389 commons-collections-3.2.1.jar
2015/03/05  14:01            52,543 commons-exec-1.1.jar
2015/03/05  14:01           185,140 commons-io-2.4.jar
2015/03/05  14:01           299,994 commons-jxpath-1.3.jar
2015/03/05  14:01           412,739 commons-lang3-3.3.2.jar
2015/03/05  14:01            62,050 commons-logging-1.1.3.jar
2015/03/05  14:01           354,647 cssparser-0.9.14.jar
2015/03/05  14:01           210,856 gson-2.3.1.jar
2015/03/05  14:01         2,256,213 guava-18.0.jar
2015/03/05  14:01            45,024 hamcrest-core-1.3.jar
2015/03/05  14:01            53,070 hamcrest-library-1.3.jar
2015/03/05  14:01         1,452,628 htmlunit-2.15.jar
2015/03/05  14:01         1,026,253 htmlunit-core-js-2.15.jar
2015/03/05  14:01           592,008 httpclient-4.3.6.jar
2015/03/05  14:01           282,793 httpcore-4.3.3.jar
2015/03/05  14:01            37,272 httpmime-4.3.6.jar
2015/03/05  14:01           101,578 ini4j-0.5.2.jar
2014/12/13  17:09         1,282,424 ivy-2.4.0.jar
2015/03/05  14:01            56,533 jcommander-1.29.jar
2015/03/05  14:01           125,727 jetty-websocket-8.1.8.jar
2015/03/05  14:01         1,006,639 jna-3.4.0.jar
2015/03/05  14:01           913,435 jna-platform-3.4.0.jar
2015/03/05  14:01           245,039 junit-dep-4.11.jar
2015/03/05  14:01           125,150 nekohtml-1.9.21.jar
2015/03/05  14:01         1,106,629 netty-3.5.7.Final.jar
2015/03/05  14:01         4,063,471 operadriver-1.5.jar
2015/03/05  14:01            10,702 phantomjsdriver-1.2.1.jar
2015/03/05  14:01           450,284 protobuf-java-2.4.1.jar
2015/03/05  14:01            15,808 sac-1.3.jar
2015/03/05  14:01           613,315 selenium-java-2.45.0-srcs.jar
2015/03/05  14:01         3,898,959 selenium-java-2.45.0.jar
2015/05/13  16:29        35,269,077 selenium-server-standalone-2.45.0.jar
2015/03/05  14:01           278,281 serializer-2.7.1.jar
2015/03/05  14:01           835,600 testng-6.8.5.jar
2015/03/05  14:01         3,176,148 xalan-2.7.1.jar
2015/03/05  14:01         1,367,760 xercesImpl-2.11.0.jar
2015/03/05  14:01           220,536 xml-apis-1.4.01.jar

実行してみる

C:>groovy SeleniumBuilder.groovy
Caught: java.lang.LinkageError: loader constraint violation in interface itable initialization: when resolving method "org.apache.
xerces.dom.NodeImpl.getChildNodes()Lorg/w3c/dom/NodeList;" the class loader (instance of org/codehaus/groovy/tools/RootLoader) of
the current class, org/apache/xerces/dom/NodeImpl, and the class loader (instance of <bootloader>) for interface org/w3c/dom/Node
have different Class objects for the type org/w3c/dom/NodeList used in the signature
java.lang.LinkageError: loader constraint violation in interface itable initialization: when resolving method "org.apache.xerces.d
om.NodeImpl.getChildNodes()Lorg/w3c/dom/NodeList;" the class loader (instance of org/codehaus/groovy/tools/RootLoader) of the curr
ent class, org/apache/xerces/dom/NodeImpl, and the class loader (instance of <bootloader>) for interface org/w3c/dom/Node have dif
ferent Class objects for the type org/w3c/dom/NodeList used in the signature
        at org.apache.xerces.parsers.AbstractDOMParser.startDocument(Unknown Source)
        at org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown Source)
        at org.apache.xerces.impl.XMLVersionDetector.startDocumentParsing(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at org.openqa.selenium.firefox.internal.FileExtension.readIdFromInstallRdf(FileExtension.java:97)
        at org.openqa.selenium.firefox.internal.FileExtension.writeTo(FileExtension.java:60)
        at org.openqa.selenium.firefox.internal.ClasspathExtension.writeTo(ClasspathExtension.java:63)
        at org.openqa.selenium.firefox.FirefoxProfile.installExtensions(FirefoxProfile.java:424)
        at org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk(FirefoxProfile.java:402)
        at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:89)
        at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:246)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:114)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:193)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:182)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:95)
        at SeleniumBuilder.main(SeleniumBuilder.groovy:14)

ログからすると Firefoxの起動時にエラーが出ている

ライブラリの設定は誤っていないと思われるのだが。

Firefox実体が見つけられない?

System.setProperty("webdriver.firefox.bin","C:\\Program Files)\\Mozilla Firefox\\Firefox.exe");

これを入れてみたがログは変わらず

Eclipseで試してみる

ライブラリのバージョンの問題?

Eclipse 4.4(luna)に groovyプロジェクトを作って実行してみる

普通に動いた!!

おわりに

結局 Eclipsをインストールしてしまった・・・

groovy のインストール方法の違いによるバージョン差かとおもって 最新版の 2.4.6 から 2.3.11 へ落として eclipse環境と合わせてみたが 解決できず。

あまり簡単に環境ができた気がしない ここまでインストールするのであれば groovyじゃなくJavaで作りますよね orz

(groovyに関する情報を書かれている方の記事は もうばりばりに Javaを使いこなしていて いろいろなことがわかっている前提になっている気がする・・)

追記(2015.5.28)

スタックトレースを追いかけてみましたが ブラウザ起動時に テンポラリーディレクトリに firefoxのプロファイルを作成し webdriverエクステンションをコピーしてゆく流れなのですが、このテンポラリーディレクトリを監視しているとエクリプスでは テンポラリーディレクトリが作成されてゆくのですが コマンドラインで実行すると この過程が確認できませんでした。eclipseとコマンドラインでどこに差があるのだろうか???

3
2
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
3
2