如何使用Appium测试Android App

package com.wbsn.appiumdemo;

 

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

 

import org.openqa.selenium.*;

importorg.openqa.selenium.interactions.HasTouchScreen;

importorg.openqa.selenium.interactions.TouchScreen;

importorg.openqa.selenium.remote.CapabilityType;

importorg.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteTouchScreen;

importorg.openqa.selenium.remote.RemoteWebDriver;

importorg.openqa.selenium.support.ui.ExpectedConditions;

importorg.openqa.selenium.support.ui.WebDriverWait;

 

import java.io.File;

import java.net.URL;

import java.util.List;

 

public class WbsnAppiumTest {

 

       private WebDriver driver;

        

        @Before

       public void setUp() throws Exception {

           // set up appium

           File classpathRoot = new File(System.getProperty("user.dir"));      

           File appDir = new File(classpathRoot, "apps");

           File app = new File(appDir, "WbsnMobileSecurity-debug.apk");

           DesiredCapabilities capabilities = newDesiredCapabilities();

           capabilities.setCapability("device","Android");

           capabilities.setCapability(CapabilityType.BROWSER_NAME, "");      

           capabilities.setCapability(CapabilityType.VERSION, "4.4");     

           capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");     

           capabilities.setCapability("platformName", "Android");

           capabilities.setCapability("deviceName","AndroidEmulator");

           //capabilities.setCapability("deviceName","4df7ad0e0342bfaf");

           

           capabilities.setCapability("app",app.getAbsolutePath());

           capabilities.setCapability("app-package", "com.wbsn.android.wms");

           //capabilities.setCapability("app-activity","com.websense.android.wms/.diagnostics.Diagnostics");

           capabilities.setCapability("app-activity", "com.websense.android.wms/com.wbsn.android.wms.WMSEndpointHome");

           driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

          

        }

 

      

        @Test

       public void lookupDNS() throws Exception {

             System.out.println("Lookup DNS onthe wbsn security app1...");       

             

              WebDriverWait wait = new WebDriverWait(driver, 10);

             

              try{

                     WebElement diag_btn = driver.findElement(By.id("com.wbsn.android.wms:id/diagnostics_btn"));

                    wait.until(ExpectedConditions.visibilityOf(diag_btn));

                    diag_btn.click();

                     

                     WebElement lookup_dns_btn = driver.findElement(By.id("com.wbsn.android.wms:id/dg_lookup_dns_btn"));

                    wait.until(ExpectedConditions.visibilityOf(lookup_dns_btn));

                    lookup_dns_btn.click();

                    

                    WebElement network_address_edt = driver.findElement(By.id("com.wbsn.android.wms:id/dg_network_address_edt"));

                    wait.until(ExpectedConditions.visibilityOf(network_address_edt));

                    //network_address_edt.clear();

                    network_address_edt.sendKeys("www.baidu.com");

                 

                     WebElement start_btn = driver.findElement(By.id("com.wbsn.android.wms:id/dg_start_btn"));

                    wait.until(ExpectedConditions.visibilityOf(start_btn));

                    start_btn.click();

                 

                     WebElement result_edt = driver.findElement(By.id("com.wbsn.android.wms:id/dg_result_edt"));

                    wait.until(ExpectedConditions.visibilityOf(result_edt));

 

                     String results =result_edt.getText();

                     System.out.println(results);

                 

                     WebElement save_test_logs_btn =driver.findElement(By.id("com.wbsn.android.wms:id/dg_save_test_logs_btn"));

                     wait.until(ExpectedConditions.visibilityOf(save_test_logs_btn));

                     save_test_logs_btn.click();

                 

                     WebElement save_log_status_txt= driver.findElement(By.id("com.wbsn.android.wms:id/dg_save_log_status_txt"));

                     wait.until(ExpectedConditions.visibilityOf(save_log_status_txt));

                     results =save_log_status_txt.getText();

                 

                     assertTrue(results.contains("Test logsaved"));                 

                  

              }

             catch(Exception e){

                     System.out.println(e.getMessage());

             }           

        }

      

        @After

       public void tearDown() throws Exception {

            driver.quit();

       }

 

      

       public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {

               

              private RemoteTouchScreen touch;

      

           public SwipeableWebDriver(URL remoteAddress,Capabilities desiredCapabilities) {

              super(remoteAddress,desiredCapabilities);

              touch = new RemoteTouchScreen(getExecuteMethod());

           }

      

           public TouchScreen getTouch() {

              return touch;

           }

           

        }   

        

}

Appium Introduction

Appium is an open source test automation framework(tool) for usewith native, hybridand mobile web apps.
It drives iOS and Android apps using the WebDriver protocol.

Essentially, we support a subset of the SeleniumWebDriver JSON Wire Protocol, and extend it so that you can specifymobile-targeted desiredcapabilities to run your test through Appium.

Appium Concepts

Client/Server Architecture
Appium is at its heart a webserver that exposes a REST API. It receivesconnections from a client, listens for commands, executes those commands on amobile device, and responds with an HTTP response representing the result ofthe command execution.

The fact that we have a client/server architecture opens up a lot ofpossibilities: we can write our test code in any language that has a httpclient API, but it is easier to use one of the Appium client libraries. We can put theserver on a different machine than our tests are running on.

Session
Automation is always performed in the context of a session. Clients initiate asession with a server in ways specific to each library, but they all end upsending a POST /sessionrequest to the server, with a JSON object called the 'desired capabilities'object. At this point the server will start up the automation session andrespond with a session ID which is used for sending further commands.

Desired Capabilities
Desired capabilities are a set of keys and values (i.e., a map or hash) sent tothe Appium server to tell the server what kind of automation session we'reinterested in starting up. There are also various capabilities which can modifythe behavior of the server during automation. For example, we might set the platformName capability to iOS to tell Appium that we want an iOSsession, rather than an Android one. Or we might set the safariAllowPopups capability to true in order to ensure that, during aSafari automation session, we're allowed to use JavaScript to open up newwindows. See the capabilitiesdoc for the complete list of capabilities available for Appium.

Appium Server
Appium is a server written in Node.js. It can be built and installed fromsource or installed directly from NPM.

Appium Clients
There are client libraries (in Java, Ruby, Python, PHP, JavaScript, and C#)which support Appium's extensions to the WebDriver protocol.

driver = newSwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);

> info: [debug] Appium session started with sessionId 643bcde5-a8f4-440c-ba6b-0d25e42402f8

> info: [37m<-- POST /wd/hub/session[39m[36m303[39m[90m 33160.514 ms - 9[39m [90m[39m

> info: [37m-->[39m [37mGET[39m[37m/wd/hub/session/643bcde5-a8f4-440c-ba6b-0d25e42402f8[39m [90m{}[39m

> info: [debug] Responding to client with success:{"status":0,"value":{"platform":"WINDOWS","browserName":"","platformVersion":"4.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"C:\\Users\\tmsautomation\\workspace\\xwang\\AppiumTest\\apps\\WebsenseMobileSecurity-debug.apk","browserName":"","app-package":"com.websense.android.wms","app-activity":"com.websense.android.wms/com.websense.android.wms.WMSEndpointHome","platformName":"Android","device":"Android","version":"4.4","deviceName":"AndroidEmulator","platform":"WINDOWS"},"app":"C:\\Users\\tmsautomation\\workspace\\xwang\\AppiumTest\\apps\\WebsenseMobileSecurity-debug.apk","app-package":"com.websense.android.wms","app-activity":"com.websense.android.wms/com.websense.android.wms.WMSEndpointHome","platformName":"Android","device":"Android","version":"4.4","deviceName":"AndroidEmulator"},"sessionId":"643bcde5-a8f4-440c-ba6b-0d25e42402f8"}

Android support uses the UiAutomator framework for newer platforms and Selendroid for olderAndroid platforms.

> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.websense.android.wms:id/diagnostics_btn","context":"","multiple":false}]

> info: [debug] [BOOTSTRAP] [debug] Got data from client:{"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.websense.android.wms:id/diagnostics_btn","context":"","multiple":false}}

> info: [debug] [BOOTSTRAP] [debug] Got command of typeACTION

> info: [debug] [BOOTSTRAP] [debug] Got command action:find

> info: [debug] [BOOTSTRAP] [debug] Findingcom.websense.android.wms:id/diagnostics_btn using ID with the contextId:  multiple: false

> info: [debug] [BOOTSTRAP] [debug] Using:UiSelector[INSTANCE=0, RESOURCE_ID=com.websense.android.wms:id/diagnostics_btn]

> info: [debug] [BOOTSTRAP] [debug] Returning result:{"value":{"ELEMENT":"1"},"status":0}

> info: [debug] Responding to client with success:{"status":0,"value":{"ELEMENT":"1"},"sessionId":"643bcde5-a8f4-440c-ba6b-0d25e42402f8"}

Download ADT

http://developer.android.com/sdk/index.html

With a single download, the Eclipse ADTbundle includes everything you need to begin developing apps:

  • Eclipse + ADT plugin
  • Android SDK Tools
  • Android Platform-tools
  • A version of the Android platform
  • A version of the Android system image for the emulator
Development tools
  • JDK 6 (JRE alone is not sufficient)
  • Apache Ant 1.8 or later
  • Not compatible with Gnu Compiler for Java (gcj)

Download Selenium

http://www.seleniumhq.org/download/

http://selenium-release.storage.googleapis.com/2.44/selenium-server-standalone-2.44.0.jar

Source code: http://grepcode.com/file/repo1.maven.org/maven2/org.seleniumhq.webdriver/webdriver-remote-client/0.9.7376/org/openqa/selenium/remote/RemoteWebDriver.java

 

How to run android app automation test

1.      Create java project

2.      Import selenium package

3.      Import Junit package

4.      Add test app apk file

5.      Create test class

6.      Attach real device or start emulator fromAndroid VDM.

7.      Use uiautomatorviewer tool to find out app uicontrols id and update test code.

Google includes the uiautomatorviewer toolin the Android SDK. uiautomator requires API 16 (Android 4.1, Jelly Bean) orabove. Appium’s uiautomator support works best on API 17 (Android 4.2, JellyBean MR1) or above.

8.      Start Appium server

9.      Run test

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值