[PYTHON] Create an environment for test automation with AirtestIDE (Tips)

Introduction

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.

environment

macOS 10.13.6 Airtest IDE 1.2.2(Python) Android 9

script

Import scripts used in common

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

Processing to wait for processing

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.

Output log

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

Post your results to Slack


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

Command line

Run from command line

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.

Get the report

/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.

Execute by connecting multiple units

$ 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)

in conclusion

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

Create an environment for test automation with AirtestIDE (Tips)
Create an environment with virtualenv
Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)
Create execution environment for each language with boot2docker
Building an Anaconda environment for Python with pyenv
Various commands for building an environment with Apache
Try building an environment for MayaPython with VisualStudioCode
Test automation for work
Creating an environment for OSS-DB Silver # 1_Create a Linux environment (CentOS7 virtual environment) with VirtualBox/Vagrant
[Linux] WSL2 Build an environment for laravel7 with Ubuntu 20.04
Building an environment for natural language processing with Python
Create an environment for MkDocs on Amazon Linux (attempted)
Create an OpenAI Gym environment with bash on Windows 10
Create an API with Django
Rebuilding an environment for machine learning with Miniconda (Windows version)
Minimum Makefile and buildout.cfg to create an environment with buildout
Create a Python execution environment for Windows with VScode + Remote WSL
Create an alias for Route53 to CloudFront with the AWS API
Tips for running Go with docker
Tips for importing macOS-optimized TensorFlow in an Apple M1 chip environment
Create a virtual environment with Python!
Create an Excel file with Python3
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Created an environment for Anaconda & Jupyter
Create an age group with pandas
Create an arbitrary machine learning environment with GCP + Docker + Jupyter Lab
Create a USB boot Ubuntu with a Python environment for data analysis
Create an application by classifying with Pygame
[Python] Building an environment with Anaconda [Mac]
Build an environment for Blender built-in Python
Create an image processing viewer with PySimpleGUI
Note when creating an environment with python
Tips for dealing with binaries in Python
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
Quickly create an excel file with Python #python
Create Python + uWSGI + Nginx environment with Docker
Create a virtual environment with Python_Mac version
Tips for using python + caffe with TSUBAME
Tips for plotting multiple lines with pandas
Crawl Follower for an account with Instagram
Create an OpenCV3 + python3 environment on OSX
[Python] Quickly create an API with Flask
Test automation starting with L-Chika (3) Oscilloscope integration
Create an add-in-enabled Excel instance with xlwings
Create an English word app with python
Building an environment for "Tello_Video" on Raspbian
Create an upgradeable msi file with cx_Freeze
Building an environment for "Tello_Video" on Windows
Notes from installing Homebrew to building an Anaconda environment for Python with pyenv
Quickly create an environment where you can play with Raspberry Pi 4 (target 1 hour)
I tried to create an environment to check regularly using Selenium with AWS Fargate
[Building a CI environment in 2 hours] Procedure for building a Python Web server with CircleCI and passing an HTTP communication test
Create a Vim + Python test environment in 1 minute
For beginners to build an Anaconda environment. (Memo)
Create test data like that with Python (Part 1)
Create an app that guesses students with python
Create an academic society program with combinatorial optimization
(Test automation) Nesting images used for image recognition
Building an environment for executing Python scripts (for mac)
Create a LINE BOT with Minette for Python