2014-09-30 325 views
4

因此,我有一個UIAutomator測試腳本,在執行測試期間我希望它運行一些adb命令。我該怎麼做呢 ?如何從UIAutomator運行「adb命令」

我在想有沒有辦法在UI automator類中調用腳本?或者有沒有辦法直接執行「adb命令」?

這是我的代碼:

package com.uia.example.my; 


import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

import com.android.uiautomator.core.UiObjectNotFoundException; 
import com.android.uiautomator.testrunner.UiAutomatorTestCase; 

public class ADBCommandTest extends UiAutomatorTestCase{ 
    public void testDemo() throws UiObjectNotFoundException{ 

     System.out.println("Entering the shell script"); 
     try { 
      ProcessBuilder pb = new ProcessBuilder("./run.sh"); 
      Process p = pb.start(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        System.out.println(line); 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

    } 

} 

這是錯誤我得到:

INSTRUMENTATION_STATUS: current=1 
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner 
INSTRUMENTATION_STATUS: class=com.uia.example.my.ADBCommandTest 
INSTRUMENTATION_STATUS: stream= 
com.uia.example.my.ADBCommandTest: 
INSTRUMENTATION_STATUS: numtests=1 
INSTRUMENTATION_STATUS: test=testDemo 
INSTRUMENTATION_STATUS_CODE: 1 
Entering the shell script 
java.io.IOException: Error running exec(). Command: [./run.sh] Working Directory: null Environment: [EMULATED_STORAGE_SOURCE=/mnt/shell/emulated, SECONDARY_STORAGE=/storage/sdcard1, ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/storage/emulated/legacy, CUSTOM_NOTIFICATION_MANAGER_SERVICE=com.amazon.device.notification.AmazonNotificationManagerService, LD_PRELOAD=/vendor/lib/libNimsWrap.so, ASEC_MOUNTPOINT=/mnt/asec, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/telephony-msim.jar:/system/framework/DeviceClientMetrics-Api.jar:/system/framework/acos-util.jar:/system/framework/acosservices.jar:/system/framework/acos-core.jar:/system/framework/icu4j.jar:/system/framework/i18n-utilities.jar:/system/framework/amazon.core.jar:/system/framework/amazon-search.jar:/system/framework/qcmediaplayer.jar:/system/framework/WfdCommon.jar:/system/framework/oem-services.jar:/system/framework/dolby_ds.jar:/system/framework/com.amazon.headtracking.jar:/system/framework/com.amazon.motiongestures.jar:/system/framework/com.amazon.headtracking.filter.jar:/system/framework/eaclibrary.jar:/system/framework/euclidlibrary.jar:/system/framework/qcom.fmradio.jar, ANDROID_SOCKET_adbd=10, EMULATED_STORAGE_TARGET=/storage/emulated, RANDOM=20395, AMAZON_COMPONENT_LIST=com.amazon.geo.maps:com.amazon.platform;com.amazon.device.messaging:com.amazon.platform, _=/system/bin/app_process, ANDROID_DATA=/data/local/tmp, ANDROID_ROOT=/system, LD_LIBRARY_PATH=/vendor/lib:/system/lib, ANDROID_STORAGE=/storage, ANDROID_ASSETS=/system/app, CLASSPATH=/system/framework/android.test.runner.jar:/system/framework/uiautomator.jar::/data/local/tmp/ADBCommandTest.jar, PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, GCOV_PREFIX=/data/gcov, LOOP_MOUNTPOINT=/mnt/obb, run_base=/data/local/tmp, base=/system, VENDOR_SERVICES_LIST=tyto.vibrator.VibratorService:com.amazon.client.metrics.AndroidMetricsFactoryImpl:amazon.communication.SingleRequestResponseManagerService:amazon.communication.CommunicationManagerService:com.amazon.device.nos.NetworkOptimizationManagerImpl:amazon.profile.ProfileManager:com.amazon.communication.ByteAccountantService, AMAZON_EXTRA_RESOURCE_LIST=framework/amazon-res.apk:vendor/amazon/framework/framework-res.apk:framework/amazon-b-res.apk:framework/euclid-res.apk, ANDROID_PROPERTY_WORKSPACE=9,66048] 
    at java.lang.ProcessManager.exec(ProcessManager.java:211) 
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:195) 
    at com.uia.example.my.ADBCommandTest.testDemo(ADBCommandTest.java:17) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at junit.framework.TestCase.runTest(TestCase.java:168) 
    at junit.framework.TestCase.runBare(TestCase.java:134) 
    at junit.framework.TestResult$1.protect(TestResult.java:115) 
    at junit.framework.TestResult.runProtected(TestResult.java:133) 
    at junit.framework.TestResult.run(TestResult.java:118) 
    at junit.framework.TestCase.run(TestCase.java:124) 
    at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAutomatorTestRunner.java:144) 
    at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutomatorTestRunner.java:87) 
    at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.java:90) 
    at com.android.commands.uiautomator.Launcher.main(Launcher.java:83) 
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method) 
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.io.IOException: No such file or directory 
    at java.lang.ProcessManager.exec(Native Method) 
    at java.lang.ProcessManager.exec(ProcessManager.java:209) 
    ... 17 more 
INSTRUMENTATION_STATUS: current=1 
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner 
INSTRUMENTATION_STATUS: class=com.uia.example.my.ADBCommandTest 
INSTRUMENTATION_STATUS: stream=. 
INSTRUMENTATION_STATUS: numtests=1 
INSTRUMENTATION_STATUS: test=testDemo 
INSTRUMENTATION_STATUS_CODE: 0 
INSTRUMENTATION_STATUS: stream= 
Test results for WatcherResultPrinter=. 
Time: 0.042 

OK (1 test) 


INSTRUMENTATION_STATUS_CODE: -1 

我把run.sh腳本文件在我的類文件是同一個地方。請幫忙。

回答

1

以下是可用的代碼。在運行UiAutomator測試之前,我已將腳本文件「command.sh」複製到外部SD卡。

該代碼不需要任何root權限。

package ui; 
import java.io.File; 
import java.io.IOException; 
import android.os.RemoteException; 
import com.android.uiautomator.core.UiDevice; 
import com.android.uiautomator.core.UiObjectNotFoundException; 
import com.android.uiautomator.testrunner.UiAutomatorTestCase; 

public class SampleCode extends UiAutomatorTestCase { 
    public void testHome() throws RemoteException, 
     UiObjectNotFoundException { 

    // press Home 
    UiDevice mydevice = getUiDevice(); 
    if (!mydevice.isScreenOn()) { 
     mydevice.wakeUp(); 
    } 
    mydevice.pressHome(); 

    // Script path 
    String filePath = "/mnt/sdcard_ext/command.sh"; 
    File command = new File(filePath); 
    command.setExecutable(true); 

    // Run command 
    Process p = null;  
    try { 
     Runtime.getRuntime().exec(
       "/system/bin/sh /mnt/sdcard_ext/command.sh"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (p != null) 
      p.destroy(); 
    } 

} 

}

這裏是 「command.sh」 的內容,輸出將被寫入到文件上的外部SD卡

SDCARD="/mnt/sdcard_ext/" 
LOG="$SDCARD/testlog.txt" 
logcat -d >> $LOG 
1

試試這個方法 -

public String shell(String cmd) { 

    String s = null; 
    try { 
     Process p = Runtime.getRuntime().exec(cmd); 

     BufferedReader stdInput = new BufferedReader(new InputStreamReader(
       p.getInputStream())); 

     BufferedReader stdError = new BufferedReader(new InputStreamReader(
       p.getErrorStream())); 

     // read the output from the cmd 
     String result = ""; 
     while ((s = stdInput.readLine()) != null) { 
      result = result + s + "\n"; 
     } 

     // read any errors from the attempted comd 
     while ((s = stdError.readLine()) != null) { 
      System.out.println(s); 
     } 
     return result; 

    } catch (IOException e) { 
     System.out.println("exception here's what I know: "); 
     e.printStackTrace(); 
     return "Exception occurred"; 
    } 
} 

現在撥打電話

String ls= Shell("ls"); 
+0

我該如何運行** am **命令? – 2017-09-06 14:21:11

7

我以前用於使用運行時方法在測試setUp()/ tearDown()中注入shell命令,但當我切換到UIAutomator 2.0和Gradle時,它停止工作。

如果你細使用API​​ 21(棒棒糖)及以上的,谷歌提供這個API我們:

InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("pm clear " + Constants.PACKAGE_NAME); 

這個方法對我的作品。來源:

http://developer.android.com/reference/android/app/UiAutomation.html#executeShellCommand(java.lang.String)