1.環境問題自行搭建
2.專案簡介:
本專案是基于python+appium+unittest以及pytest中的一些方法搭建的,資料驅動使用的是python的ddt模塊,整體實作是PO(Page
Objects)模式,也就是頁面物件設計模式,用該模式的好處就是實作了頁面的元素和測驗用例分離,后期維護時只要修改測驗用例就行,資料驅動使用時python的第三方庫ddt
3.專案目錄:
(1).config:存放專案的全域引數,
(2).src:存放的測驗用例相關的,
(3).src/common:存放的是測驗用例需要的公共方法,
(4).src/data:存放的是測驗用例用到的資料,
(5).src/pages:對頁面元素的封裝,
(6).src/report:測驗報告存放的目錄,
(7).src/test_case:測驗用例存放的目錄,
(8).runtest.py:測驗入口
4.config:存放的全域引數

globalparameter檔案 是專案的全域引數,(下面是代碼實作)
# coding:utf-8
__author__ = "helen"
'''
description:配置全域引數
'''
import time,os
# 獲取當前專案的存放路徑
project_path = os.path.abspath(os.path.join(os.path.dirname(os.path.split(os.path.realpath(__file__))[0]), '.'))
# 測驗用例代碼存放路徑(擁有構建suite)
test_case_path = project_path+"\\src\\test_case"
# 測驗報告的存放路徑,并以當前時間作為報告的前綴
report_path = project_path+"\\src\\report\\"
report_name = report_path+time.strftime('%Y-%m-%d_%H_%M_%S_',time.localtime())
# img存放路徑
img_path = report_path+"\\img\\"
img_name = img_path+time.strftime('%Y-%m-%d_%H_%M_%S_',time.localtime())+".png"
5.src/common 存放的測驗用例的公共方法,

(1)Base_page檔案 是重寫重寫find_element方法
# coding:utf-8
__author__ = 'Helen'
'''
description:UI頁面公共類
'''
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Base_page():
def __init__(self, driver):
self.driver = driver
def find_element(self, *loc):
'''重寫find_element方法,顯式等待'''
try:
WebDriverWait(self.driver, 3).until(EC.visibility_of_element_located(loc))
return self.driver.find_element(*loc)
except Exception as e:
raise e
def send_keys(self, value, *loc):
try:
self.find_element(*loc).clear()
self.find_element(*loc).send_keys(value)
except AttributeError as e:
raise e
(2)driver_config檔案 封裝appium的driver
from appium import webdriver
class Driver_Config():
def get_driver(self):
try:
self.desired_caps = {}
# 設備資訊
self.desired_caps['platformName'] = 'Android'
self.desired_caps['platformVersion'] = '6.0.1'
self.desired_caps['deviceName'] = '手機dirver'
# app的資訊
self.desired_caps['appPackage'] = '包名'
self.desired_caps['appActivity'] = 'appActivity'
self.desired_caps['unicodeKeyboard'] = True
self.desired_caps['resetKeyboard'] = True
self.desired_caps['automationName'] = 'uiautomator2'
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', self.desired_caps)
return self.driver
except Exception as e:
raise e
(3)slide_handle檔案 封裝的是頁面滑動的操作 (需要怎樣的滑動操作,在該檔案中封裝)
(滑動操作需要根據自己的需要進行封裝,可以參考一下)
class SlideHandle():
def __init__(self, driver):
self.driver = driver
def add_tysor_swipe(self, n):
# 下滑操作,(下滑操作時sx不變,變得的是sy,ey)
x = self.driver.get_window_size()["width"]
y = self.driver.get_window_size()["height"]
sx = x * 0.05
sy = y * 0.78
ey = y * 0.73
for i in range(n):
self.driver.swipe(sx, sy, sx, ey)
sleep(1)
6 data 存放資料

data檔案中是資料
#所使用的資料封裝成函式,使用時使用ddt模塊呼叫
def get_log_data():
return [
['', ''],
['173102755', ''],
['', 'asd123456'],
['184342222', 'asd123456'],
['173102715', '1234567'],
]
7 pages 存放頁面元素

(pages下的檔案都差不多一樣,就貼一個login檔案)
login檔案 封裝登錄頁面的元素,以及click,send_key操作
'''
頁面;登錄頁面
'''
from src.common import Base_page
from appium.webdriver.common import mobileby
class Login_page(Base_page.Base_page):
by = mobileby.MobileBy()
# 手機輸入框
user = (by.ID,"com.ansiyida.cgjl:id/editText_phone")
# 密碼輸入框
password = (by.ID,"com.ansiyida.cgjl:id/editText_password")
# 登錄按鈕
enter_button= (by.ID,"com.ansiyida.cgjl:id/btn_login")
# 注冊按鈕
register_button = (by.ID,"com.ansiyida.cgjl:id/text_reg")
# qq登錄按鈕
qq_button = (by.ID,"com.ansiyida.cgjl:id/tv_qq")
# 微信按鈕
wechat_button = (by.ID,"com.ansiyida.cgjl:id/tv_weiXin")
# 微博按鈕
microblog_button = (by.ID,"com.ansiyida.cgjl:id/tv_xinLang")
# 輸入手機號碼
def input_user(self,username):
self.send_keys(username,*self.user)
# 輸入密碼
def input_password(self,pwd):
self.send_keys(pwd,*self.password)
# 點擊登錄按鈕
def click_enter_button(self):
self.find_element(*self.enter_button).click()
# 點擊注冊按鈕
def click_register_button(self):
self.find_element(*self.register_button).click()
# 點擊qq按鈕
def click_qq_button(self):
self.find_element(self.qq_button).click()
# 點擊微信按鈕
def click_wechat_button(self):
self.find_element(self.wechat_button).click()
# 點擊微博按鈕
def click_microblog_button(self):
self.find_element(self.microblog_button).click()
8 report 存放的是測驗報告

9 test_case 存放的是測驗用例
(test_case里的用例也是大同小異,就寫一個test_login檔案)
import pytest
import unittest
from src.data.data import *
from ddt import data, ddt, unpack
from src.pages import login, index, personal
from src.common import driver_cofig
from config.globalparameter import img_name
@ddt
class TestLong(unittest.TestCase):
def setUp(self):
driver = driver_cofig.Driver_Config()
self.driver = driver.get_driver()
@data(*get_log_data())
@unpack
@pytest.mark.flaky(rerus=3)
def test_long_1(self, username, pasword):
self.inder = index.index_page(self.driver)
self.inder.click_my_button()
self.pers = personal.login_page(self.driver)
self.pers.click_hade_button()
self.login = login.Login_page(self.driver)
self.login.input_user(username)
self.login.input_password(pasword)
self.login.click_enter_button()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
注解:
在上述代碼中 @data(*get_log_data())@data中的資料是在 get_log_data()
函式中,get_log_data前加 * 號時是因為@data讀取引數型別是元組,加 * 是為了將get_log_data()
函式中的回傳的資料串列變為元組型別
在上述代碼中@unpack 表示用來解壓元組中的多個元素,實作一條測驗用例實作多個測驗點,
在上述代碼中@pytest.mark.flaky(rerus=3) 實作的是當用例報錯是重新執行測驗用例,用例執行成功后繼續執行下一條用例,(rerus=3)表示執行3次(借用的是pytest框架)
10 runtest檔案 入口
import HTMLTestRunner
import unittest
from config.globalparameter import report_name, test_case_path
suite = unittest.defaultTestLoader.discover(start_dir=test_case_path,pattern='test*.py')
# 執行測驗
if __name__=="__main__":
report = report_name+"Report.html"
fb = open(report,'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fb,
title=u'采購精靈自動化測驗',
description=u'網路正常'
)
runner.run(suite)
后記:到目前為止專案搭建就完畢了,比較粗糙,有興趣的朋友可以接著優化
看完點贊 ~養成好習慣,以上內容希望對你有幫助,如果對軟體測驗、介面測驗、自動化測驗、面試經驗交流感興趣可以加入我們,642830685,免費領取最新軟體測驗大廠面試資料和Python自動化、介面、框架搭建學習資料!技術大牛解惑答疑,同行一起交流,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/184369.html
標籤:其他
