zoukankan      html  css  js  c++  java
  • Android uiautomator gradle build system

    This will guide you through the steps to write your first uiautomator test using gradle as it build system.

    What is gradle?

    “Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build.”

    For more visit gradle page.

    What is uiautomator?

    “The uiautomator testing framework lets you test your user interface (UI) efficiently by creating automated functional UI testcases that can be run against your app on one or more devices.”

    For more visit uiautomator page.

    Java source code

    Create a directory named bluetooth:

    $ mkdir bluetooth

    Create a java project layout:

    $ cd bluetooth
    $ mkdir -p src/main/java/com/github/wiliamsouza/bluetooth

    Create a java file BluetoothTest.java with the following content:

    package com.github.wiliamsouza.bluetooth;
    
    import java.io.IOException;
    
    import com.android.uiautomator.core.UiObject;
    import com.android.uiautomator.core.UiObjectNotFoundException;
    import com.android.uiautomator.core.UiScrollable;
    import com.android.uiautomator.core.UiSelector;
    import com.android.uiautomator.testrunner.UiAutomatorTestCase;
    
    public class BluetoothTest extends UiAutomatorTestCase {
    
        public void setUp() throws UiObjectNotFoundException, IOException {
            getUiDevice().pressHome();
            String packageName = "com.android.settings";
            String component = packageName + "/.Settings";
            String action = "am start -a android.intent.action.MAIN -n ";
    
            // start settings application
            Runtime.getRuntime().exec(action + component);
    
            UiObject settingsUi = new UiObject(new UiSelector().packageName(packageName));
            assertTrue("Application settings not started", settingsUi.waitForExists(5000));
        }
    
        public void testBluetooth() throws UiObjectNotFoundException {
            UiScrollable scroll = new UiScrollable(new UiSelector().scrollable(true));
            UiObject layout = scroll.getChildByText(new UiSelector().className(android.widget.LinearLayout.class.getName()),"Bluetooth", true);
            UiObject switchBluetooth = layout.getChild(new UiSelector().className(android.widget.Switch.class.getName()));
            assertTrue("Unable to find bluetooth switch object", switchBluetooth.exists());
            switchTo(switchBluetooth, "Bluetooth");
        }
    
        private void switchTo(UiObject switchObject, String name) throws UiObjectNotFoundException {
            // Before start test ensure switch is off
            if (switchObject.isChecked()) {
                switchObject.click();
                sleep(3000);
            }
            
            switchObject.click();
            sleep(3000);
           	assertTrue("Unable to turn " + name + " on", switchObject.isChecked());
           	
        	switchObject.click();
            sleep(3000);
           	assertFalse("Unable to turn " + name + " off", switchObject.isChecked());
        }
    }
    

    Gradle

    Create a build.gradle with the following content:

    apply plugin: 'java'
    apply plugin: 'idea'
    
    sourceCompatibility = 1.5
    targetCompatibility = 1.5
    
    version = '0.1'
    
    project.ext {
        dexDir = new File('build/dex')
        distDir = new File('./dist')
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile fileTree(dir: androidSdkHome + '/platforms/' + androidSdkTarget, include: '*.jar')
        compile group: 'junit', name: 'junit', version: '4.11'
    }
    
    jar {
        doLast {
            tasks.dex.execute()
        }
    }
    
    task dex(dependsOn: jar, type:Exec) {
        println 'Building dex...'
        project.dexDir.mkdirs()
        workingDir '.'
        commandLine androidSdkHome + '/' + androidSdkBuildToolsDir + '/' + 'dx', '--dex', '--no-strict', '--output=' + buildDir +'/dex/' + project.name + '.jar', jar.archivePath
        doLast {
            tasks.dist.execute()
        }
    }
    
    task dist(dependsOn:dex, type:Copy) {
        project.distDir.mkdirs()
        from(project.dexDir)
        into(project.distDir)
        include('*.jar')
    }
    

    Configure

    Now lets configure the build system, Create a file gradle.properties containing:

    androidSdkHome=/home/wiliam/bin/android-sdk-linux
    androidSdkTarget=android-18
    androidSdkBuildToolsDir=build-tools/18.1.1
    
    • androidSdkHome – Point to the root directory of android SDK.
    • androidSdkTarget – Is the target, points to a directory inside platforms.
    • androidSdkBuildToolsDir – Location of dx program.

    Build

    $ gradle build

    Install

    $ adb push dist/bluetooth.jar /data/local/tmp/

    Run

    $ adb shell uiautomator runtest bluetooth.jar -c com.github.wiliamsouza.bluetooth.BluetoothTest
  • 相关阅读:
    .net Remoting学习笔记(一) 中庸
    培训是一种乐趣(2)
    ExtJS实战(10)项目总结
    ExtJS实战(4)struts
    ExtJS实战(7)登陆
    ExtJS实战(5)dwr
    让老师崩溃的回答-程序员的经典笑话
    ExtJS实战(9)疑难杂症分析
    ExtJS实战(6)extjs+json
    ExtJS实战(8)CRUD+分页+复杂查询+排序
  • 原文地址:https://www.cnblogs.com/rosepotato/p/4704768.html
Copyright © 2011-2022 走看看