1. Introduction

Hello, I’m Anguo!

I have written many articles on App automation before, which are mostly realized based on Appium, Airtest, barrier-free services and other technologies

Among them, the automation scripts written by Appium and Airtest rely on PC terminal and cannot be run directly on mobile terminal. Accessibility requires the creation of a separate Android project without complete documentation and a certain threshold to use

Starting with this article, we introduce an automated tool that can run directly on mobile: AutoJS

2. AutoJS is introduced

AutoJS is similar to the button wizard. It is an automatic tool on the Android platform. It performs automatic operation on App by writing JavaScript scripts and combining with the “barrier-free service” of the system

Official documentation: pro.autojs.org/docs/#/zh-c…

Its advantages include:

  • Use JS script, code readable

  • Script files are small in size and can be packaged as APK and installed directly

  • Have rich UI components for building GUI interfaces

  • Non-root devices can also perform automated operations and run directly without a PC

  • Provides a variety of elements positioning mode, can be adapted to various models

  • The official documentation is very detailed and the cost of learning is low

3. Prepare

There are several versions of AutoJS, the two most commonly used are Auto-.js Pro and Auto-.js 4.1.1 Beta

PS: For some reason, AutoJS authors now only maintain auto.js Pro versions, and auto-.js Pro limits some major apps

Reason: pro.autojs.org/faq

The rest of the article will use auto-.js 4.1.1 Beta as an example to explain (download method is provided at the end of the article).

First, download VS Code software and 2 plug-ins

The 2 plug-ins include:

  • Auto.js-VSCodeExt

  • Auto.js-VSCodeExt-Fixed

Auto-.js-vscodeext-fixed is partially optimized for the plug-in auto-.js-vscodeext, which is more convenient for us to debug scripts

Then, use VS Code shortcut keys “Ctrl/Command + Shift + P” and select “Auto. Js :Start Server” to Start the AutoJS service

Next, install the AutoJS application and the AutoJS packaging tool application on the real machine or emulator

PS: If using simulator, it is recommended to use netease MuMu or Thunder simulator

Open the AutoJS application, enter the prompt dialog box for application closure and update for the first time, and follow the instructions to open “Barrier-free Service”.

On the main interface of the software, click the upper left corner to slide out the sidebar and open barrier-free service, front desk service and suspension window successively

Front-desk services are used to improve service survival and prevent service recycling

The suspension window will float over any interface and provide some quick functions and operations, including:

  • List of file items

    Will show example code and their own scripts, folders, you can quickly complete script editing, running, scheduled tasks, packaging and other operations

  • Script recorded

    The recording script is only applicable to devices after Root. It is rarely used because it is based on coordinate points and does not have strong adaptability

  • Element control positioning

    For the current interface layout control analysis, layout hierarchy analysis

  • Close the executing script

    Stop all executing script tasks with one click

  • More Settings

    You can quickly go to the Barrier-free services page and view the current application package name and Activity name

Finally, select “Connect to Computer” in the software sidebar and enter the IP address of the PC in the dialog box

PS: If there is no error when AutoJS connects to the computer, VS Code notification bar and OUTPUT will show the successful connection message of the device

4. Get real

With the above preparations in place, we are ready to use JS to write automated scripts in VS Code

Here is an example of automatic brush douyin short video

First, use “auto-.waitfor ()” to ensure that the barrier-free service is started successfully

Then, use the launchApp + App name to quickly launch the Tiktok App

The interface element content + waitFor() method is then used to waitFor the element to appear, representing the completion of the interface load

Finally, Root + Swipe + coordinates are used to simulate interface sliding

PS: For convenience, you can directly use the API method of Root device. If you are not Root device, you can use the official sliding API or control center coordinate click event to implement

The complete code is as follows:

Auto-.waitfor () // open App var appName = "tiktok "; (appName); Text (" home ").waitfor (); Toast (" Ready to Swipe ") // Swipe(Root+ coordinates) while (true) {Swipe(200, 1000, 210, 400, 500); // sleep(5000); Toast (" Keep sliding..." )}Copy the code

5. The last

This article introduces you to the basics of using AutoJS and illustrates its use with a simple example

I have uploaded the software mentioned in the article to the background, and the reply keyword “autoJS” can be obtained

If you think the article is good, please like, share, leave a message, because this will be my continuous output of more high-quality articles the strongest power!

Recommended reading

How does Postman debug an encrypted interface?

5 minutes, using Intranet penetration to quickly achieve remote desktop

How can Jmeter execute Python scripts concurrently

Talk about the best PC automation solution – Pywinauto

Talk about the best PC automation scheme – WinAppDriver