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 生成报告
执行结束查看报告