appium精简教程

环境配置

package appium;

import io.appium.java_client.android.*;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
//import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.testng.Assert;
//import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.util.*;

public class appium {
    private AndroidDriver dr;
    private String reportPath = "C:\\inetpub\\wwwroot\\Content\\Images\\";
    private int sleepBase = 1000;
    public String port;
    public String udid;
    public String pver;

    @BeforeTest  //must be j2se-1.5
    @Parameters({ "port", "udid" , "pver"})
    public void beforeSuite(String port, String udid, String pver) {
    //public void beforeSuite() {
        this.port = port;
        this.udid = udid;
        this.pver = pver;

        //this.port = "4723";
        //this.udid = "0123456789ABCDEF";
        //this.pver = "4.4";
    }
    @Test
    public void TC_001() throws InterruptedException, MalformedURLException
    {
       try
       {
         LaunchApp();

         //Step1: go to view
         ClickByIndex("android.widget.ImageView", 6, "回单验证");

         //Step2: fill the form ; To do: put the input text into XML
         java.util.List<WebElement> arrEditText= dr.findElementsByClassName("android.widget.EditText");
         arrEditText.get(0).sendKeys("3243243255");
         arrEditText.get(1).sendKeys("1112312345123451234512345456");
         arrEditText.get(2).sendKeys("221234512345123456");

         InputkeyboardNum(arrEditText.get(3));

         arrEditText.get(4).sendKeys("441234123451234556");
         arrEditText.get(5).sendKeys("551234123451234556");
         PrintScreen(reportPath+udid+"_填写表单.jpg");

         //Step3: click search
         ClickByID("com.boc.bocsoft.phone:id/btn_receipt_ok", "点击查询");

         //Step4: check the string
         PrintScreen(reportPath+udid+"_回单验证查询.jpg");
         String text = dr.findElementById("com.boc.bocsoft.phone:id/tv_receipt_result_one_show_no").getText();
         Assert.assertEquals(text, "没有符合查询条件的交易回单");
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
    }  

    @Test
    public void TC_002() throws InterruptedException, MalformedURLException
    {
        Login();

        //Step1: go to view
            ClickByIndex("android.widget.ImageView", 3, "转账汇款");

        //Step2: fill the form
        ClickByID("com.boc.bocsoft.phone:id/text_function_menu_child", "对公单笔汇款");
        ClickByID("com.boc.bocsoft.phone:id/payee_picker", "选择收款人");
        ClickByIndex("android.widget.TextView",2, "签约收款人");
        ClickByIndex("android.widget.TextView",8, "选择签约收款人");

        InputkeyboardNum(dr.findElementById("com.boc.bocsoft.phone:id/item_content"));

        //ClickByID("com.boc.bocsoft.phone:id/pick_indicator", "输入用途");
        //dr.findElementById("com.boc.bocsoft.phone:id/usage_content").sendKeys("输入中文用途");
        //dr.sendKeyEvent(4);
        //ClickByID("com.boc.bocsoft.phone:id/confirm", "确定");

        for(int i =0; i< 3;i++)
        {
            swipeScreen(716.0, 988.0, 530.0, 504.0, 0.5, 4, dr);
            dr.swipe(716, 988, 530, 504, 1);
        }

        ClickByID("com.boc.bocsoft.phone:id/submit", "提交");
        String expect = dr.findElementById("com.boc.bocsoft.phone:id/result_info").getText();
        System.out.println(expect);

        ClickByID("com.boc.bocsoft.phone:id/main_iv_bank", "主页");
        ClickByIndex("android.widget.ImageView", 3, "转账汇款");
        ClickByIndex("android.widget.TextView", 9, "网银操作记录查询");
        ClickByID("com.boc.bocsoft.phone:id/operation_record_trade_query", "查询网银操作记录");
        List<WebElement> arrActual = dr.findElementsById("com.boc.bocsoft.phone:id/review_amount");
        String actual = arrActual.get(arrActual.size()-1).getText();
        System.out.println(actual);
        Assert.assertTrue(expect.contains(actual), "expect: " + expect + " ; actual: " + actual);
    }

//basic operation
    private void ClickByID(String id, String log) throws InterruptedException, MalformedURLException
    {
        Thread.sleep(2000);
        System.out.println("Click " + log);
        dr.findElementById(id).click();
        Thread.sleep(sleepBase * 5);
        PrintScreen(reportPath+udid+ "_" + log + ".jpg");
    }
    private void ClickByIndex(String className, int index, String log) throws InterruptedException, MalformedURLException
    {
        Thread.sleep(2000);
        java.util.List<WebElement> arr= dr.findElementsByClassName(className);
        arr.get(index).click();
        System.out.println("Click " + log);
        Thread.sleep(sleepBase * 5);
        PrintScreen(reportPath+udid+ "_" + log + ".jpg");
    }
    private void SendKeysByID(String id, String keys) throws InterruptedException, MalformedURLException
    {
        System.out.println("Send " + keys);
        dr.findElementById(id).sendKeys(keys);
        Thread.sleep(1000);
    }

    private void Tappoint(int x, int y) throws InterruptedException, MalformedURLException
    {
        System.out.println("Tappoint: " + x + " , " + y);
        dr.tap(1, x, y, 100);
        Thread.sleep(1000);    

    }
    private void PrintScreen(String fileName) throws InterruptedException, MalformedURLException
    {
        System.out.println(fileName);

        File file= null;
        try
        {
           file= dr.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(file, new File(fileName));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    //
    public static void swipeScreen(Double startX, Double startY, Double endX,
            Double endY, Double duration, int repeat, AndroidDriver driver) {
            JavascriptExecutor js = (JavascriptExecutor) driver;
            java.util.HashMap<String, Double> swipeObj = new java.util.HashMap<String, Double>();
            swipeObj.put("startX", startX);
            swipeObj.put("startY", startY);
            swipeObj.put("endX", endX);
            swipeObj.put("endY", endY);
            for (int i = 0; i < repeat; i++) {
                try {
                    js.executeScript("mobile: flick", swipeObj);
                } catch (Exception ex) {
                    System.out.println("滑动屏幕失败");
                }
            }
        }
    public static void scroll(String direction, AndroidDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        java.util.HashMap<String, String> scrollObject = new java.util.HashMap<String, String>();
        scrollObject.put("direction", direction);
        js.executeScript("mobile: scroll", scrollObject);
    }

//app operation
    private void LaunchApp() throws InterruptedException, MalformedURLException
    {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("deviceName", udid);
        capabilities.setCapability("udid", udid);
        capabilities.setCapability("platformVersion", pver);
        capabilities.setCapability("platformName", "Android");
        //aapt.exe dump badging apkPath
        capabilities.setCapability("appPackage", "com.boc.bocsoft.phone");
        capabilities.setCapability("appActivity", "com.boc.bocsoft.phone.activity.business.splash.MEBSplashActivity");
        capabilities.setCapability("unicodeKeyboard", "True");  //for input Chinese
        capabilities.setCapability("resetKeyboard", "True");

        dr = new AndroidDriver(new URL("http://127.0.0.1:" + port + "/wd/hub"),capabilities);
        Thread.sleep(sleepBase * 10);
        PrintScreen(reportPath+udid+"_Launch.jpg");
    }
    private void ExitAppium()
    {
        if(dr != null)
        {
            dr.closeApp();
        }
    }
    private void Login() throws InterruptedException, MalformedURLException
    {
            try
            {
                LaunchApp();
                ClickByID("com.boc.bocsoft.phone:id/main_title_right_btn", "登录");
                SendKeysByID("com.boc.bocsoft.phone:id/et_login_user_name", "jqhd001");
                //SendKeysByID("com.boc.bocsoft.phone:id/sipbox_login_password", "0123456789abc");
                //SendKeysByID("com.boc.bocsoft.phone:id/sipbox_login_verifys", "456789");

                dr.findElementById("com.boc.bocsoft.phone:id/sipbox_login_password").click();
                Thread.sleep(3000);
                for(int i=0;i<5;i++)
                {
                    Tappoint(60,1360);
                    Tappoint(160,1360);
                }
                dr.sendKeyEvent(4);

                dr.findElementById("com.boc.bocsoft.phone:id/sipbox_login_verifys").click();
                Thread.sleep(1000);
                for(int i=0;i<6;i++)
                {
                    Tappoint(178,1360);
                }
                dr.sendKeyEvent(4);

                PrintScreen(reportPath+udid+"_登录.jpg");
                ClickByID("com.boc.bocsoft.phone:id/tv_login_submits", "点击登录");

                //wait for login: appium will automatic shutdown if no new cmd in 60s
                System.out.println("Wait 100s for login");
                WebElement loginSign = null;
                for(int i=0;i<5;i++)
                {
                    Thread.sleep(20000);
                    Tappoint(0,0);

                    try{
                    loginSign = dr.findElementById("com.boc.bocsoft.phone:id/btn_dialog_error_enter");}
                    catch(Exception e){}

                    if(loginSign != null)
                    {
                        System.out.println("Login done ! " + loginSign);
                        ClickByID("com.boc.bocsoft.phone:id/btn_dialog_error_enter", "修改密码提示");
                        return;
                    }
                }
            }
            catch(Exception e){}
        //to do: we may have to retry 3 times because login always failed by bad network
    }
    private void InputkeyboardNum(WebElement ele) throws InterruptedException, MalformedURLException
    {
        System.out.println("software keyboard: input 123");
        ele.click();
        Thread.sleep(2000);
        //int x = dr.manage().window().getSize().width;
        //int y = dr.manage().window().getSize().height;
        Tappoint(137, 1330);
        Tappoint(414, 1330);
        Tappoint(670, 1330);
        Tappoint(940, 1670);
        System.out.println("Input done");
    }

    @AfterTest
    public void afterTest()
    {
        ExitAppium();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1">
  <parameter name = "port" value = "4723"/>
  <parameter name = "udid" value = "0123456789ABCDEF"/>
  <parameter name = "pver" value = "4.4"/>
  <test name="Test">
    <classes>
      <class name="appium.appium"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
时间: 2024-11-08 22:52:48

appium精简教程的相关文章

转帖-[教程] Win7精简教程(简易中度)2016年8月-0day

[教程] Win7精简教程(简易中度)2016年8月 0day 发表于 2016-8-19 16:08:41  https://www.itsk.com/thread-370260-1-1.html 本帖最后由 0day 于 2016-8-23 20:15 编辑 1.资源准备1.1 Win7 SP1 专业版原版(版本不限)1.2 Win7 SP1 便捷更新包2016年4月 本帖隐藏的内容 KB3020369 32位:https://download.microsoft.com/download/

appium简明教程(11)——使用resource id定位

上一节乙醇带大家了解了appium的定位策略.实际上appium的控件定位方式是完全遵守webdriver的mobile扩展协议的. 这一节将分享一下如何使用resource id来定位android策略. 什么是resource id,这个不属于本文的范畴,大家可以点这里了解. 我们可以有两种方式来使用resource id进行定位: 使用 findElement(By.id("resourceId")) 的方式.这也是原生的webdriver定义的方法,不过竟然在appium的官方

appium简明教程(4)——appium client的安装

appium client是对webdriver原生api的一些扩展和封装.它可以帮助我们更容易的写出用例,写出更好懂的用例. appium client是配合原生的webdriver来使用的,因此二者必须配合使用缺一不可. 从本节开始,教程的内容将涵盖3个语言,ruby/python/java. 安装appium client ruby篇(一定要在线安装) ruby的appium client叫做appium lib,为什么是这样就不解释了,总之是历史原因. 首先update rubygem和

appium简明教程(转)

转:http://www.yangyanxing.com/article/1266.html appium简明教程(1)——appium和它的哲学世界 什么是appium? 下面这段介绍来自于appium的官网. Appium is an open-source tool you can use to automate mobile native, mobile web, and mobile hybrid applications on iOS and Android platforms. “

appium简明教程(5)——appium client方法一览

appium client扩展了原生的webdriver client方法 下面以java代码为例,简单过一下appium client提供的适合移动端使用的新方法 resetApp() getAppString() sendKeyEvent() currentActivity() pullFile() pushFile() pullFolder() hideKeyboard() runAppInBackground() performTouchAction() performMultiTouc

appium简明教程(10)——控件定位基础

狭义上讲,UI级的自动化测试就是让机器代替人去点来点去的过程. 但机器去点什么(点上面还是点左边),怎么点(是长按还是轻触),这些东西是必须由代码的编写者所指示清楚的. 控件定位就是解决机器点什么的问题的. 一般说来,我们可以这样告诉机器:去点登陆按钮. 机器很笨,它并不知道什么是登陆按钮.因为登陆按钮是自然语言的描述. 如果你让一个人去点登陆按钮,那么他其实也是要经过一系列的脑补以后才可以做这件事的. 这个脑补的过程还原如下: 这个一定是个按钮 这个按钮一定在被测的应用上 这个按钮大概上面有登

appium简明教程(6)——启动appium及android模拟器

一般情况下,我们都从命令行启动appium. windows下,dos命令窗口输入 appium 如果该命令报错,那么请重装appium npm install -g appium 如果安装出错,请自行更换npm源. npm -g --registry http://registry.cnpmjs.org install appium 然后请打开android的模拟器,如果没有请新建一个虚拟设备 下面的代码以启动android原生的计算器程序为例 ruby篇 require 'appium_li

appium简明教程(9)——如何获取android app的Activity

有时候在appium的Desired Capabilities中需要指定被测app的appActivity,下面的方法可能会对你有所帮助. 方法一 如有你有待测项目的源码,那么直接查看源码就好.如果没有,那么请联系有源码的同学,这是推荐方法. 本文版权归乙醇所有,欢迎转载,但请注明作者与出处,严禁用于任何商业用途 方法二 如果你没有代码,那么可以反编译该app. 这里将用到2个工具,分别是dex2jar和jd-gui.你可以在这里下载目前为止的最新版本以及示例apk. 我们以工具包里的Conta

appium简明教程(8)——那些工具

那片笑声让我想起我的那些tool 在我生命每个角落静静为我开着 我曾以为我会永远守在她身旁 今天我们已经离去在人海茫茫 她们都老了吧 都更新换代了吧 幸运的是我曾陪她们开发 啦…… 想她 啦…… 她还更新吗 啦…… 去呀 她们已经被风吹走散落在天涯 正所谓工欲善其事必先利其器,对于appium实战android来说,下面的这些工具(以windows系统为例)是必须熟练使用的. monitor.bat(hierarchyviewer.bat已经不赞成继续使用了) 该文件位于your_andriod