网站建站免费,青州网站建设优化推广,上虞区驿亭镇新农村建设网站,商务网站开发实训体会概述
在UI自动化测试中#xff0c;POM模式是一种设计思路#xff0c;它的核心思想是方法的封装。它将方法类和页面元素进行分离#xff0c;增强了代码的可维护性。值得注意的是#xff0c;这种分层的设计模式#xff0c;最好也是从线性代码开始#xff0c;逐步将代码进行…概述
在UI自动化测试中POM模式是一种设计思路它的核心思想是方法的封装。它将方法类和页面元素进行分离增强了代码的可维护性。值得注意的是这种分层的设计模式最好也是从线性代码开始逐步将代码进行升级最后形成以POM设计思路为核心的自动化测试框架。
思路梳理
版本升级思路
V1不使用任何设计模式和单元测试框架V2(使用PyTest管理用例)V3(使用方法封装的思想对代码进行优化。)V4(引入POM设计思路也就是对象层、操作层、业务层将方法类和页面元素进行分离)
编写思路梳理
技术架构pythonseleniumpytestallure
思路和步骤
搭建测试环境配置pycharm以及python环境并进行验证。安装所需的测试依赖包进行代码编写首先导入我需要的包。例如selenium、pytest、sleep等必需的工具包。其次在编写的过程中需要使用定位方法例如xpath等合适的方法对登录框和密码框、登陆按钮进行定位。之后使用sendkey等方法进行网页操作。最后使用assert进行断言。代码运行成功后需要对代码进行持续的升级以及优化。例如方法的封装、调用以及页面元素的封装以及调用等直到形成测试框架的初始形态对于登录页功能来讲代码量较少较易理解。框架形成后需要对框架进行持续升级和优化因为页面元素在变化、项目也在不断迭代因此测试框架需要持续升级才能适应项目的更新迭代特别是对于敏捷测试来讲。
代码示例
方法封装封装基类也叫关键字
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait# 工具类
class WebKeys:# 构造方法用于接受driver对象def __init__(self, driver):self.driver driverself.wait WebDriverWait(self.driver, 5)# 打开浏览器def open(self, url):self.driver.get(url)self.wait.until(ec.url_contains(url))# 元素定位def locator(self, name, value):el self.driver.find_element(name, value)# 将元素的所在框标记出来self.locator_station(el)return el# 定位显示等待def locator_with_wait(self, name, value):元素的定位显示等待locator (name, value)self.wait.until(ec.visibility_of_element_located(locator))el self.driver.find_element(name, value)# 将元素的所在框标记出来self.locator_station(el)return el# 窗口切换def change_window(self, n):窗口切换的功能# 获取句柄handles self.driver.window_handles# 切换到原始的页面, n 0# 切换到第二个页面, n 1 依次类推# 切换到最新的页面, n -1self.driver.switch_to.window(handles[n])print(当前跳转成功的页面, self.driver.title)# 元素文本获取 显示等待def text_wait(self, name, value, text):el (name, value)res self.wait.until(ec.text_to_be_present_in_element(el, text))return res# 对获取到的元素进行判断# 获取到的元素的文本信息进行判断# 鼠标事件def mouse_hold(self):鼠标事件的操作action ActionChains(self.driver)action.click().click_and_hold().scroll_to_element().perform()# 键盘事件# 滚轮事件# 显示定位的地方方便确认定位位置def locator_station(self, ele):self.driver.execute_script(arguments[0].setAttribute(style,arguments[1]);,ele,border: 2px solid red # 边框red红色)数据读取驱动
import yamldef load_yaml(path):file open(path, r, encodingutf-8)data yaml.load(file, Loaderyaml.FullLoader)return data
页面定位元素
from selenium.webdriver.common.by import By# 用户名输入框
page_login_user [xpath, //input[placeholder手机号码]]# 密码输入框
page_login_indexPwd [xpath, //input[placeholder密码]]# 登陆按钮
page_login_loginBtn [xpath, //input[namebtnLogin]]# 作家专区
page_index_author [By.LINK_TEXT, 作家专区]# 章节管理按钮
page_authorManage_chapterManageBtn [By.XPATH, //*/a[contains(text(),章节)]]操作元素
from selenium.webdriver.support.wait import WebDriverWait
from locate.allPages import *
from selenium.webdriver.support import expected_conditions as ecfrom key_word.keyword import WebKeys# 创建章节的业务场景类
class CreateChapterLogic(WebKeys):def createChapter(self, title):# 实例化waitwait WebDriverWait(self.driver, 10)# 测试步骤# 01 点击作家专区、打开新页签self.locator_with_wait(*page_index_author).click()# 等待新页签出现wait.until(ec.number_of_windows_to_be(2))# 02 切换窗口self.change_window(-1)# 03 点击章节管理按钮self.locator_with_wait(*page_authorManage_chapterManageBtn).click()
用例层
import timeimport pytest
from selenium.webdriver.common.by import Byfrom data_driver.yaml_driver import load_yaml
from logic.create_chapter import CreateChapterLogic
from logic.login import LoginPage
from VAR.BOOKHOUSE_VAR import *pytest.mark.skip
def test_case01(borwser):# 需要谷歌浏览器的实例化对象login LoginPage(borwser)login.login(LOGIN_URL, USERNAME, PASSWD)# 创建章节的步骤createChapter CreateChapterLogic(borwser)createChapter.createChapter(章节0001)pytest.mark.parametrize(data, load_yaml(./data/userData.yaml))
def test_case02(borwser, data):print(data, data)# 需要谷歌浏览器的实例化对象login LoginPage(borwser)login.login(LOGIN_URL, str(data[username]), str(data[passwd]))logic_text login.text_wait(link text, str(data[username]), str(data[username]))assert logic_text is Truelogin.mouse_hold()使用main_run运行
import pytestdef run():pytest.main([-vs])if __name__ __main__:run()总结
搭建框架前希望大家可以思考以下几个问题 1、封装后的框架作用大不大
2、封装什么内容是否明白
3、自动化的技术到底可以帮助功能测试解决什么问题 耗时、重复的步骤多、避免人为的因素导致测试的结果正确性低
4、需要把重复的内容给他挑出来 因为程序可以反复的执行循环、重复的调用
5、下一次执行用例的时候如果遇到了这么重复性的内容可以干什么 复用
在这种方法思路POM的指导下我们还可以对框架进行深度优化和二次封装增强测试框架的易用性。
以上就是今天为大家分享的全部内容下一篇文章再见感谢阅读。