《自拍教程49》Python_adb批量字符输入

Android终端产品系统或App测试,涉及输入框边界值测试,
比如wifi热点设置热点名称, 或者搜索输入框,
需要验证该文本输入框是否最多可以输入256个字符,
如何快速实现进准的256个字符的输入呢?


准备阶段
  1. 手动先点击wifi热点名称文本输入框,确保光标已经在编辑框内了
  2. 利用adb shell input text + 256个字符, 可以输入256字符串输入
  3. string.ascii_letters 可以包含大小写的英文字母
  4. string.digits 可以包含数字1-10
  5. random.sample 可以随机实现从一个数组“池” 里随机采样

Python批处理脚本形式
# coding=utf-8

import os
import string
import random

chars_num = 256  # chars num字符数量

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需要确保数组“池”里的数据足够多,所以需要:
(string.ascii_letters + string.digits) * 5


Python面向过程函数形式
# 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面向对象类形式
# 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")

运行方式与效果

确保Android设备通过USB线与电脑连接了,adb设备有效连接,
以上代码的3种实现形式都可以直接运行,比如保存为input_text.py并放在桌面,
建议python input_text.py运行,当然也可以双击运行。
运行效果如下:


更多更好的原创文章,请访问官方网站:www.zipython.com
自拍教程(自动化测试Python教程,武散人编著)
原文链接:https://www.zipython.com/#/detail?id=4556ebb04f9f446095531eba4274a51f
也可关注“武散人”微信订阅号,随时接受文章推送。

发布了59 篇原创文章 · 获赞 57 · 访问量 3261

猜你喜欢

转载自blog.csdn.net/qq_45572661/article/details/105055589