热门标签 | 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第一个安卓自动化工程


推荐阅读
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
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社区 版权所有