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

  1. Eclipse is installed
  2. Android SDK and APIs for recent versions of Android
  3. Selenium Webdriver knowledge
  4. Java knowledge

SETUP

  1. Download Selenium Java zip archive and extract all the files in a folder called Selenium.
  2. Download Appium for Windows zip archive and extract all files in a folder called Appium.
  3. 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.
  4. 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).
  5. 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

  1. Download and install SDK.
  2. Inside SDK Manager download Tools (Android SDK build-tools) and APIs for recent versions of Android.
  3. Open SDK folder and go to build-tools folder.
  4. Open any folder (example: 21.1.2).
  5. Open a command prompt window in this folder ( Shift+ right click – Open command window here).
  6. 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).
  7. Open the manifest.txt txt file.
  8. At the beginning of the file there should be details about the package including name ( example: com.example.android.contactmanager).
  9. At the end of the file there should be details about activity including name ( example: ContactManager).

HOW TO RUN APPIUM SERVER

  1. Go to the Appium folder you downloaded earlier and run Appium.exe.
  2. Click on General Settings button (second button) and verify URL you defined in Eclipse matches the server address and port from the Appium app. 
  3. Click on Launch Appium Node Server.

HOW TO FIND ELEMENTS IN A NATIVE APP

  1. Go to SDK folder and open the tools folder.
  2. Open uiautomatorviewer. 
  3. On the actual device, open the app to the page you want to automate.
  4. In UI Automator Viewer, click on Device screenshot (second button).
  5. 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.)
  6. 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(“[email protected]”);
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(“[email protected]”);
driver.findElementByName(“Save”).click();
//insert assertions here
}

时间: 2024-11-09 02:55:55

appium第一个安卓自动化工程的相关文章

【原创】开发第一个安卓程序(教程系列贴)

[面向人员]:立志于安卓开发的所有人员,做到真正的从零基础起步: [教程说明]:本教程在内容上保证原创.简洁.详细,在附件链接上保证可用.最新.完整: [回帖说明]:教程有不清晰的地方,请及时回帖互动,楼主会第一时间更新帖子,谨以此开源.分享.共进. 1.通过代理在线更新SDK 依次打开安卓001教程建立的开发文件夹,android/develope,找到并双击SDK Manager.exe可执行文件 稍等片刻,自动弹出Android SDK Manager Log窗口,窗口内未配置代理钱会显示

使用appium框架测试安卓app时,获取toast弹框文字时,前一步千万不要加time.sleep等等待时间。

使用appium框架测试安卓app时,如果需要获取toast弹框的文案内容,那么再点击弹框按钮之前,一定记得千万不要加time.sleep()等待时间,否则有延迟,一直获取不到: 获取弹框的代码: message=self.driver.find_element_by_xpath("//*[contains(@text,'成功添加到购物车')]")   原文地址:https://www.cnblogs.com/zhouchuanlun/p/12687890.html

【Qt for Android】第一个安卓程序

1)首先需要去官网上下载 Qt for android 版本的 Qt SDK 2)下载 android 相关的 SDK,下载地址不用找了,在Qt Creator工具选项的Android配置中有相应的下载链接地址. 3)下载完安卓SDK后,在上面的配置界面中指定相应SDK的目录,然后在"Build & Run"中新增Android的Kits 4)编写Qt代码后,点击"Run"即可,在弹出的Android设备列表中,选择程序要安装运行的Android设备.(下图

【1】第一个安卓程序

第一个程序从 Hello World 开始?? 也许C语言学习是从这开始的,但是书上不是这么开始的,也许起点就决定了高度?? 一.新建安卓项目 (1)选择安卓项目 (2)随便建个名字的项目,默认下一步 二.运行环境 (1)新建虚拟设备,在电脑上运行这个虚拟的安卓模拟器. (2)在自己的手机上运行,手机用数据线连接电脑,调为开发者模式,然后运行的时候就会检测到手机,然后选择用手机运行. 我选择把自己的小米3作为了开发的测试手机,因为电脑的安卓模拟器调试应该会很卡. 三.运行出来其实就是个hello

。。。我的第一个安卓小程序。。。

今天开学习安卓了,还是比较开心的,因为这个对于零基础安卓的我来说,学习安卓是比较有吸引力的!!! 今天学到了注册控件事件的两种方法(暂时只学习了两种): 第一种,直接在content_main.xml文件中,点击“design”按钮,进行设计,点击控件,然后在右边的属性栏里面,找到onClick属性,填写方法名称(事件函数名),然后在MainAcvity.java中,写public void 注册事件方法名称(View view){...},注意:这里面的参数View view 是一定要填写的,

【1】Android 学习笔记 【第一个安卓程序】

一.java JDK  & 配置 java 系统变量 java JDK 这个请自行百度安装 配置 java 系统变量  1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值(jdk安装目录):C:\Program Files\Java\jdk1.7.0 变量名:CLASSPATH 变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; 3. 选择“系统变量

AndroidStudio学习笔记-第一个安卓程序

要带一个本科生做一部分跟安卓有点关系的项目,于是趁着机会学习一下编写安卓程序. 第一篇材料来自谷歌官方,传送门:https://developer.android.com/training/basics/firstapp/index.html.由于官方例程写的很清楚了,基本上只要按着步骤就好了,所以不打算完全的复制粘贴,主要实践过程中我的一些理解和思考. 1. 创建新工程 一个安卓程序的层次结构为 Application -> many activities (可以理解为很多个界面) -> l

appium第一天

严格来说不算第一天.. 花了一天,终于把毕业设计做得差不多..其实还差很多..不过安心了.. 昨天开始尝试安装appium..官网上有详细介绍..当然,还是遇到些问题.. 1.node.js安装后,按照最简单的方法:cmd输入:npm install -g appium 结果:失败. 原因:网络连接失败. 花了两个小时,在网速高达几M/S的深夜校园网..结果还是无法避开防火墙的火眼金睛.不得已,放弃这种简洁的方式. 解决:下载appium的包,解压后直接软件安装. 2.检查环境是否配置好.. 在

我的第一个安卓程序

当天的笔记 重写(Override) 就是子类在应许父类访问对父类方法的重写.返回值行参都不能改变.即外壳不变,核心重写! 好处:子类可以根据自己的需要定制自己的行为: Animal b = new Dog();//可以调用到子类重写的方法 注意:在上面的例子中可以看到,尽管b属于Animal类型,但是它运行的是Dog类的move方法. 这是由于在编译阶段,只是检查参数的引用类型 当需要在子类中调用父类的被重写方法时,要使用super关键字 安卓学习流程图