1. 程式人生 > 實用技巧 >selenium(12)-web UI自動化專案實戰(PO模式,程式碼封裝)

selenium(12)-web UI自動化專案實戰(PO模式,程式碼封裝)

web UI自動化專案實戰-專案

專案使用禪道,所以你需要搭建1個禪道,搭建禪道的方法和步驟見

https://www.cnblogs.com/xinhua19/p/13151296.html

搭建UI自動化的框架

第一步,搭建框架,從配置檔案資訊開始,然後讀取配置檔案,然後是讀取驅動。

第二步,對selenium中的一些方法做二次封裝

第三步,封裝頁面的元素物件和元素的新增,刪除,查詢,修改等操作

第四步,測試用例層,設計測試用例

selenium讀取配置檔案的方法封裝

有了配置config.ini的配置檔案,肯定是要讀取這個配置檔案的,話不多說,直接上程式碼

import os
import codecs
import configparser
import logging proDir = os.path.split(os.path.realpath(__file__))[0]
rootDir = os.path.dirname(proDir)
dataDir = os.path.join(rootDir, "data")
configPath = os.path.join(rootDir, "config", "config.ini") class ReadConfig:
def __init__(self):
fd = open(configPath)
data = fd.read() # remove BOM
if data[:3] == codecs.BOM_UTF8:
data = data[3:]
file = codecs.open(configPath, "w")
file.write(data)
file.close()
fd.close() self.cf = configparser.ConfigParser()
self.cf.read(configPath) def get_email(self, name):
value = self.cf.get("EMAIL", name)
return value def get_http(self, name):
value = self.cf.get("HTTP", name)
return value def get_db(self, name):
value = self.cf.get("DATABASE", name)
return value def get_postgresql(self, name):
value = self.cf.get("POSTGRESQL", name)
return value def get_loglevel(self, name):
value = self.cf.get("LOG", name)
return value def get_account(self, name):
value = self.cf.get("ACCOUNT", name)
return value def get_accounts(self):
return self.cf.items("ACCOUNT") def get_webapp(self, name):
return self.cf.get("WEBAPP", name)

那麼讀取到了配置檔案資訊後,肯定也是要應用配置檔案的,話不多說,直接上程式碼

# coding:utf-8
import time
from utils import readconfig
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC localReadConfig = readconfig.ReadConfig() class BasePage:
scheme = localReadConfig.get_http("scheme")
base_url = scheme + r"://" + localReadConfig.get_http("baseurl")+':'+localReadConfig.get_http("port")+'/zentao' def __init__(self, driver, url=None):
self.url = url
self.driver = driver
self.timeout = 12
self.wait = WebDriverWait(self.driver, self.timeout) self.url = self.base_url
if url:
self.url = self.base_url + url def open(self, url=None):
if not url:
self.driver.get(self.url)
else:
self.driver.get(url)
self.driver.maximize_window() def wait_for_element_presence(self, locator):
self.wait.until(EC.presence_of_element_located(locator)) def find_element(self, locator):
try:
return self.driver.find_element(*locator)
except Exception as e:
print("element not be found {}".format(locator[1]))
print("python tell us: {}".format(str(e)))
return None def wait_for_element_visible(self, locator):
self.wait.until(EC.visibility_of_element_located(locator)) def wait_for_element_invisible(self, locator):
self.wait.until(EC.invisibility_of_element(locator)) def implicitly_wait(self, time_to_wait=3):
self.driver.implicitly_wait(time_to_wait) @staticmethod
def sleep(t=1):
time.sleep(t)

selenium讀取驅動的方法封裝

selenium常用方法的二次封裝

頁面元素物件和 操作的封裝

測試用例呼叫操作,執行測試用例

HTMLTestRunner,py 生成報告

執行結束檢視報告