Implement Android automated test deployment and run shell script sharing _linux Shell

Source: Internet
Author: User

My configuration is the Linux, android4.2.2 SDK. The implementation details are in the code comments, and the variable name and echo content are part of the description.

The main process is:

1. The simulator that detects whether the port is specified is already running, if it is closed
2. Create simulator
3. Start simulator
4. Using the ant build package, check to see if there are any apk in the bin directory to determine whether the success
5. Check if the emulator for the specified port is functioning properly
6. Installation of apk with ADB
7. Start APK activity, let the program run itself
8. Use shell PS Loop to detect whether the program has exited, if exited, check whether SDcard is successfully generated report file
9. If the report file is incomplete, that is, the procedure is aborted/crashed, restart the activity
10. Detect if timeout

Copy Code code as follows:

#!/bin/bash
# by Hursing 2013-10-28
# Specify port to not interfere with other running emulators. Depending on the SDK documentation, port can be between 5554 and 5584, preferably even
portnumber=5578
Emulatorpid= ' Ps-ef | grep "Emulator" | grep "Port $portNumber" | awk ' {print $} ' `
If [-N "$emulatorPID"]; Then
echo "Close Emulator Window"
Kill-9 $emulatorPID
Fi

# Create the simulator, the Force option can overwrite the existing emulator, that is, sdcard content is erased. Echo No is the answer does not require a custom hardware configuration
echo "Create AVD"
Emulatorname= "Test"
Echo No | Android Create AVD--name $emulatorName--force--target android-17--sdcard 300M--abi armeabi-v7a

# Start Simulator, port option to specify TCP port. & is another process executing
echo "Start AvD"
EMULATOR-AVD $emulatorName-port $portNumber &

# no certificate signed, so it is debug
echo "Build apk"
Android Update project-p.
Ant Debug
If [$?-ne 0]; Then
echo "Build failed, try again now ..."
Ant Clean
Ant Debug
If [$?-ne 0]; Then
echo "Can not build apk!"
Exit 5
Fi
Fi
Apkfile= ' Find./bin/*.apk | Grep-v "unaligned"
If [-Z "$apkFile"]; Then
echo "No apk file generated!"
Exit 7
Fi

# Wait-for-device can block the script until the offline
echo check emulator ready ... "
emulatorserialnumber=" emulator-$ PortNumber "
ready= ' adb devices | grep" $emulatorSerialNumber "'
If [-Z" $ready "]; then
 echo" Emulator C A not launch. "
 exit 3
Else
 ready= ' adb devices | grep ' $emulatorSerialNumber. *offline '
 if [-N ' $ready " ]; Then
  echo "Wait-for-device"
  adb-s $emulatorSerialNumber wait-for-device
   sleep
 fi
Fi

# when the ADB can be installed, the simulator may not have been initialized to complete, or will fail, so to detect error, try 3 times.
echo "Install APK"
Let maxtrytimes=3
While [$maxTryTimes-GT 0]; Todo
result= ' adb-s $emulatorSerialNumber install $apkFile ' # ' Ant installd ' can not specify device
Check= ' echo ' $result | Grep-o "Error" '
If [-Z "$check"]; Then
echo "Successfully install APK"
Break
Else
echo "$result"
echo "Install failed, try again after sleeping seconds"
Sleep 10
Let Maxtrytimes-=1
Fi
Done
If [$maxTryTimes-eq 0]; Then
echo "Can not install apk to emulator."
Exit 4
Fi

echo "Start Mainactivity"
Activityname= "Com.hursing.MainActivity"
Packagename= ' Cat Androidmanifest.xml | Grep-o "package=\". *\ "" | Sed ' s/package= '//;s/'//'
Adb-s $emulatorSerialNumber Shell am start-w-N "$packageName/$activityName"

Heartbeat=10
Echo waiting for test finished.check every $heartBeat seconds. "
resultfile= "/sdcard/result.txt"
successflag= "end in"
Sleep $heartBeat
Let maxtimelimit=60*10
While [$maxTimeLimit-GT 0]; Do
 running= ' adb-s $emulatorSerialNumber Shell PS | grep $packageName '
 if [-n ' $running]; then
  let maxtimelimit-= $heartBeat
  echo "Waiting, sleep another $heartBeat seconds ..."
  sleep $heartBeat
 else
  report= ' adb-s $emulatorSerialNumber Shell Cat ' $ Resultfile "'
  check= ' echo" $report | grep-o "$successFlag"
  if [-Z "$check"]; then
   echo "app crash, run activity again ..."
   adb-s $emulatorSerialNumber Shell am STA Rt-w-N "$packageName/$activityName"
  else
   echo "app exit normally"
 & Nbsp; break
  fi
 fi
Done

echo "Close emulator window."
Kill-9 ' Ps-ef | grep "Emulator" | grep "Port $portNumber" | awk ' {print $} ' `

If [$maxTimeLimit-eq 0]; Then
echo "Test timeout!"
echo "$report"
Exit 9
Else
echo "Test finished!"
Echo
echo "$report"
Fi

In the Java code there is a point to note that if the program crashes, pop-up "has Stopped" dialog box, then PS can still be found, to call themselves

Copy Code code as follows:

Thread.setdefaultuncaughtexceptionhandler

, in Handler direct system.exit (0) just do.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.