LoginSignup
1
2

More than 5 years have passed since last update.

webdriver用のテストシナリオをJavaScript(nashorn)で書いてみる その2

Posted at

はじめに

 webdriver用のテストシナリオをJavascript(Nashorm)で書いてみるのつづきです。

アサーション

実際にテストシナリオを作るには アサーションが必要です。選択肢でいえば JavaのJunit,TestNGなどを使うことも可能なはずですね(言語が混じって解りにくくなるかもですが)

Junit

Junitをダウンロードしてきます。また、あわせてhamcrestも必要です
Junit4.12
hamcrest-core-1.3.jar

テストスクリプトを作成して動作確認。

load("nashorn:mozilla_compat.js");

importPackage(org.junit);
importPackage(org.hamcrest.core);

print("Start!");

Assert.assertThat("Test",Is.is("Test"));
Assert.assertThat("Test1",Is.is("Test"));

print("End!");

Javaで Junitを使用する場合 import static を使うので、クラス指定はしないと思いますが、nashornから呼び出すには指定しないとだめなようです(ほかに方法があるかも)

ちなみに

Junitを使用するに当たり hamcrestを別途 組み込んでいます。今回Junitは最新の 4.12を使用しているのですが、4.11からこれが取り除かれており、この手間が必要のようです( 4.10までは hamcrestが組み込まれていた junit-4.xx.jar と、組み込まれていない junit-dep-4.xx.jar というのがあったが 4.11から組み込まれないものに統一された模様)

組み合わせて

webdriverとJunit両方を組み合わせたもの

load("nashorn:mozilla_compat.js");

importPackage(org.junit);
importPackage(org.hamcrest.core);


print("Start!");
  var wd = JavaImporter(org.openqa.selenium.firefox);
  var driver = new wd.FirefoxDriver();

  driver.get("http://www.google.com");

  var element = driver.findElementsByName("q");
  element.get(0).sendKeys("selenium");
  element.get(0).submit();

  Assert.assertThat(driver.getTitle(), Is.is("Google"));

  driver.quit();

print("End!");

考察

JunitはJavaのテストツールですが、テストシナリオをJavaScriptで記述しているのにJavaのツールを使うのは、混乱しそうです。

が、JavaScript用のテストツールを使うとなると、どれもインストールに  npm を使用していて簡単に環境を作るところにたどりつけなさそうです。chai-webdriverとかが使えるとよさげなのですが、

アサーションは出来ましたが、実行に際してはテストランナーに相当するものもほしいところ
(Junitにある アノテーションなどはさすがに使えないですから)

また、実行することができましたが、実使用するには いわゆる エコシステム的なものがほしいですね。

あった

とおもったら Nashorn + Maven + .js 的な ものがあるそうです。

Nashorn Maven: Easy Library Classpath Setup

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