Android終端產品系統或App測驗,涉及輸入框邊界值測驗,
比如wifi熱點設定熱點名稱, 或者搜索輸入框,
需要驗證該文本輸入框是否最多可以輸入256個字符,
如何快速實作進準的256個字符的輸入呢?
準備階段
- 手動先點擊wifi熱點名稱文本輸入框,確保游標已經在編輯框內了
- 利用adb shell input text + 256個字符, 可以輸入256字串輸入
- string.ascii_letters 可以包含大小寫的英文字母
- string.digits 可以包含數字1-10
- 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
也可關注“武散人”微信訂閱號,隨時接受文章推送,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/166995.html
標籤:Python
下一篇:實體009:暫停一秒輸出
