appium 初學

1.下載Appium for windows,現在是0.12.3版本

https://bitbucket.org/appium/appium.app/downloads/

解壓後如下圖

雙擊Appium.exe就能啓動Appium界面

點擊Launch開啓服務

2. 下載Android SDK

https://developer.android.com/sdk/index.html

解壓後

3. 配置系統環境變量

ANDROID_HOME: C:\adt-bundle-windows-x86_64-20131030\sdk

Path添加: %ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools

4. 啓動AVD,耗資源啊,這時候我T400的CPU已經100%了

5. 編寫Test,使用ADT安裝好Maven插件,創建一個Maven項目,添加一個文件夾apps用來存放被測的app,這裏測試的是ContactManager.apk

pom.xml添加如下依賴

複製代碼
 1   <dependencies>
 2     <dependency>
 3             <groupId>junit</groupId>
 4             <artifactId>junit</artifactId>
 5             <version>4.11</version>
 6             <scope>test</scope>
 7         </dependency>
 8         <dependency>
 9             <groupId>org.seleniumhq.selenium</groupId>
10             <artifactId>selenium-java</artifactId>
11             <version>LATEST</version>
12             <scope>test</scope>
13         </dependency>
14   </dependencies>
複製代碼

編寫AndroidContactsTest

複製代碼
 1 package com.guowen.appiumdemo;
 2 
 3 import org.junit.After;
 4 import org.junit.Before;
 5 import org.junit.Test;
 6 import org.openqa.selenium.*;
 7 import org.openqa.selenium.interactions.HasTouchScreen;
 8 import org.openqa.selenium.interactions.TouchScreen;
 9 import org.openqa.selenium.remote.CapabilityType;
10 import org.openqa.selenium.remote.DesiredCapabilities;
11 import org.openqa.selenium.remote.RemoteTouchScreen;
12 import org.openqa.selenium.remote.RemoteWebDriver;
13 import java.io.File;
14 import java.net.URL;
15 import java.util.List;
16 
17 public class AndroidContactsTest {
18     private WebDriver driver;
19 
20     @Before
21     public void setUp() throws Exception {
22         // set up appium
23         File classpathRoot = new File(System.getProperty("user.dir"));
24         File appDir = new File(classpathRoot, "apps/ContactManager");
25         File app = new File(appDir, "ContactManager.apk");
26         DesiredCapabilities capabilities = new DesiredCapabilities();
27         capabilities.setCapability("device","Android");
28         capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
29         capabilities.setCapability(CapabilityType.VERSION, "4.4");
30         capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
31         capabilities.setCapability("app", app.getAbsolutePath());
32         capabilities.setCapability("app-package", "com.example.android.contactmanager");
33         capabilities.setCapability("app-activity", ".ContactManager");
34         driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
35     }
36 
37     @After
38     public void tearDown() throws Exception {
39         driver.quit();
40     }
41 
42     @Test
43     public void addContact(){
44         WebElement el = driver.findElement(By.name("Add Contact"));
45         el.click();
46         List<WebElement> textFieldsList = driver.findElements(By.tagName("textfield"));
47         textFieldsList.get(0).sendKeys("Some Name");
48         textFieldsList.get(2).sendKeys("[email protected]");
49         driver.findElement(By.name("Save")).click();
50     }
51 
52     public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
53         private RemoteTouchScreen touch;
54 
55         public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
56             super(remoteAddress, desiredCapabilities);
57             touch = new RemoteTouchScreen(getExecuteMethod());
58         }
59 
60         public TouchScreen getTouch() {
61             return touch;
62         }
63     }
64 }
複製代碼

6. 運行Test,注意AVD裏的Android如果沒有解鎖需要先解鎖

這時候我們可以看到AVD在運行了,

同時Appium的命令行有對應的輸出

7. 更多信息請參考Appium的Github

https://github.com/appium/appium

建議可以Clone下這個項目,在Sample-code文件夾下有示例

https://github.com/appium/appium/tree/master/sample-code

同時Testerhome發起翻譯活動的中文文檔也提交進去了

https://github.com/appium/appium/tree/master/docs/cn

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章