Recently, I started test automation of smartphone apps (games) with AirtestIDE. So I would like to spell out what I did to create the script and environment I actually created.
macOS 10.13.6 Airtest IDE 1.2.2(Python) Android 9
from airtest.core.api import using
using("common.air")
from common import *
If I was wondering what to do, it was written in the official document https://airtest.readthedocs.io/en/latest/README_MORE.html#import-from-other-air
import sys
def waitForExists(obj, timeout = 60):
for t in range(timeout):
if (exists(obj)): return 1
elif t == timeout - 1:
print("timeout")
sys.exit()
sleep(1)
Wait for processing until obj is found
I haven't made the exception handling of timeout yet.
raise waitforobjecttimeouterror("Processing timed out")
I want to look like this
def waitAndTouch(obj, timeout = 60):
for t in range(timeout):
if (exists(obj)):
sleep(1)
touch(obj)
return 1
elif t == timeout - 1:
print("timeout")
sys.exit()
sleep(1)
Wait until you find obj, then tap
def swipeForExists(obj, obj2, vector, dulation = 8, maxCount = 10):
for c in range(maxCount):
if (exists(obj)): break
elif c == maxCount - 1:
print("maxCount")
sys.exit()
elif (exists(obj2)):
swipe(obj2, vector, duration = dulation)
Swipe obj2 until you find obj
def tapForExists(obj, obj2, timeout = 60):
for t in range(timeout):
if (exists(obj)):
sleep(1)
touch(obj)
return 1
elif t == timeout - 1:
print("timeout")
sys.exit()
elif (exists(obj2)): touch(obj2)
sleep(1)
Keep tapping obj2 until obj is found (tap when obj is found)
def tapForNotExists(obj, obj2, timeout = 60):
for t in range(timeout):
sleep(1)
if (not exists(obj)): return 1
elif t == timeout - 1:
print("timeout")
sys.exit()
elif (exists(obj2)): touch(obj2)
Keep tapping obj2 until you can't find obj
def tapForNotExistsXY(obj, x, y, timeout = 60):
for t in range(timeout):
sleep(1)
if (not exists(obj)): return 1
elif t == timeout -1:
print("timeout")
sys.exit()
else:
touch(v=(x,y))
Keep tapping on specific coordinates until obj is missing
def tapIfExists(*args):
n = 0
for i in args:
n += 1
if (exists(args[0])):
sleep(1)
if (n == 1):
touch(args[0])
elif (n == 2):
touch(args[1])
When there is one argument: Tap if there is a target obj When there are two arguments: If there is a target obj, tap obj2
With this much work, most of the things I want to do have become sufficient.
import datetime
def log_ok(file, msg):
now = datetime.datetime.now()
#for airtestIDE
file.write(now.strftime("%Y/%m/%d %H:%M:%S") + " [info] " + msg + " OK" + chr(10))
#for airtest
#file.write(now.strftime("%Y/%m/%d %H:%M:%S") + " [info] " + msg.decode("utf-8") + " OK".decode("utf-8") + chr(10))
file.flush()
It seems that the character code processing is different between AirtestIDE and Airtest
import codecs
file = codecs.open("<Log file name>", "w", "utf-8")
log_ok(file, "Automatic test start")
###for IDE
install("../<App>.apk")
###for CLI
#install("<App>.apk")
log_ok(file, "Step1 -Installation")
start_app("<App id>")
#(Omitted)
log_ok(file, "Automatic test end")
stop_app("<App id>")
file.close()
Example of use. In addition, it seems that the path of the application is slightly different when executing from the IDE and when executing from the CLI IDE ... The executed air folder becomes the home CLI ... The place where you executed it becomes your home
import json
import requests
url = "https://slack.com/api/chat.postMessage"
token = "<token>"
channel = "<channel>"
message = "Of log output" + chr(10) + "It is a sample"
body = {
'token' : token,
'channel' : channel,
'text' : message,
'as_user' : 'true'
}
requests.post(url, data=body)
I'm thinking of importing the output log file and posting it to Slack. https://api.slack.com/methods/chat.postMessage
adb shell input keyevent KEYCODE_WAKEUP
cd $PROJECT_HOME
/Applications/AirtestIDE.app/Contents/MacOS/AirtestIDE runner "<project>.air" --device Android:/// --log log
Image to start by hitting the shell from Jenkins etc.
/Applications/AirtestIDE.app/Contents/MacOS/AirtestIDE reporter "<project>.air" --log_root log --export exp
You can get richer reports with Airtest IDE than with airtest It would be nice to set the output destination of the report to DocumentRoot of apache so that it can be referenced from the intranet.
$ airtest run argo.air --device Android://127.0.0.1:5037/<serialno>
If multiple smartphone terminals are connected via USB, you can execute multiple devices at the same time by specifying `<serialno>`
, which is listed by the adb devices
command (. I haven't tried it with Airtest IDE yet)
Did you find any references? There were some points that I stumbled upon when I actually tried to implement the script, so I hope it helps.
Recommended Posts