Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the implementations of android automated test deployment and running Shell scripts

Shulou Source: shulou.com Published: 2022-06-03 09:21:23 09月30日 Update

This article introduces the knowledge of "how to implement android automated test deployment and run Shell scripts". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The main process is:

1. Check whether the simulator of the specified port is already running, if so, turn it off

two。 Create a simulator

3. Start the simulator

4. Use the ant build package to determine whether it is successful by checking for apk in the bin directory

5. Check whether the simulator of the specified port is running properly.

6. Install apk with adb

7. Start apk's activity and let the program run on its own

8. Use the shell ps loop to detect whether the program has exited, and if so, check whether sdcard has successfully generated the report file

9. If the report file is incomplete, that is, the program is aborted / crashed, restart activity

10. Detect whether or not to time out

The code is as follows:

#! / bin/bash

# by hursing 2013-10-28

# specify port, which does not interfere with other running simulators. According to sdk documents, port can be between 5554 and 5584, preferably an even number

PortNumber=5578

EmulatorPID= `ps-ef | grep "emulator" | grep "port $portNumber" | awk'{print $2;}'`

If [- n "$emulatorPID"]; then

Echo "close emulator window"

Kill-9$ emulatorPID

Fi

# to create a simulator, the force option can overwrite the existing simulator, that is, the sdcard content can also be erased. Echo no is the answer that no custom hardware configuration is required.

Echo "create avd"

EmulatorName= "Test"

Echo no | android create avd-- name $emulatorName-- force-- target android-17-- sdcard 300m-- abi armeabi-v7a

# start the simulator, and the port option specifies the tcp port. & is the execution of another process

Echo "start avd"

Emulator-avd $emulatorName-port $portNumber &

# No certificate signature, 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 scripts until offline is not displayed

Echo "check emulator ready..."

EmulatorSerialNumber= "emulator-$portNumber"

Ready= `adb devices | grep "$emulatorSerialNumber" `

If [- z "$ready"]; then

Echo "emulator can 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 10

Fi

Fi

# when adb can be installed, the simulator may not have been initialized, but it will fail, so check the error and try it 3 times.

Echo "install apk"

Let maxTryTimes=3

While [$maxTryTimes-gt 0]; do

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 10 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 Compact 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 at"

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 start-W-n "$packageName/$activityName"

Else

Echo "app exit normally"

Break

Fi

Fi

Done

Echo "close emulator window."

Kill-9 `ps-ef | grep "emulator" | grep "port $portNumber" | awk'{print $2;}'`

If [$maxTimeLimit-eq 0]; then

Echo "Test timeout!"

Echo "$report"

Exit 9

Else

Echo "Test Finished!"

Echo

Echo "$report"

Fi

One thing to note in the java code is that if the program crashes and the "has stopped" dialog box pops up, then ps can still find it and call it yourself.

The code is as follows:

Thread.setDefaultUncaughtExceptionHandler

, directly System.exit (0) in handler.

That's all for "how to automate android test deployment and run Shell scripts". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Simulator run program test script code content port check test automation success report file more knowledge or practical learned next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Docker OPPO Reno macOS