The original article is reprinted from “Liu Yue’s technology blog” v3u.cn/a_id_196

Python automation is probably one of the hottest topics of the year. In July, the hiring market for Python automation continues unabated, so what can Python automation do for us?

First, Python automation can avoid repetitive work for skilled workers. For relatively complete and mature software, every new version, whether large or small, will have almost or exactly the same functionality and interface as the previous version, but the so-called upward compatibility. You can’t introduce new features without testing old features that were tested when the last version was released, so this part of the feature is particularly well suited for automated testing, where tests can test every feature.

Second, Python automation can help us improve our testing efficiency: Such as the development cycle of a project to only a few short month, and during the test is on a weekly basis to release a version for the tester to test, a system is out to thousands of tens of thousands of function point, manual testing is very time-consuming and tedious, this will make the test efficiency is low, and automated process just help us to improve the test efficiency.

So for mobile App test field, if a new application releases, QA personnel are faced with the challenge is how to deal with thousands of models on the market compatibility test, there is no doubt that this is a very huge task, not to mention some engineering machine on the market basically procurement, such as Google Nexus and Pixel series mobile phone, Therefore, this time, we will try to use Genymotion simulator and Docker to build Appium container under Win10 system, and realize the automatic test of thousands of models in a short time.

First of all, why do you choose Genymotion as an Android emulator? Of course, there are other emulators available in China, such as Blue God. However, As the leader in the Android emulator field, Genymotion has a very fast startup and running speed, which not only supports multiple Android versions, Genymotion has its own downside, of course, is that the client version of the kernel x86 architecture does not support ARM framework applications, but the cloud service already supports ARM architecture. This is certainly a big plus in the simulator world.

Enter the Genymotion registration page: www-v1.genymotion.com/account/cre…

After the success of the registration, pay attention to the mailbox need to activate, then enter the download page: www.genymotion.com/download/

If you do not have VirtualBox installed, select with VirtualBox, otherwise select without VirtualBox. If you do not have VirtualBox installed, select With VirtualBox. If you do not have VirtualBox installed, select Without VirtualBox. Simply select the Genymotion ontology.

After the installation is successful, log in with the account you just registered and select Personal Use:

Then select the models that need to be tested to create:

After creating a VM for a mobile phone, you need to perform certain Settings to ensure that the VM runs properly.

Select Ohter Linux-64 from Virtualbox Settings General options

Also select the promiscuous mode of the network option: Allow virtual machines. This will be used later when linking to virtual machines:

Finally, for security purposes, it is a good idea to turn off the hyper-V feature on the host, open the terminal with administrator privileges, and execute commands

bcdedit /set hypervisorlaunchtype off
Copy the code

Restart the computer and start the phone emulator. If the Android interface appears, the configuration is successful:

Next, let’s configure Android ADB. What is Android ADB? ADB is the Android Debug Bridge, which is a very common tool for developing or using Android. You can use the command line to control your phone from your computer via A USB connection to your Android phone.

Here we mainly through the ADB command to get the IP virtual machine terminal, direct download zip files: dl.google.com/android/rep…

Decompress it to the root directory of drive C, C:\platform-tools_r31.0.2- Windows \platform-tools

Then configure the directory as a global environment variable so that it can be accessed directly from within the terminal:

C:\Users\liuyue> ADB --version Android Debug Bridge version 1.0.41 version 31.0.2-7242960 Installed as C: \ platform - tools_r31. 0.2 Windows \ platform - the tools \ adb exeCopy the code

Now run the device list command:

C:\Users\liuyue> ADB Devices List of Devices Attached 192.168.42.103:5555 deviceCopy the code

As you can see, the virtual machine we just booted appears in the device list. The connect command is used to connect to the virtual machine, which is almost the same as the real machine:

C:\Users\liuyue> ADB Devices List of Devices Attached 192.168.42.103:5555 DEVICE C:\Users\liuyue> ADB Connect 192.168.42.103:5555 already connected to 192.168.42.103:5555 C:\Users\liuyue>Copy the code

Docker’s main task is to use the container to run the Appuim automation script, so as to avoid the cumbersome Appuim installation and configuration process, of course, your computer must be installed in advance, if not, please move: Play with DockerToolBox in Windows 10 and change domestic image source (various gods pit)

Then download Appium base image, Dockerhub on the image of good and evil people mixed up, here or recommend the official version: hub.docker.com/r/appium/ap…

Execute command:

docker pull appium/appium
Copy the code

Viewing a mirror:

liuyue@DESKTOP-NVU6CCV MINGW32 ~ $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE appium/appium latest 70f3d328b949 6 weekes line 1.55 GBCopy the code

Next, start the container:

docker run --privileged -d -p 4723:4723 --name appium appium/appium
Copy the code

Here we start the Appium container, map the port to 4723, privileged and give it root privileges, -d background execution.

Then check the container health status:

liuyue@DESKTOP-NVU6CCV MINGW32 ~ $ docker run --privileged -d -p 4723:4723 --name appium appium/appium a2e8f11fdf7c561b075b563dfcc1efb6e5381e78dc3d4435a89cf8f97be52f6d liuyue@DESKTOP-NVU6CCV MINGW32 ~ $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a2e8f11fdf7c appium/appium "/bin/sh -c '/root/w..." 7 minutes ago Up 19 seconds 4567/ TCP, 0.0.0.0:4723->4723/ TCP appiumCopy the code

At this point, we can use the ADB service in the Docker container to operate:

Docker exec-it appium ADB Connect 192.168.42.103:5555Copy the code

As you can see, Docker successfully connected to the phone pair:

liuyue@DESKTOP-NVU6CCV MINGW32 ~ $docker exec-it appium adb connect 192.168.42.103:5555 connected to 192.168.42.103:5555Copy the code

Common ADB commands can be used, such as checking the Android version of your phone:

liuyue@DESKTOP-NVU6CCV MINGW32 ~ $docker exec-it appium adb shell getprop ro.build.version.release 5.0Copy the code

Let’s write a simple set of Appium automated test scripts, first install the Appium library:

pip install Appium-Python-Client
Copy the code

Write appium_test. Py:

from appium import webdriver cap = { "platformName": "Android", "platformVersion": "5", "deviceName": "192.168.42.103:5555", "uDID ":"192.168.42.103:5555"," uDID ":"192.168.42.103:5555", "7.1.2", # "deviceName": "10d4e4387d74", "noReset": True, "unicodeKeyboard": True, "resetKeyboard ": True} driver = webdriver. Remote (' https://192.168.99.100:4723/wd/hub ', App_path ='C:\\test.apk', replace=False, # allowTestPackages=True, # allow test package useSdcard=False, # do not install on Sdcard grantPermissions=False) # grant permission driver.quit()Copy the code

Here 192.168.42.103:5555 is the address of the mobile client Genymotion simulation, and https://192.168.99.100:4723/wd/hub is based on the Docker Appium container, here we install a test app for mobile phones.

After the installation script is executed, you can determine whether the installation is successful:

from appium import webdriver cap = { "platformName": "Android", "platformVersion": "5", "deviceName": "192.168.42.103:5555", "uDID ":"192.168.42.103:5555"," uDID ":"192.168.42.103:5555", "7.1.2", # "deviceName": "10d4e4387d74", "noReset": True, "unicodeKeyboard": True, "resetKeyboard ": True} driver = webdriver. Remote (' https://192.168.99.100:4723/wd/hub ', cap) # whether APP installed, Parameters passed to the package name res = driver. Is_app_installed (' com. Tencent. Android. Qqdownloader) print (res) driver. The quit ()Copy the code

You can also use scripts to launch some apps, such as the built-in calculator app:

from appium import webdriver from time import sleep cap = { "platformName": "Android", "platformVersion": "", "deviceName": "192.168.42.103:5555"," uDID ":"192.168.42.103:5555", "platformName": "Android", "platformVersion": "7.1.2", "deviceName": "10d4e4387d74", "appPackage": "com.android.calculator2", "appActivity": "com.android.calculator2.Calculato", "noReset": True, "unicodeKeyboard": True, "resetkeyboard": True} driver = webdriver. Remote (' https://192.168.99.100:4723/wd/hub ', cap) # wait for 3 seconds to sleep (# 3) put the APP in the background for 5 seconds, Background_app (5) # close APP driver.close_app() sleep(3) # Restart APP driver.launch_app() sleep(3) driver.quit()Copy the code

Here the calculator’s package name and activity information are configured in the cap variable.

Kill the application process:

from appium import webdriver cap = { "platformName": "Android", "platformVersion": "5", "deviceName": "192.168.42.103:5555", "uDID ":"192.168.42.103:5555"," uDID ":"192.168.42.103:5555", "7.1.2", # "deviceName": "10d4e4387d74", "appPackage": "com.android.calculator2", "appActivity": "com.android.calculator2.Calculato", "noReset": True, "unicodeKeyboard": True, "resetkeyboard": True} driver = webdriver. Remote (' https://192.168.99.100:4723/wd/hub ', cap) # wait for 3 seconds to sleep (# 3) if the application is not running or is in the background, Activate the application driver.activate_app('com.android.calculator2') sleep(3) # Terminate the application driver.terminate_app('com.android.calculator2') sleep(3) driver.quit()Copy the code

Almost all mobile application operations can be automated by writing Python scripts, and Appium can be thought of as the mobile Selenium, which is easy to use.

Conclusion: Shakespeare said, “There are a thousand Hamlets in the eyes of a thousand audiences.” In the tens of thousands of Genymotion simulators, mobile apps can look like hundreds of thousands of different things, and by writing automated scripts with Appium, testers can free up thousands of iterations of testing. Why not?

The original article is reprinted from “Liu Yue’s technology blog” v3u.cn/a_id_196