UIAutomator创建一个Note的实例

来源:互联网 发布:淘宝宝贝关联怎么弄 编辑:程序博客网 时间:2024/06/09 18:41

紧接之前的创建一个Note的Appium和Robotium的实例,这里给出实现同样功能的UIAutomator的实例如下:

[java] view plaincopy
  1. package majcit.com.UIAutomatorDemo;  
  2.   
  3. import com.android.uiautomator.core.UiDevice;  
  4. import com.android.uiautomator.core.UiObject;  
  5. import com.android.uiautomator.core.UiObjectNotFoundException;  
  6. import com.android.uiautomator.core.UiScrollable;  
  7. import com.android.uiautomator.core.UiSelector;  
  8. import com.android.uiautomator.testrunner.UiAutomatorTestCase;  
  9.   
  10. public class NotePadTest extends UiAutomatorTestCase {  
  11.       
  12.      public void testDemo() throws UiObjectNotFoundException {    
  13.             UiDevice device = getUiDevice();  
  14.             device.pressHome();    
  15.             // Start Notepad  
  16.             UiObject appNotes = new UiObject(new UiSelector().text("Notes"));   
  17.             appNotes.click();    
  18.             //Sleep 3 seconds till the app get ready  
  19.             try {    
  20.                 Thread.sleep(3000);    
  21.             } catch (InterruptedException e1) {    
  22.                 // TODO Auto-generated catch block    
  23.                 e1.printStackTrace();    
  24.             }    
  25.               
  26.             //Evoke the system menu option  
  27.             device.pressMenu();  
  28.             UiObject addNote = new UiObject(new UiSelector().text("Add note"));  
  29.             addNote.click();  
  30.               
  31.             //Add a new note  
  32.             UiObject noteContent = new UiObject(new UiSelector().className("android.widget.EditText"));  
  33.             noteContent.clearTextField();  
  34.             noteContent.setText("Note 1");  
  35.             device.pressMenu();  
  36.             UiObject save = new UiObject(new UiSelector().text("Save"));  
  37.             save.click();  
  38.               
  39.             //Find out the new added note entry  
  40.             UiScrollable noteList = new UiScrollable( new UiSelector().className("android.widget.ListView"));    
  41.             //UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true));   
  42.             UiObject note = null;  
  43.             if(noteList.exists()) {  
  44.                 note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1"true);    
  45.                 //note = noteList.getChildByText(new UiSelector().text("Note1"), "Note1", true);   
  46.             }  
  47.             else {  
  48.                 note = new UiObject(new UiSelector().text("Note1"));  
  49.             }  
  50.             //assertThat(note,notNullValue());  
  51.               
  52.             note.longClick();  
  53.               
  54.             UiObject delete = new UiObject(new UiSelector().text("Delete"));  
  55.             delete.click();  
  56.                 
  57.         }    
  58.   
  59. }  

0 0