Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report