MyException - 我的异常网
当前位置:我的异常网» Android » Android Robotium自动化测试2

Android Robotium自动化测试2

www.MyException.Cn  网友分享于:2013-02-20  浏览:64次
Android Robotium自动化测试二

/*
 * This is an example test project created in Eclipse to test NotePad which is a sample 
 * project located in AndroidSDK/samples/android-11/NotePad
 * 
 * 
 * You can run these test cases either on the emulator or on device. Right click
 * the test project and select Run As --> Run As Android JUnit Test
 * 
 * @author Renas Reda, renas.reda@jayway.com
 * 
 */

package com.jayway.test;

import com.example.android.notepad.NotesList;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;


public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList>{

	private Solo solo;

	public NotePadTest() {
		super(NotesList.class);

	}
	
	@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 testAddNote() throws Exception {
		solo.clickOnMenuItem("Add note");
		//Assert that NoteEditor activity is opened
		solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); 
		//In text field 0, add Note 1
		solo.enterText(0, "Note 1");
		solo.goBack(); 
		//Clicks on menu item
		solo.clickOnMenuItem("Add note");
		//In text field 0, add Note 2
		solo.enterText(0, "Note 2");
		//Go back to first activity named "NotesList"
		solo.goBackToActivity("NotesList"); 
		//Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/".
		solo.takeScreenshot();
		boolean expected = true;
		boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2");
		//Assert that Note 1 & Note 2 are found
		assertEquals("Note 1 and/or Note 2 are not found", expected, actual); 

	}

	public void testEditNote() throws Exception {
		// Click on the second list line
		solo.clickInList(2); 
		// Change orientation of activity
		solo.setActivityOrientation(Solo.LANDSCAPE);
		// Change title
		solo.clickOnMenuItem("Edit title");
		//In first text field (0), add test
		solo.enterText(0, " test");  
		solo.goBack();
		boolean expected = true;
		
		// (Regexp) case insensitive
		boolean actual = solo.waitForText("(?i).*?note 1 test"); 
		//Assert that Note 1 test is found
		assertEquals("Note 1 test is not found", expected, actual); 

		solo.
	}

	public void testRemoveNote() throws Exception {
		//(Regexp) case insensitive/text that contains "test"
		solo.clickOnText("(?i).*?test.*");
		//Delete Note 1 test
		solo.clickOnMenuItem("Delete");
		//Note 1 test & Note 2 should not be found
		boolean expected = false;   
		boolean actual = solo.searchText("Note 1 test");
		//Assert that Note 1 test is not found
		assertEquals("Note 1 Test is found", expected, actual);  
		solo.clickLongOnText("Note 2");
		//Clicks on Delete in the context menu
		solo.clickOnText("Delete");  
		actual = solo.searchText("Note 2");
		//Assert that Note 2 is not found
		assertEquals("Note 2 is found", expected, actual);  
	}
}


Robotium代码解析:

1、在Robotium工程中需要引用用robotium-solo-3.6.jar包。

public class NotePadTest extendsActivityInstrumentationTestCase2<NotesList>

2、继承类ActivityInstrumentationTestCase2<NotesList>,并在 测试工程中定义一个solo对象,使用此对象做自动化操作。

private Solo solo;

3、在构造方法中指定被测程序中的主Activity,即被测项目源代码中查看AndroidManifest.xml文件中注册的activity的action属性为:

<activity android:name="NotesList"
           android:label="@string/title_notes_list">
            <intent-filter>
                <actionandroid:name="android.intent.action.MAIN" />
                <categoryandroid:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

在这里对应的是NotePad项目中的NotePad类。

public NotePadTest() {
       super(NotesList.class);
    }

4、setUp( )方法是在自动化操作前的做一下配置。

    

@Override
    public voidsetUp() throws Exception {
       solo = newSolo(getInstrumentation(), getActivity());
    }

5、tearDown()方法在测试用例已经执行完成时,关闭所有在测试过程中的活动事件。

@Override
    public voidtearDown() throws Exception {
       solo.finishOpenedActivities();
    }

6、常用操作包括点击、长按、滚动屏幕、设置等待时间等操作。

public void testAddNote() throws Exception {
       solo.clickOnMenuItem("Addnote");
       //Assert thatNoteEditor activity is opened
       solo.assertCurrentActivity("ExpectedNoteEditor activity", "NoteEditor"); 
       //In textfield 0, add Note 1
       solo.enterText(0,"Note 1");
       solo.goBack(); 
       //Clicks onmenu item
       solo.clickOnMenuItem("Addnote");
       //Assert thatNoteEditor activity is opened
       solo.assertCurrentActivity("ExpectedNoteEditor activity", "NoteEditor");
       //In textfield 0, add Note 1
       solo.enterText(0,"Note 1");
       solo.goBack();
       //Clicks onmenu item
       solo.clickOnMenuItem("Addnote");
       //In textfield 0, add Note 2
       solo.enterText(0,"Note 2");
       //Go back tofirst activity named "NotesList"
       solo.goBackToActivity("NotesList");
       //Takes ascreenshot and saves it in "/sdcard/Robotium-Screenshots/".
       solo.takeScreenshot();
       booleanexpected = true;
       booleanactual = solo.searchText("Note 1") &&solo.searchText("Note 2");
       //Assert thatNote 1 & Note 2 are found
       assertEquals("Note1 and/or Note 2 are not found", expected, actual);
 
    }


7、assertEquals( )验证实际结果和期望结果,如果实际结果和期望结果不一致,则该测试用例执行失败,并停止执行该用例。例子中给出的是三个用例组成的测试集,当其中一个测试用例执行失败,不影响其他测试用例执行。Junit测试结果中给出了详细的执行结果以及执行失败原因的提示。

 

以上是对Android Robotium自动化测试工具的基本解析,大家可以调试体验其他的一些功能。



文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有