groovy,geb(selenium),spockによる自動化テスト その3
Geb Navigator API
- gebを使えば、jqueryのselectorに似ている書き方で要素page要素を取得することができる。
script3.html
<div>div1 text</div> <div>div2 text</div> <div class="search1"> <form onsubmit="return bridge();"> <input type="text" placeholder="input keyword" class="input" id="keyword" name="keyword"> <br> <input type="button" value="search" class="submit" id="btnSearch" name="btnSearch" onclick="alert('clicked');"> </form> </div>
script3_navigator.groovy
/** * Geb Navigator API * * http://www.gebish.org/manual/current/#the-jquery-ish-navigator-api * * jquery selector * https://www.w3schools.com/jquery/trysel.asp * https://www.w3schools.com/jquery/jquery_selectors.asp * * */ import geb.Browser def keywords = 'javait.hatenablog.com' def testPage = new File('src/main\\java/html/script3.html') Browser.drive { go testPage.toURI().toString() // // search element by tag, class, id // $("div") // // Match the first "div" element on the page. // $("div", 0) // // Match all "div" elements with a title attribute value of "section". // $("div", title: "section") // // Match the first "div" element with a title attribute value of "section". // $("div", 0, title: "section") // // Match all "div" elements who have the class "main". // $("div.main") // // Match the first "div" element with the class "main". // $("div.main", 0) // search element by traversing // <div class="a"> // <div class="b"> // <p class="c"></p> // <p class="d"></p> // <p class="e"></p> // </div> // <div class="f"></div> // </div> // assert $("p.d").previous() == $("p.c") // assert $("p.e").prevAll() == $("p.c").add("p.d") // assert $("p.d").next() == $("p.e") // assert $("p.c").nextAll() == $("p.d").add("p.e") // assert $("p.d").parent() == $("div.b") // assert $("p.c").siblings() == $("p.d").add("p.e") // assert $("div.a").children() == $("div.b").add("div.f") // by tag println "divタグ数 : " + $("div").size() println "2個目divタグのtext : " + $("div", 1).text() // by id $('#keyword').value(keywords) $('#btnSearch').click() sleep 10 * 1000 }.quit()
groovy,geb(selenium),spockによる自動化テスト その2
eclipseでGroovy ScriptでGebを利用する
eclipse4.6 http://mergedoc.osdn.jp/
groovy eclipse https://github.com/groovy/groovy-eclipse/wiki
buildship Gradle integration 2.0
gradleプロジェクト生成
- build.gradleに依存関係追加
dependencies {
// for geb
compile “org.gebish:geb-core:1.1.1”
compile “org.seleniumhq.selenium:selenium-firefox-driver:3.1.0”
compile “org.seleniumhq.selenium:selenium-support:3.1.0”
} - scpirt1.groovy
/** * Geb sample * * http://www.gebish.org/manual/current/#the-jquery-ish-navigator-api * * jquery selector * https://www.w3schools.com/jquery/trysel.asp * https://www.w3schools.com/jquery/jquery_selectors.asp * * */ import geb.Browser /** * geb常用メソッド * * .value() elementのtext代入 * .text() elementのtext取得 * .click() elementのclick * waitFor 条件待ち、時間経過するとWaitTimeoutExceptionが発生する * */ def keyword = 'javait.hatenablog.com' Browser.drive { go 'http://yahoo.co.jp' // タイトルがYahooになるまで待つ waitFor { title.startsWith('Yahoo') } // keyword入力 $('#srchtxt').value(keyword) // ボタンクリック $($('#srchbtn')).click() // タイトルが検索結果で終わるまで待つ waitFor() { title.endsWith('検索') } // waitFor(1) { false } 1秒後timeout // waitFor { title.endsWith('設定した時間後timeoutが発生する') } // 出力 $('h3').each { println "* ${it.text()}" } }.quit()
groovy,geb(selenium),spockによる自動化テスト その1
環境
jdk,mvn,groovy,geb,webdriver
webdriverダウンロード
https://github.com/mozilla/geckodriver/releases環境変数設定 PATH、JAVA_HOME、M2_HOME
バージョン確認
E:\dev>mvn -version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T20:57:37+09:00)
Maven home: E:\dev\apache-maven-3.3.3
Java version: 1.8.0_40, vendor: Oracle Corporation
Java home: D:\pleiades-e4.4\java\8\jre
Default locale: ja_JP, platform encoding: MS932
OS name: “windows 7”, version: “6.1”, arch: “amd64”, family: “dos”
E:\dev>groovy -version
Groovy Version: 2.4.8 JVM: 1.8.0_40 Vendor: Oracle Corporation OS: Windows 7
Groovy Shellでgebを実行する
groovysh
ライブラリダウンロード
groovy:000> import groovy.grape.Grape
===> groovy.grape.Grape
groovy:000> Grape.grab(group: ‘org.gebish’, module: ‘geb-core’, version:‘1.1.1’)
===> null
groovy:000> Grape.grab(group: ‘org.seleniumhq.selenium’, module: ‘selenium-firefox-driver’, version: ‘3.1.0’)
===> null
環境変数
groovy:000> System.setProperty(“webdriver.gecko.driver”, “E:\dev\geckodriver.exe”)
===> null
groovy:000> browser = new geb.Browser()
===> geb.Browser@6ad11a56
ブラウザオープン
groovy:000> browser.go ‘http://google.com’
URL:google.comが開かれる。
検索キー入力&検索ボタンクリック
groovy:000>form = browser.$(‘form#tsf’)
groovy:000>form.size()
groovy:000>form.q = ‘http://javait.hatenablog.com/’
groovy:000>btnK = form.find(‘input[name=btnK]’)
groovy:000>btnK.click()
検索結果タイトルを表示
groovy:000>browser.$(‘h3’).each { println it.text() }
liguofeng29’s blog
2016年現在のJavaについて - arclamp
arclamp
よねのはてな
Challenge Java EE
A Memorandum
HTML5のform関連 - 新規属性 - liguofeng29’s blog
【Java】GETメソッドでHTTP通信をする汎用オブジェクトを自作してみた …
google-http-java-client 入門 - ひだまりソケットは壊れない
略・・・・・
Groovy ScriptでGebを利用する
- スクリプト
@Grab('org.gebish:geb-core:1.1.1') @Grab('org.seleniumhq.selenium:selenium-firefox-driver:3.1.0') import geb.Browser System.setProperty("webdriver.gecko.driver", "E:\\dev\\groovy_geb\\geckodriver.exe") def keywords = args.join(' ') Browser.drive { go 'http://google.com' $('form#tsf').with { q = keywords btnK().click() } // argsがなければ // geb.waiting.WaitTimeoutException: condition did not pass in 5.0 seconds waitFor { $('h3').size() > 0 } $('h3').each { println "* ${it.text()}" } }.quit()
- 実行
groovy script1.groovy javait.hatenablog.com
Get Properties from File.
package org.lee.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Iterator; import java.util.Properties; public class PropertyUtil { private Properties getProperties(String path) { Properties conf = new Properties(); InputStream inputStream = null; try { inputStream = new FileInputStream(new File(path)); conf.load(inputStream); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return conf; } public static void main(String[] args) { PropertyUtil util = new PropertyUtil(); Properties p = util.getProperties("ppp.conf"); // 方法1 Enumeration<?> en = p.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); System.out.println(key + "=" + p.get(key)); } // 方法2 Iterator<?> it = p.keySet().iterator(); while (it.hasNext()) { String key = (String)it.next(); System.out.println(key + "=" + p.get(key)); } } }
b=2 a=1 b=2 a=1
※うん・・順序は保持してくれないね。
junit.framework.TestSuiteを使って、AllTest.javaを実装する。
・AllTest.java
package org.lee.samle; import junit.framework.JUnit4TestAdapter; import junit.framework.Test; import junit.framework.TestSuite; public class AllTest { public static Test suite() { TestSuite suite = new TestSuite(); // TestCase継承した場合 suite.addTestSuite(AppTestWithTestCase.class); // TestCase継承してない場合 suite.addTest(new JUnit4TestAdapter(AppTestWithoutTestCase.class)); return suite; } }
・App.java(テスト対象)
package org.lee.samle; /** * Hello world! * */ public class App { public boolean isAdult(int age) { return age >= 20; } }
・AppTestWithoutTestCase.java(テストクラス)
package org.lee.samle; import org.junit.Test; import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.runner.RunWith; import org.seasar.framework.unit.S2Assert; /** * Unit test for simple App. */ @RunWith(JUnit4ClassRunner.class) public class AppTestWithoutTestCase { App app; @Test public void testIsAdult() { app = new App(); S2Assert.assertFalse(app.isAdult(19)); S2Assert.assertTrue(app.isAdult(20)); S2Assert.assertTrue(app.isAdult(21)); } }
・AppTestWithTestCase .java(テストクラス)
package org.lee.samle; import junit.framework.TestCase; import org.seasar.framework.unit.S2Assert; /** * Unit test for simple App. */ public class AppTestWithTestCase extends TestCase{ App app; public void testIsAdult() { app = new App(); S2Assert.assertFalse(app.isAdult(19)); S2Assert.assertTrue(app.isAdult(20)); S2Assert.assertTrue(app.isAdult(21)); } }
※AllTest.javaを実行することで対象のすべてのテストクラスが実行される。 ※AllTest.javaをEclEmmaなどでカバレッジを確認すればよい。