Robotium4.xでWebViewのテストをする。 #android

Robotium4.xでWebViewのテストをする。 #android

更新: 2013年7月1日

■ 背景

Robotium
Androidの自動テストフレームワーク

詳細

以前RobotiumでWebViewの無理やりな記事を書いたのですが、
Robotium4.x系でWebViewのテストが可能に!
ということで、とりあえず動かしてみるところまでやってみました。

project

あとで・・・;

■ コード

アプリ

package net.muzigen.webview.webviewexample;

import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.webkit.WebView; import android.webkit.WebViewClient;

public class MainActivity extends Activity {

@Override protected void onCreate(Bundle savedInstanceState) { WebView webView; super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webView1); webView.setWebViewClient(new WebViewClient());

webView.loadUrl(“http://google.co.jp”); }

@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }

}

テストする側

package net.muzigen.webview.webviewexample.test;

import net.muzigen.webview.webviewexample.MainActivity; import android.test.ActivityInstrumentationTestCase2;

import com.jayway.android.robotium.solo.By; import com.jayway.android.robotium.solo.Solo;

public class WebViewExampleTest extends ActivityInstrumentationTestCase2>MainActivity<{

private Solo solo;

public WebViewExampleTest(){ super(MainActivity.class); }

public WebViewExampleTest(Class>MainActivity< activityClass) { super(activityClass); }

@Override public void setUp() throws Exception { //setUp() is run before a test case is started. //This is where the solo object is created. solo = new Solo(getInstrumentation(), getActivity()); }

@Override public void tearDown() throws Exception { //tearDown() is run after a test case has finished. //finishOpenedActivities() will finish all the activities that have been opened during the test execution. solo.finishOpenedActivities(); }

public void testWebView(){ solo.waitForWebElement(By.id(“lst-ib”)); solo.enterTextInWebElement(By.id(“lst-ib”),“ラーメン”); solo.sleep(5000); solo.waitForWebElement(By.id(“tsbb”)); solo.clickOnWebElement(By.id(“tsbb”)); solo.sleep(50000); }

}

■ 解説

WebViewでGoogleを表示するだけのアプリと
そのアプリでラーメンを検索するテストです。

  • solo.waitForWebElement
  • solo.enterTextInWebElement
  • solo.clickOnWebElement

指定要素が表示されるのを待つ
指定要素にテキストを入力する
指定要素をクリックする。
といったWebView操作が追加されています。要素の指定には「By」classを使用し
text,id,xpath等で指定することが可能です。

■ 結論

RobotiumでWebViewの操作が可能になりましたのでハイブリッドなアプリとか
のシナリオテスト等に使ってみるといいんじゃないでしょうか。
自分もとりあえず、詳細なテストに使ってみたいと思います。

最新記事をフォロー

RSS Feed