Appium(学习)

最近有空玩了一下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("Some@example.com");
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

OscarXie.net

关注质量与体验——电子商务与自动化测试
http://www.cnblogs.com/oscarxie/

### Appium 学习教程入门指南实例 #### 1. 安装和配置环境 为了开始学习Appium,首先需要完成一系列的基础准备工作。这包括但不限于安装Appium服务器以及设置必要的环境变量等[^3]。 ```bash npm install -g appium ``` #### 2. 启动Appium服务端 通过命令行工具可以轻松启动Appium的服务端部分。确保已经正确设置了路径以便能够全局访问`appium`命令。 ```bash appium & ``` #### 3. 连接测试设备或模拟器 无论是物理设备还是虚拟机,在执行自动化脚本之前都需要将其连接至开发机器上,并确认USB调试模式已开启(对于Android设备而言)。此外还需验证ADB/Instruments能否正常识别目标装置。 #### 4. 配置Desired Capabilities参数 当准备就绪之后,则需定义一些描述性的属性来告知Appium有关待测应用的信息,比如APK文件的位置、包名称及Activity入口点等等。 ```python desired_caps = { 'platformName': 'Android', 'deviceName': 'emulator-5554', 'appPackage': 'com.example.app', 'appActivity': '.MainActivity' } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) ``` #### 5. 执行基本交互动作 利用Python绑定库中的API接口实现对移动应用程序UI组件的操作,例如点击按钮、输入文本框内容或是滑动手势等。这里展示了一个简单的例子——向屏幕上的某个位置发送触摸事件[^2]: ```python action = TouchAction(driver) action.tap(x=50, y=50).perform() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值