“Selfie tutorial 49” Python_ ADB batch character input

Time:2020-9-15

Android terminal product system or app test involves input box boundary value test,
For example, WiFi hotspot setting hotspot name, or search input box,
You need to verify that the text input box can input up to 256 characters,
How to quickly realize the input of 256 characters?

Preparation stage
  1. Manually click the WiFi hotspot name text input box to make sure the cursor is in the edit box
  2. Using ADB shell input text + 256 characters, you can input 256 strings
  3. string.ascii_ Letters can contain upper and lower case letters
  4. string.digits Can contain numbers 1-10
  5. random.sample Random sampling can be realized from an array “pool”
Python batch script form
# coding=utf-8

import os
import string
import random

chars_ Num = 256 # chars num number of characters

random_list = random.sample((string.ascii_letters + string.digits) * 5, chars_num - 8)
random_str = ''.join(random_list)
random_str = "START" + random_str + "END"
print(random_str)
os.system("adb shell input text %s" % random_str)
print("Inputed %s chars, please check the \"START\" and \"END\" keyword" % chars_num)
os.system("pause")

random.sample You need to ensure that there is enough data in the “pool” of the array, so you need to:
(string.ascii_letters + string.digits) * 5

Python oriented procedural function form
# coding=utf-8

import os
import string
import random

def input_text(chars_num):
    if chars_num > 8:
        random_list = random.sample((string.ascii_letters + string.digits) * 5, chars_num - 8)
        random_str = ''.join(random_list)
        random_str = "START" + random_str + "END"
        print(random_str)
        os.system("adb shell input text %s" % random_str)
        print("Inputed %s chars, please check the \"START\" and \"END\" keyword" % chars_num)
    else:
        print("chars num too short...")

input_text(256)
os.system("pause")

Python object oriented class form
# coding=utf-8

import os
import string
import random


class TextInputer():
    def __init__(self):
        pass

    def input_text(self, chars_num):
        if chars_num > 8:
            random_list = random.sample((string.ascii_letters + string.digits) * 5, chars_num - 8)
            random_str = ''.join(random_list)
            random_str = "START" + random_str + "END"
            print(random_str)
            # os.system("adb shell input text %s" % random_str)
            print("Inputed %s chars, please check the \"START\" and \"END\" keyword" % chars_num)
        else:
            print("chars num too short...")


t_obj = TextInputer()
t_obj.input_text(256)

os.system("pause")

Operation mode and effect

Make sure that the Android device is connected to the computer through the USB cable, and that the ADB device is effectively connected,
The three implementation forms of the above code can be run directly, such as saving as input_ text.py And put it on the table,
Suggest Python input_ text.py Run, of course, you can double-click to run.
The operation effect is as follows:

For more and better original articles, please visit the official website: www.zipython.com
Self portrait course (Python course of automatic test, edited by Wu Sanren)
Link to the original text: https://www.zipython.com/#/detail ?id=4556ebb04f9f446095531eba4274a51f
You can also pay attention to the wechat subscription number of “wusanren” and accept article push at any time.