appium第一个安卓自动化工程
转自:https://university.utest.com/how-to-set-up-your-first-android-automation-project-with-appium/
Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS and Android platforms. Native apps are those written using the iOS or Android SDKs. Mobile web apps are web apps accessed using a mobile browser (Appium supports Safari on iOS and Chrome or the built-in ‘Browser’ app on Android). Hybrid apps have a wrapper around a “webview” — a native control that enables interaction with web content. Projects likePhonegap, make it easy to build apps using web technologies that are then bundled into a native wrapper, creating a hybrid app.
Importantly, Appium is cross-platform. It allows you to write tests against multiple platforms (iOS and Android), using the same API. This enables code reuse between iOS and Android test suites.
For this example, I will use a contact manager app provided by Appium as a sample app and a Samsung Galaxy S4 with Android 4.4.2. Get Contact Manager at GitHub.
PREREQUISITES
- Eclipse is installed
- Android SDK and APIs for recent versions of Android
- Selenium Webdriver knowledge
- Java knowledge
SETUP
- Download Selenium Java zip archive and extract all the files in a folder called Selenium.
- Download Appium for Windows zip archive and extract all files in a folder called Appium.
- Download Appium Java client jar and add it to Apium folder. This contains abstract class AppiumDriver which inherits from Selenium Java client. IOSDriver and AndroidDriver both extend AppiumDriver and add specific methods for Android and iOS automation. Basically this is an adaptation of Selenium Java client,but adapted to mobile automation.
- Create a new java project in Eclipse and add Selenium and Appium Java client Jar files to the project ( Right-click project -> Properties -> java build path -> Add external JARs).
- Add a package and a new class to the project.

IMPORT REQUIRED LIBRARIES
1. The first step in writing a new test class is to import the needed libraries:
//used to verify if URL is malformed
import java.net.MalformedURLException;
//library used to create URL for the Appium server
import java.net.URL;
//library used to create the path to APK
import java.io.File;
//library used to find elements (by id, class, xpath etc)
import org.openqa.selenium.By;
//library for web element
import org.openqa.selenium.WebElement;
//libraries for configuring Desired Capabilities
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
//library for test methods
import org.junit.*;
//library for Appium drivers
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

CREATING THE PATH TO APK FILE
Since the apk is stored in the computer and is not already installed on the device we need to create a file object which represents the actual apk file on the disk. I placed the folder ContactManager which contains the apk file inside the Eclipse project.
File classpathRoot = new File(System.getProperty(“user.dir”)); // path to Eclipse project
File appDir = new File(classpathRoot, “/ContactManager”); // path to <project folder>/Contact Manager
File app = new File(appDir, “ContactManager.apk”); path to <project folder>/Contact Manager/ContactManager.apk
DESIRED CAPABILITIES
To be able to test the app on an actual device Desired Capabilities need to be set. Desired Capabilities are a set of keys and values sent to the Appium server to tell the server what kind of automation session we’re interested in starting up. There are also capabilities used to modify the behaviour of the server during automation.
DesiredCapabilities capabilities = new DesiredCapabilities();
//Name of mobile web browser to automate. Should be an empty string if automating an app instead.
capabilities.setCapability(CapabilityType.BROWSER_NAME, “”);
//which mobile OS to use: Android, iOS or FirefoxOS
capabilities.setCapability(“platformName”, “Android”);
//Mobile OS version – in this case 4.4 since my device is running Android 4.4.2
capabilities.setCapability(CapabilityType.VERSION, “4.4”);
//device name – since this is an actual device name is found using ADB
capabilities.setCapability(“deviceName”, “111bd508″);
//the absolute local path to the APK
capabilities.setCapability(“app”, app.getAbsolutePath());
//Java package of the tested Android app
capabilities.setCapability(“appPackage”, “com.example.android.contactmanager”);
// activity name for the Android activity you want to run from your package. This need to be preceded by a . (example: .MainActivity)
capabilities.setCapability(“appActivity”, “.ContactManager”);
// constructor to initialize driver object
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
HOW TO FIND DEVICENAME
Since we are trying to run the test on an actual device we need to use ADB to find the device name. This is required when creating desired capabilities to specify on what device you want your tests to be run. You can use this course to see how to use “adb devices” to find your device id. Once you run adb devices command the only thing left is to copy the device id in the code.
HOW TO FIND APPPACKAGE AND APPACTIVITY FOR APK FILE
- Download and install SDK.
- Inside SDK Manager download Tools (Android SDK build-tools) and APIs for recent versions of Android.
- Open SDK folder and go to build-tools folder.
- Open any folder (example: 21.1.2).
- Open a command prompt window in this folder ( Shift+ right click – Open command window here).
- Run command “aapt list -a <path_to_apk_we_need_to_test> >manifest.txt” ( this command will write the app manifest in manifest.txt file inside the same folder).
- Open the manifest.txt txt file.
- At the beginning of the file there should be details about the package including name ( example: com.example.android.contactmanager).
- At the end of the file there should be details about activity including name ( example: ContactManager).


HOW TO RUN APPIUM SERVER
- Go to the Appium folder you downloaded earlier and run Appium.exe.
- Click on General Settings button (second button) and verify URL you defined in Eclipse matches the server address and port from the Appium app.

- Click on Launch Appium Node Server.

HOW TO FIND ELEMENTS IN A NATIVE APP
- Go to SDK folder and open the tools folder.
- Open uiautomatorviewer.

- On the actual device, open the app to the page you want to automate.
- In UI Automator Viewer, click on Device screenshot (second button).
- Click any element on the page and look in the Node detail window (there you will find details about the selected element: id, class, name, etc.)
- Use the info found (id, class) in Eclipse to click buttons, fill input fields.

SELENIUM CODE
Since Appium server is running and all the settings for testing on the actual phone are set we are ready to write test methods:
@Test
public void addContact() throws exception{
// locate Add Contact button and click it
WebElement addContactButton = driver.findElement(By.name(“Add Contact”));
addContactButton.click();
//locate input fields and type name and email for a new contact and save it
List<WebElement> textFieldsList = driver.findElementsByClassName(“android.widget.EditText”);
textFieldsList.get(0).sendKeys(“Some Name”);
textFieldsList.get(2).sendKeys(“Some@example.com”);
driver.findElementByName(“Save”).click();
//insert assertions here
}
Since this is the first basic test to see how and if the automation is working there are no assertions in the code. You can modify the code and add assertions to see if contact was added successfully. Learn more about assertions in this uTest University course.
The entire code:
package tests;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.junit.*;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
public class TestOne {
private AndroidDriver driver;
@Before
public void setUp() throws MalformedURLException{
File classpathRoot = new File(System.getProperty(“user.dir”));
File appDir = new File(classpathRoot, “/ContactManager”);
File app = new File(appDir, “ContactManager.apk”);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, “”); //Name of mobile web browser to automate. Should be an empty string if automating an app instead.
capabilities.setCapability(“platformName”, “Android”);
capabilities.setCapability(CapabilityType.VERSION, “4.4”);
capabilities.setCapability(“deviceName”, “111bd508″);
capabilities.setCapability(“app”, app.getAbsolutePath());
capabilities.setCapability(“appPackage”, “com.example.android.contactmanager”);
capabilities.setCapability(“appActivity”, “.ContactManager”);
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
}
@Test
public void addContact() throws Exception {
WebElement addContactButton = driver.findElement(By.name(“Add Contact”));
addContactButton.click();
List<WebElement> textFieldsList = driver.findElementsByClassName(“android.widget.EditText”);
textFieldsList.get(0).sendKeys(“Some Name”);
textFieldsList.get(2).sendKeys(“Some@example.com”);
driver.findElementByName(“Save”).click();
//insert assertions here
}
appium第一个安卓自动化工程的更多相关文章
- [转]RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第一篇【安装】
前言:关于RobotFrameWork+APPIUM实现对安卓APK的自动化测试的文章都是取自于乐于分享知识于网络的好心人们,所以我也希望我的知识可以分享给大家. 首先我们先罗列一下我们要安装的软件 ...
- RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第一篇【安装】
文章来源http://blog.csdn.net/deadgrape/article/details/50563119 前言:关于RobotFrameWork+APPIUM实现对安卓APK的自动化测试 ...
- RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第三篇【实例】
http://blog.csdn.net/deadgrape/article/details/50579565 在这一篇里我先让大家看一下RF+APPIUM这个框架的实际运行时什么样子的,给大家一个直 ...
- Android开发之 Windows环境下通过Eclipse创建的第一个安卓应用程序(图文详细步骤)
第一篇 windows环境下搭建创建的第一个安卓应用程序 为了方便,我这里只采用了一体包进行演示. 一.下载安卓环境的一体包. 官网下载:安卓官网(一般被墙了) 网盘下载: http://yunpa ...
- Android项目第一天,下载安装Android Studio和“我的第一个安卓项目”
一.AS的下载我是在AS官方网站进行下载的最新版本,如图所示 二.AS的安装过程 点击你下载的安装包安装即可,傻瓜式一站到底. 到这一步时选择第二个按钮, 随后出现如下界面 这个窗口是提示我们设置代理 ...
- Appium Desktop Inspector 安卓真机配置(Windows)
本文是基于 Windows环境 通过Appium Desktop 测试真机,首先要确保测试机已经和电脑正确连接(将手机和电脑通过USB数据线连接,手机打开USB调试) 确认电脑与手机是否连接成功的方法 ...
- Android Studio 学习笔记1.1 创建自己的第一个安卓项目并且打包APK
自从上一次安装完安卓开发工具Android Studio后抽时间看视屏尝试编写自己的第一个安卓项目约两周的时间 每天下班后会花上1~2小时的时间去学习 目前的成果如下:次元宅的我.apk 嘛 总而 ...
- 七天从零基础学习android(2)--第一个安卓程序
在环境配置的那一部分,已经把基础的环境配置好了,接下来应该实现第一个安卓程序,就是著名的hello world 先在avd里面新建一个虚拟机,并且启动它 然后该虚拟机器能够在eclipse上正常识别 ...
- Android开发第一天---AndroidStudio的安装和第一个安卓开发
今天已经是开始学习Android的第二天,我居然才把AndroidStudio开发环境安装并配置好,我只能说“我太难了”,下了好几个版本,终于找到了一个合适的,得出一个结论外国的东西是真的不太好用啊, ...
随机推荐
- 原 jmeter中类似lr的场景设置
有一天如果你们领导来一句给我测下这个首页到底能扛多少并发,并发量极限是多少,这时你不要慌不要忙,拿出jmeter神器,设置下场景,目标设置成1000,每10秒启动100个并发,等着看什么时候系统响应开 ...
- Dell Omsa在Linux服务器上安装部署
前言 本页详述了在一台Linux(RHEL6.4 x86_64)服务器上部署安装OMSA的通用做法,包括OMSA软件的获取方法和安装步骤. 演示环境: PowerEdge R620, RHEL 6.4 ...
- [android开发篇]自定义权限
有时候,我们可能遇到如下需求场景:当用户在一个应用程序中进行某项操作时,会启动另外一个应用程序,最常见的时直接打开了另外一个应用程序,并进入其中某个Activity(如:有的应用中有推荐应用列表,当用 ...
- Subsequence(hdu 3530)
题意:给你一个长度为n的数列,要求一个子区间,使得区间的最大值与最小值的差s满足,m<=s<=k,求满足条件的最长子区间 /* 单调队列 我们可以用单调队列分别维护最大值和最小值 当差值大 ...
- Codevs 2956 排队问题
2956 排队问题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 有N个学生去食堂,可教官规定:必须2人或3人组成一组,求有多少种不 ...
- StoryBoard中使用xib
转自:http://blog.csdn.net/li6185377/article/details/8131042 一般自定义View 代码方式 有 在初始化的时候添加 子Vi ...
- [bzoj1110][POI2007]砝码Odw_贪心
bzoj-1110 POI-2007 砝码Odw 参考博客:http://hzwer.com/4761.html 题目大意:在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件 ...
- AIO
IBM® 市场 (英文) 提交 我的 IBM 站点导航 学习 开发 社区 学习 Java technology 内容 概览 简单介绍 Asynchronous I/O 开始简单的异步 I/ ...
- BZOJ——1571: [Usaco2009 Open]滑雪课Ski
http://www.lydsy.com/JudgeOnline/problem.php?id=1571 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: ...
- Crypto另外两段加密解密的代码
第一段代码风格-平铺直叙: import sys from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex class p ...