欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Appium创建一个Note的实例

程序员文章站 2022-07-05 21:38:27
近来通过Appium,Robotium等几个框架去了解移动平台自动化测试。Appium官方实例是使用ContactManager.apk,而Robotium使用的是SDK自带的Notepad.apk,为了方便比较,在了解Appium的同时把实例修改成跟Robotium一致的Notepad.apk并记录下其中一个Case如下:1.package majcit.com.AppiumDemo; 2. 3.import io.appium.java_client.AppiumDriver;...

近来通过AppiumRobotium等几个框架去了解移动平台自动化测试。Appium官方实例是使用ContactManager.apk,而Robotium使用的是SDK自带的Notepad.apk,为了方便比较,在了解Appium的同时把实例修改成跟Robotium一致的Notepad.apk并记录下其中一个Case如下:

1.	package majcit.com.AppiumDemo;  
2.	  
3.	import io.appium.java_client.AppiumDriver;  
4.	import io.appium.java_client.TouchAction;  
5.	  
6.	import java.io.File;  
7.	import java.net.URL;  
8.	import java.util.List;  
9.	  
10.	import org.junit.Test;  
11.	import org.junit.After;  
12.	import org.junit.Before;  
13.	import org.openqa.selenium.By;  
14.	import org.openqa.selenium.WebElement;  
15.	import org.openqa.selenium.remote.DesiredCapabilities;  
16.	import static org.hamcrest.Matchers.*;  
17.	import static org.hamcrest.MatcherAssert.assertThat;  
18.	  
19.	/** 
20.	 * Unit test for simple App. 
21.	 */  
22.	public class NoetPadTest {  
23.	    /** 
24.	     * Create the test case 
25.	     * 
26.	     * @param testName name of the test case 
27.	     */  
28.	    private AppiumDriver driver;  
29.	  
30.	    @Before  
31.	    public void setUp() throws Exception {  
32.	        // set up appium  
33.	        File classpathRoot = new File(System.getProperty("user.dir"));  
34.	        File appDir = new File(classpathRoot, "apps");  
35.	        File app = new File(appDir, "NotePad.apk");  
36.	        DesiredCapabilities capabilities = new DesiredCapabilities();  
37.	        capabilities.setCapability("deviceName","Android");  
38.	        //capabilities.setCapability("platformVersion", "4.2");  
39.	        capabilities.setCapability("platformName", "Android");  
40.	        //capabilities.setCapability("app", app.getAbsolutePath());  
41.	        capabilities.setCapability("appPackage", "com.example.android.notepad");  
42.	        capabilities.setCapability("appActivity", "com.example.android.notepad.NotesList");  
43.	        //capabilities.setCapability("appActivity", ".NotesList");  
44.	        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);  
45.	    }  
46.	  
47.	    @After  
48.	    public void tearDown() throws Exception {  
49.	        driver.quit();  
50.	    }  
51.	  
52.	    @Test  
53.	    public void addNoteCNTitle() throws InterruptedException{  
54.	        //Evoke the system menu option  
55.	        driver.sendKeyEvent(82);  
56.	  
57.	          
58.	        try {  
59.	            Thread.sleep(1000);  
60.	        }catch(Exception e) {  
61.	            System.out.println(e.getMessage());  
62.	        }  
63.	          
64.	        //Click on the "Add Note" menu entry  
65.	        WebElement el = driver.findElement(By.name("Add note"));  
66.	        el.click();  
67.	          
68.	        //Enter the note info and save it  
69.	        WebElement text = driver.findElementByClassName("android.widget.EditText");  
70.	        text.sendKeys("Note 1");  
71.	          
72.	        driver.sendKeyEvent(82);  
73.	        el = driver.findElement(By.name("Save"));  
74.	        el.click();  
75.	          
76.	        //Find out the new added note entry  
77.	        List <WebElement> entries = driver.findElements(By.className("android.widget.TextView"));  
78.	          
79.	        WebElement targetEntry = null;  
80.	        for(WebElement entry : entries) {  
81.	            if(entry.getText().equals("Note1")) {  
82.	                targetEntry = entry;  
83.	                break;  
84.	            }  
85.	        }  
86.	          
87.	        //Long press on the men entry to call out the context menu options  
88.	        TouchAction action = new TouchAction(driver);  
89.	        action.longPress(targetEntry);  
90.	        //action.moveTo(we,240,10);  
91.	        action.perform();  
92.	          
93.	        //Find out the menu entry of "Delete"  
94.	        WebElement optionListView = driver.findElement(By.className("android.widget.ListView"));  
95.	        List <WebElement> options = optionListView.findElements(By.className("android.widget.TextView"));  
96.	        WebElement delete = null;  
97.	        for (WebElement option:options) {  
98.	            if (option.getText().equals("Delete")) {  
99.	                delete = option;  
100.	                break;  
101.	                  
102.	            }  
103.	        }  
104.	          
105.	        assertThat(delete,notNullValue());  
106.	          
107.	        //Delete the menu entry  
108.	        delete.click();  
109.	          
110.	        try {  
111.	            Thread.sleep(1000);  
112.	        }catch(Exception e) {  
113.	            System.out.println(e.getMessage());  
114.	        }  
115.	          
116.	          
117.	          
118.	    }  
119.	      
120.	}  

 

本文地址:https://blog.csdn.net/qq_31344287/article/details/108732301