VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之《自拍教程49》Python_adb批量字符输入

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

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运行,当然也可以双击运行。
运行效果如下:

相关教程