>& STDOUT

主にソフトウェアに関する日々の標準出力+標準エラー出力

NativeDriverを使ってみた:サンプルテストコード読む編その1

サンプルで入ってるテストコードの中でいちばん簡単そうな TextValueTest から。builderとか置いといて肝心の部分だけ抜粋してます。

 public void testTextValue() {
   driver.startActivity("com.google.android.testing.nativedriver"
       + ".simplelayouts.TextValueActivity");

   WebElement textView = driver.findElement(By.id("TextView01"));
   assertEquals("Hello, Android NativeDriver!", textView.getText());

   WebElement textEditView = driver.findElement(By.id("EditText01"));
   textEditView.clear();
   textEditView.sendKeys("this is some input");
   assertEquals("this is some input", textEditView.getText());
 }

あ、ちなみに僕WebDriverどころかSeleniumもロクに使ったことのない人です。そういう読者さんだとしっくり来ます。このへんのウイザードの人はアドバイスください。

ではあたまから

まず、テスト対象のActivityを起動。

driver.startActivity("com.google.android.testing.nativedriver"
       + ".simplelayouts.TextValueActivity");

これはいいとして。

WebElement textView = driver.findElement(By.id("TextView01"));

これだ。WebElement?WebDriverの名残?とおもったらNativeDriverの発表資料(pdf)に書いてありました。

WebElement.findElement is universal - all UIs are trees

Web has DOM (elements) 
Windows has HWNDs 
Android has Views 

- can be viewed with HierarchyViewer tool Each UI element kind has a set of attributes that can be searched on.

わかります。わかりますけど「WebElement」なあ…。まあいいや飲み込もう。view部品へのハンドルを、driver.findElementで”has a set of attributes”な属性を使ってゲットしてる、と。

assertEquals("Hello, Android NativeDriver!", textView.getText());

そのままですね。とってきたハンドルのテキストの値が第一引数であることを確認。staticなテキストだと「はぁ」って感じですが、実際はけっこうあるテストだと思います。

WebElement textEditView = driver.findElement(By.id("EditText01"));
textEditView.clear();
textEditView.sendKeys("this is some input");
assertEquals("this is some input", textEditView.getText());

あとは一気に。本当に読みながらこれ書いてたので、ここであんまり面白くないテストケースであることに気づくなど。だいたい雰囲気つかめると思いますが、EditViewとってきて、クリアして、sendKeysで書きこんで、そのとおりの表示になっていることを確認。


基本はOK。

ちょっともどる

ただ読むだけだとさらっと流しちゃいましたけど、やっぱりキモは

driver.startActivity("com.google.android.testing.nativedriver"
       + ".simplelayouts.TextValueActivity");


ここですね。テストコード自体から任意のActivityを起動しているところ。SDK付属のInstrumentationTestCaseではあくまでテスト対象のActivityが主でテストケースはそれに依存する形になるのですが、NativeDriverではその制約がありません。import文からもテスト対象のsimplelayouts classが不要であることがわかると思います。

package com.google.android.testing.nativedriver;

import com.google.android.testing.nativedriver.client.AndroidNativeDriver;
import com.google.android.testing.nativedriver.client.AndroidNativeDriverBuilder;

import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;