当前位置:操作系统 > 安卓/Android >>

Android Robotium自动化测试二

Robotium代码解析:
[java]  
/* 
 * 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>{  
  
  
<span style="white-space:pre">  </span>private Solo solo;  
  
  
<span style="white-space:pre">  </span>public NotePadTest() {  
<span style="white-space:pre">      </span>super(NotesList.class);  
  
  
<span style="white-space:pre">  </span>}  
<span style="white-space:pre">  </span>  
<span style="white-space:pre">  </span>@Override  
<span style="white-space:pre">  </span>public void setUp() throws Exception {  
<span style="white-space:pre">      </span>//setUp() is run before a test case is started.   
<span style="white-space:pre">      </span>//This is where the solo object is created.  
<span style="white-space:pre">      </span>solo = new Solo(getInstrumentation(), getActivity());  
<span style="white-space:pre">  </span>}  
<span style="white-space:pre">  </span>  
<span style="white-space:pre">  </span>@Override  
<span style="white-space:pre">  </span>public void tearDown() throws Exception {  
<span style="white-space:pre">      </span>//tearDown() is run after a test case has finished.   
<span style="white-space:pre">      </span>//finishOpenedActivities() will finish all the activities that have been opened during the test execution.  
<span style="white-space:pre">      </span>solo.finishOpenedActivities();  
<span style="white-space:pre">  </span>}  
  
  
  
  
<span style="white-space:pre">  </span>public void testAddNote() throws Exception {  
<span style="white-space:pre">      </span>solo.clickOnMenuItem("Add note");  
<span style="white-space:pre">      </span>//Assert that NoteEditor activity is opened  
<span style="white-space:pre">      </span>solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor");   
<span style="white-space:pre">      </span>//In text field 0, add Note 1  
<span style="white-space:pre">      </span>solo.enterText(0, "Note 1");  
<span style="white-space:pre">      </span>solo.goBack();   
<span style="white-space:pre">      </span>//Clicks on menu item  
<span style="white-space:pre">      </span>solo.clickOnMenuItem("Add note");  
<span style="white-space:pre">      </span>//In text field 0, add Note 2  
<span style="white-space:pre">      </span>solo.enterText(0, "Note 2");  
<span style="white-space:pre">      </span>//Go back to first activity named "NotesList"  
<span style="white-space:pre">      </span>solo.goBackToActivity("NotesList");   
<span style="white-space:pre">      </span>//Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/".  
<span style="white-space:pre">      </span>solo.takeScreenshot();  
<span style="white-space:pre">      </span>boolean expected = true;  
<span style="white-space:pre">      </span>boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2");  
<span style="white-space:pre">      </span>//Assert that Note 1 & Note 2 are found  
<span style="white-space:pre">      </span>assertEquals("Note 1 and/or Note 2 are not found", expected, actual);   
  
  
<span style="white-space:pre">  </span>}  
  
  
<span style="white-space:pre">  </span>public void testEditNote() throws Exception {  
<span style="white-space:pre">      </span>// Click on the second list line  
<span style="white-space:pre">      </span>solo.clickInList(2);   
<span style="white-space:pre">      </span>// Change orientation of activity  
<span style="white-space:pre">      </span>solo.setActivityOrientation(Solo.LANDSCAPE);  
<span style="white-space:pre">      </span>// Change title  
<span style="white-space:pre">      </span>solo.clickOnMenuItem("Edit title");  
<span style="white-space:pre">      </span>//In first text field (0), add test  
<span style="white-space:pre">      </span>solo.enterText(0, " test");    
<span style="white-space:pre">      </span>solo.goBack();  
<span style="white-space:pre">      </span>boolean expected = true;  
<span style="white-space:pre"&g
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,