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
也可關注“武散人”微信訂閱號,隨時接受文章推送。