不少人以爲測試沒有什麼技術含量 簡單枯燥php
在初期階段看起來是這樣的 但事實真的如此麼 那些潛移默化中積累下的方法論與世界觀html
沒作過測試的開發永遠不會懂 😄python
最近作了兩件挺有意思的事android
一件是移動端深度兼容測試的實踐git
一件是UI自動化獲取淘口令並模擬登錄更新到服務端web
移動端深度兼容測試json
依賴:Uiautomator2 Atxcookie
經過thread 多線程併發執行session
#!/usr/bin/python # -*- coding: UTF-8 -*- import thread from unittest import TestCase # 咱們使用python的unittest做爲單元測試工具 import unittest # 使用time.sleep(xx)函數進行等待 import time import atx import random import string import json import uiautomator2 as ut2 # 爲線程定義一個函數 def print_time(threadName, delay): count = 0 while count < 10: time.sleep(delay) count += 1 print "%s: %s" % (threadName, time.ctime(time.time())) def tcp_connection (device): return def randomchar_gen(length): ran_str = ''.join(random.sample(string.ascii_letters + string.digits, length)) return ran_str def thread_go(device): d = ut2.connect(device) #d.unlock() d.app_stop_all() d.unlock() d.app_start('com.lanjingren.ivwen') time.sleep(3) d(resourceId='com.lanjingren.ivwen:id/maintab_add').click_exists(15) #點擊創造加號 d(resourceId="com.lanjingren.ivwen:id/id_item_select", className="android.widget.ImageView", instance=1).click_exists(10) #選擇一張圖片 d(resourceId="com.lanjingren.ivwen:id/id_item_select", className="android.widget.ImageView", instance=3).click_exists(10) #選擇一張圖片 d(resourceId="com.lanjingren.ivwen:id/tv_actionbar_right_text").click_exists(10) #點擊完成 d(resourceId="com.lanjingren.ivwen:id/rl_edit_title").click_exists(10) #點擊編輯標題 str_title=randomchar_gen(8) d(resourceId="com.lanjingren.ivwen:id/edit").send_keys(str_title) #編輯文章標題 d(resourceId="com.lanjingren.ivwen:id/tv_actionbar_right_text").click_exists(3) #完成文章標題編輯 d(resourceId="com.lanjingren.ivwen:id/tv_actionbar_right_text").click() #time.sleep(2) d(resourceId="com.lanjingren.ivwen:id/button_theme").click() #進模板 d(resourceId="com.lanjingren.ivwen:id/imageView", className="android.widget.ImageView", instance=2).click()#選擇模板 d(resourceId="com.lanjingren.ivwen:id/text_confirm").click()#完成模板選擇 #d(className="android.widget.LinearLayout", instance=13).click() #time.sleep(2) d(textContains='下一步').click() #d(textContains='下一步').up() #d(resourceId="com.lanjingren.ivwen:id/button_comment").click #time.sleep(2) d(text='完成').click() time.sleep(5) try: thread.start_new_thread(thread_go, ("192.168.0.166:7912",)) thread.start_new_thread(thread_go, ("192.168.0.32:7912",)) thread.start_new_thread(thread_go, ("192.168.0.242:7912",)) except: print "Error: unable to start thread" while 1: pass # # 建立兩個線程 # try: # thread.start_new_thread(print_time, ("Thread-1", 2,)) # thread.start_new_thread(print_time, ("Thread-2", 4,)) # except: # print "Error: unable to start thread" # # while 1: # pass
效果:http://video.ivwen.com/users/3011/B03BED79-6A83-4A5C-A3BE-EE21DDDD5010.mp4
多線程
UI自動化獲取淘口令並模擬登錄更新到服務端
背景:淘寶分享出來的商品淘口令有效期不足兩小時,sdk獲取淘口令API 須要走阿里媽媽的合做網站註冊流程成本較高,
基於已經實現的給運營手動更新口令的功能,嘗試經過UI獲取到口令,而後模擬登錄更新口令到服務端
UI操做方法
global d def UIoperation(self): d = ut2.connect('192.168.0.166:7912') d.app_stop_all() d.unlock() d.app_start('com.taobao.taobao') time.sleep(8) d(resourceId="com.taobao.taobao:id/iv_nav_icon", className="android.widget.ImageView", instance=3).click() d(text=u"關注店鋪").click() time.sleep(5) d(description=u"美之優選").click() time.sleep(5) #d(className="android.support.v7.app.ActionBar$Tab", instance=1).click() #d(className="android.widget.ImageView", instance=14).click() d.click(0.265, 0.873) d(resourceId="com.taobao.taobao:id/uik_public_menu_action_icon").click() d(resourceId="com.taobao.taobao:id/uik_public_menu_item_title", text=u"分享").click() d(resourceId="com.taobao.taobao:id/target_item_image").click() text = d(resourceId="com.taobao.taobao:id/taopassword_alert_title").get_text() time.sleep(3) return text
提取口令方法
def getCode(self,str): utf8string = str.encode("utf-8") list = re.split('這條信息', utf8string) str = list[1] list2 = re.split('後打開', str) code = list2[0] return code
模擬登錄並獲取可用的PHP session ID ,注MozillaCookieJar是帶有save()方法的,默認沒有
def getSession(self): filename = 'cookie.txt' cookie = cookielib.MozillaCookieJar(filename) handler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) req = urllib2.Request("http://baidu.com/login.php", urllib.urlencode({"account": "馬賽克", "pwd": "馬賽克"})) response = opener.open(req) # 保存cookie到文件 # ignore_discard,即將廢棄的cookie也保存 # ignore_expires,若是文件中cookie已經存在,則覆蓋寫入 cookie.save() for ck in cookie: # print ck.name, ':', ck.value if ck.name == 'PHPSESSID': return ck.value
更新口令方法
def updateH5(self, ss, code): url = 'http://baidu.com/zhi.php?a=h5' headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9' } postdata = {'馬賽克': '1', '馬賽克': code} cookies = dict(PHPSESSID= ss) session = requests.Session() session.cookies = cookielib.CookieJar() result = session.post(url, data=postdata, headers=headers, cookies=cookies)
最後調用方法實現
if __name__ == "__main__": o = CodeGenerator() #string ="【【美之優選】心相印捲紙整箱捲筒紙3層110克紙巾家用廁所衛生紙】,複製這條信息¥i7TC0IP3A2q¥後打開👉手淘👈" #taocode = o.getCode(string) #print taocode tao = o.UIoperation() code = o.getCode(tao) print code ss = o.getSession() o.updateH5(ss, code)
事件一投入去作能夠減小第三方廠商(Wetest、Testin等)作深度兼容的費用,且能夠作得更細緻出現問題也更方便定位
事件二節省了開發資源,業務響應也更及時些
測試並非那麼無趣,咱們能夠作到更多~