热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

appium第一个安卓自动化工程

转自:https:university.utest.comhow-to-set-up-your-first-android-automation-project-with-appi

转自: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 /Contact Manager
File app = new File(appDir, “ContactManager.apk”); path to /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 >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 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 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第一个安卓自动化工程


推荐阅读
  • 使用 ModelAttribute 实现页面数据自动填充
    本文介绍了如何利用 Spring MVC 中的 ModelAttribute 注解,在页面跳转后自动填充表单数据。主要探讨了两种实现方法及其背后的原理。 ... [详细]
  • 使用IntelliJ IDEA高效开发与运行Shell脚本
    本文介绍了如何利用IntelliJ IDEA中的BashSupport插件来增强Shell脚本的开发体验,包括插件的安装、配置以及脚本的运行方法。 ... [详细]
  • 在使用KVM虚拟化技术通过NAT模式启动虚拟机时,可能会遇到qemu-ifup-nat脚本执行失败的错误。本文将详细介绍如何诊断和解决这一问题。 ... [详细]
  • ED Tree HDU4812 点分治+逆元
    这道题非常巧妙!!!我们进行点分治的时候,算出当前子节点的所有子树中的节点,到当前节点节点的儿子节点的距离,如下图意思就是当前节点的红色节点,我们要求出红色节点的儿子节点绿色节点, ... [详细]
  • IIS6批量添加主机头,修改IIS数据库
    首先,找到IIS的数据库。默认是在C:\WINDOWS\system32\inetsrv下的MetaBase.xml文件。如果找不到,请右键右键站点-》所有服务-》将配置保存到一个 ... [详细]
  • 本文详细介绍了Python中的生成器表达式、列表推导式、字典推导式及集合推导式等,探讨了它们之间的差异,并提供了丰富的代码示例。 ... [详细]
  • 利用Cookie实现用户登录状态的持久化
    本文探讨了如何使用Cookie技术在Web应用中实现用户登录状态的持久化,包括Cookie的基本概念、优势及主要操作方法,并通过一个简单的Java Web项目示例展示了具体实现过程。 ... [详细]
  • 本文详细介绍了JavaScript中数组的转换方法、栈方法、队列方法、重排序方法及操作方法,包括toLocaleString()、toString()、valueOf()等基本转换方法,以及push()、pop()、shift()、unshift()等用于模拟栈和队列行为的方法。 ... [详细]
  • 探索OpenWrt中的LuCI框架
    本文深入探讨了OpenWrt系统中轻量级HTTP服务器uhttpd的工作原理及其配置,重点介绍了LuCI界面的实现机制。 ... [详细]
  • LoadRunner中的IP欺骗配置与实践
    为了确保服务器能够有效地区分不同的用户请求,避免多人使用同一IP地址造成的访问限制,可以通过配置IP欺骗来解决这一问题。本文将详细介绍IP欺骗的工作原理及其在LoadRunner中的具体配置步骤。 ... [详细]
  • 本文探讨了Java编程语言中常用的两个比较操作符==和equals方法的区别及其应用场景。通过具体示例分析,帮助开发者更好地理解和使用这两个概念,特别是在处理基本数据类型和引用数据类型的比较时。 ... [详细]
  • 本文详细介绍了PHP中的几种超全局变量,包括$GLOBAL、$_SERVER、$_POST、$_GET等,并探讨了AJAX的工作原理及其优缺点。通过具体示例,帮助读者更好地理解和应用这些技术。 ... [详细]
  • 本文概述了在GNU/Linux系统中,动态库在链接和运行阶段的搜索路径及其指定方法,包括通过编译时参数、环境变量及系统配置文件等方式来控制动态库的查找路径。 ... [详细]
  • 本文详细介绍了如何使用Rufus工具制作一个兼容UEFI启动模式的Windows Server 2008 R2安装U盘,包括必要的软件和步骤。 ... [详细]
  • 本文介绍如何使用 Python 计算两个时间戳之间的时间差,并将其转换为毫秒。示例代码展示了如何通过 `time` 和 `datetime` 模块实现这一功能。 ... [详细]
author-avatar
yimotoumingg_681
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有