1. 程式人生 > 實用技巧 >介面自動化練手

介面自動化練手

測試的結構如下:

工具類,這裡設計的工具類用於讀取檔案,包括手機,密碼,token,cartid,:

 1 import csv
 2 import pandas       #匯入讀取csv檔案的模組
 3 import numpy
 4 from builtins import type
 5 class File():
 6     def read(self):     #讀取手機密碼
 7         f = open('../TestCase/test.csv', 'r', encoding='utf-8')
 8         cr = pandas.read_csv(f)
9 p = cr['手機'] 10 w = cr['密碼'] 11 phone = numpy.array(p) #把讀取的欄位資料轉換為列表 12 word = numpy.array(w) 13 f.close() 14 return phone[-1],word[-1] #返回最後一條資料 15 def readcookie(self): 16 f = open('../TestCase/cookie.csv', 'r', encoding='utf-8
') 17 cr = pandas.read_csv(f) 18 ck = cr['cookie'] 19 20 cook = numpy.array(ck) 21 f.close() 22 return cook[-1] 23 24 def readcartid(self): 25 f = open('../TestCase/cart_id.csv', 'r', encoding='utf-8') 26 cr = pandas.read_csv(f) 27 cartid = cr['
cart_id'] 28 29 cart_id = numpy.array(cartid) 30 f.close() 31 32 print(cart_id[-1]) 33 return cart_id[-1] 34 35 def write(self,l): 36 f = open('../TestCase/test.csv', 'a', encoding='utf-8') 37 w = csv.writer(f) 38 print(l) #測試接收到的值 39 40 w.writerow(l) 41 42 f.close() 43 44 def writecookie(self, cook): 45 f = open('../TestCase/cookie.csv', 'a', encoding='utf-8') 46 w = csv.writer(f) 47 print(cook) # 測試接收到的值 48 49 w.writerow(cook) 50 51 f.close() 52 53 def writecartid(self, cart_id): 54 f = open('../TestCase/cart_id.csv', 'a', encoding='utf-8') 55 w = csv.writer(f) 56 print(cart_id) # 測試接收到的值 57 58 w.writerow(cart_id) 59 60 f.close() 61 62 # f = File() 63 # l = ['16093252748', 'VzQBKQv6'] 64 # print(f.read())

業務類,包括註冊,登入,加入購物車,立即購買功能:

getMobileCode.py:

 1 import string
 2 
 3 import requests
 4 import time
 5 import random
 6 class GetMobileCode():
 7     mobile = '1' + ''.join(random.choice(string.digits) for i in range(10)) #隨機生成1開頭手機號
 8     def cause(self):
 9         if __name__ == '__main__':
10             print(self.mobile)
11         self.currStr = time.time()  #時間戳
12         return self.mobile
13 
14     def getcode(self):
15         d = {'currStr': self.currStr, 'mobile':self.mobile}
16         r = requests.post('http://106.54.81.164:8080/front/getMobileCode', data=d)#呼叫post方法
17         print(r.status_code)
18         print(r.text)

mobileRigester.py:

 1 import random
 2 import string
 3 import requests
 4 # #
 5 class MobileRigester():
 6     def cause_passwd(self):
 7         self.passwd = ''.join(random.choice(string.digits + string.ascii_letters) for i in range(8))
 8         return self.passwd
 9     def rigester(self,mobile):
10         self.d = {'mobileCode':'abc','mobile':mobile, 'password':self.passwd}
11         r = requests.post('http://106.54.81.164:8080/front/mobileRegister',data=self.d)
12         print(r.text)
13         return mobile,self.passwd   #返回手機號,密碼的元組

loginCheck:

 1 import requests
 2 
 3 class LoginCheck():
 4 
 5     def login(self,li):
 6         d = {'username':li[0], 'password': li[1], 'captcha': 'abc'}
 7         # print(username,passwd)
 8         r = requests.post('http://106.54.81.164:8080/front/loginCheck',data=d)
 9         print(r.text)
10         print(r.status_code)
11         status = r.status_code
12         r_json = r.json()
13         c = r.cookies
14         return c

buyNow.py

 1 import requests
 2 
 3 
 4 class Buy():
 5         def buynow(self,token):    #,cookie
 6             d = {'goodsId':'08d03f04c22f454c86cbb869d6163f2f','count':'2','specId':'0065f44fbd0342c1a9eafc1fa1630b53','TOKEN':token}
 7             r = requests.post('http://106.54.81.164:8080/front/cart/buyNow', data=d)      #把token放到data裡一起提交
 8             print(r.text)
 9             # print('1111')
10             return r.json()
11 
12 # b = Buy()
13 # b.buynow()

addOrder.py:

 1 import requests
 2 
 3 class order():
 4     def add(self,cartid,token):  #,cookie,cartid
 5         # print(cartid)
 6         # print(token)
 7         d = {'cartIds':cartid, 'storeId': 'b785c011f9a64d2087331abc5e581f3d', 'address_options': 'f015b66a6ac645558324e49e05f5106e', 'paytype': '1', 'isPd': '0','TOKEN':token}
 8         r = requests.post('http://106.54.81.164:8080/front/cart/addOrder', data=d)
 9         print(r.text)
10 # o = order()
11 # o.add()

測試類,對以上業務進行測試:

getmobilecode.py:

1 from ttt.Transaction.getMobileCode import GetMobileCode#匯入業務類包
2 class test_mobilecode():
3     def test(self):
4         g = GetMobileCode() #建立業務類物件
5         g.cause()
6         g.getcode()
7         return g.cause()
8 t = test_mobilecode()   #測試類物件
9 mob = t.test()

mobilerigester.py:

 1 from ttt.TestCase.getmobilecode import mob
 2 from ttt.Transaction.mobileRigester import MobileRigester
 3 from ttt.Util.tools import File
 4 
 5 class test_rigester():
 6     def test(self):
 7         t_rigester = MobileRigester()
 8 
 9         t_rigester.cause_passwd()
10         l = list(t_rigester.rigester(mob))      #獲得返回的list型別資料
11         f = File()
12         f.write(l)      #把手機,密碼寫入檔案 #在當前包下呼叫其他類執行open操作,只會在當前包進行查詢,別的包找不到,如果沒有,就會建立該檔案
13         print(l)  # 測試傳入的值
14 
15 t_r = test_rigester()
16 t_r.test()

loginCheck_test.py:

 1 from ttt.Transaction.loginCheck import LoginCheck
 2 from ttt.Util.tools import File
 3 
 4 
 5 class login():
 6     def login_test(self):
 7         logincheck = LoginCheck()
 8         re = File()
 9         li = re.read()
10         cook = logincheck.login(li)     #獲得cookie
11         cookie = [cook['TOKEN']]
12         # pickle.dump()
13         # cookie = cook[cook.index('='):cook.index('/') + 1]      #擷取cookie的int欄位
14 
15         w = File()
16         w.writecookie(cookie)               #呼叫寫cookie方法寫入
17         # print(li[0],li[1])
18         return cook
19 l = login()
20 l.login_test()

buyNow.py:

 1 from builtins import type
 2 
 3 from ttt.Transaction.buyNow import Buy
 4 from ttt.Util.tools import File
 5 
 6 
 7 class buynow():
 8     def buy(self):
 9         buy = Buy()
10         f = File()
11         c = f.readcookie()
12 
13         print(c)
14         print(type(c))
15         cartid = buy.buynow(c)
16         cart_id = [cartid['cartIds']]
17         fc = File()
18         fc.writecartid(cart_id)
19     # status = buy.r.status_code
20     # r_dict = buy.r.json()
21     # r_card = r_dict['cartIds']
22     # print(buy.r.text)
23 b = buynow()
24 b.buy()

addorder_test.py:

 1 from ttt.Transaction.addOrder import order
 2 from ttt.Util.tools import File
 3 
 4 class addOrder():
 5     def add(self):
 6 
 7         f = File()
 8         cookie = f.readcookie()
 9         cartid = f.readcartid()
10         print('cookie:',cookie)
11         print('cartid:',cartid)
12 
13         aod = order()
14         aod.add(cartid,cookie)
15 addorder_test = addOrder()
16 addorder_test.add()

還有三個存資料的csv檔案:

test.csv:

cookie.csv:

cart_id.csv: