Appium创建一个Note的实例

近来通过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.	}  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用 Appium 编写第一个程序时,你需要完成以下步骤: 1. 安装 Appium:首先,确保你的计算机上已经安装了 Node.js。然后,在命令行中运行以下命令来安装 Appium: ``` npm install -g appium ``` 2. 安装 Appium Server:你可以通过命令行运行 `appium` 命令来启动 Appium 服务器。 3. 创建一个测试项目:在你选择的编程语言中,创建一个新的项目。你可以使用 Java、Python、C# 等编程语言来编写 Appium 测试脚本。 4. 配置测试环境:在你的项目中,添加 Appium 的相关依赖库或包。这些依赖库或包通常是由相应编程语言的测试框架提供的。例如,对于 Java,你可以使用 TestNG 或 JUnit。 5. 编写测试脚本:在你的测试项目中,编写测试脚本来模拟用户与应用程序的交互。你可以使用 Appium 提供的 API 来定位元素、执行操作和断言结果。 以下是一个使用 Java 和 TestNG 的示例代码: ```java import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.*; import java.net.URL; public class AppiumTest { private AppiumDriver<WebElement> driver; @BeforeTest public void setup() throws Exception { // 设置 Appium 配置 DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "Android Emulator"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("platformVersion", "10.0"); capabilities.setCapability("app", "/path/to/your/app.apk"); // 初始化 AppiumDriver driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @Test public void testApp() { // 在此处编写你的测试步骤 // 例如:driver.findElement(By.id("elementId")).click(); } @AfterTest public void teardown() { // 关闭 AppiumDriver driver.quit(); } } ``` 请注意,上述代码仅供参考。你需要根据你的具体应用程序和测试需求进行适当的修改。 希望这个简单的示例能够帮助你开始使用 Appium 编写第一个程序!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值