提高网站转化率,表白网站制作模板,简单的网站开发工具,中国城市建设网网站前言#xff1a;
封装Selenium基本操作#xff0c;让所有页面操作一键调用#xff0c;让UI自动化框架脱离高成本、低效率时代#xff0c;将用例的重用性贯彻到极致#xff0c;让烦人的PO模型变得无所谓#xff0c;让一个测试小白都能编写并实现自动化。
知识储备前提
封装Selenium基本操作让所有页面操作一键调用让UI自动化框架脱离高成本、低效率时代将用例的重用性贯彻到极致让烦人的PO模型变得无所谓让一个测试小白都能编写并实现自动化。
知识储备前提熟练python语言理论与实际运用熟悉selenium库与自动化测试环境配置。
browseroperator.py 浏览器操作
webdriveroperator.py WEBd页操作 分层设计基础目录浏览器操作与WEB操作分开。
一、browseroperator.py 的代码如下
1、初始化函数def __init__(self)初始化浏览相关参数
2、初始化浏览器方法def open_url(self, **kwargs)先判断使用哪种浏览器。
**kwargs是不定长参数dict格式参数只需要传 urlwww.baidu.com ,方法调用只用 opr.open_url(urlwww.baidu.com)打开了浏览器他会返回webdriver的句柄调用处接收到全流程操作网站元素。
暂时还未封装IE 、火狐留给各位朋友们实现吧让我们一起学习
3、def close_browserself, **kwargs关闭浏览器齐活一并封装了
import os
import time
from selenium import webdriver
from common.getconf import Config
from common.getfiledir import BASEFACTORYDIRclass BrowserOperator(object):def __init__(self):self.conf Config()self.driver_path os.path.join(BASEFACTORYDIR, chromedriver.exe)def open_url(self, **kwargs):打开网页:param url::return: 返回 webdrivertry:url kwargs[locator]except KeyError:return False, 没有URL参数try:type self.conf.get(base, browser_type) #从配置文件里取浏览器的类型if type chrome:#处理chrom弹出的info# chrome_options webdriver.ChromeOptions()# #option.add_argument(disable-infobars)# chrome_options.add_experimental_option(excludeSwitches, [enable-automation])# self.driver webdriver.Chrome(optionschrome_options, executable_pathself.driver_path)self.driver webdriver.Chrome(executable_pathself.driver_path)self.driver.maximize_window()self.driver.get(url)elif type IE:print(IE 浏览器)else:print(火狐浏览器)except Exception as e:return False, ereturn True, self.driverdef close_browser(self, **kwargs):关闭浏览器:return:self.driver.quit()return True, 关闭浏览器成功
二、webdriveroperator.py代码如下
1、def __init__(self, driver:Chrome)初始化浏览器返回的deriver句柄
2、内容不一 一 介绍了实现了所有页面的操作定义成功与否判断、日志返回等细节。各位看官细细品尝细节都在代码里每个方法注释大体可以说明了这个方法意义很容易看懂。
还有很多UI操作没有搬运上来留给各位朋友们去实现吧让我们一起学习
import os
import timefrom selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Chrome
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from common.getfiledir import SCREENSHOTDIRclass WebdriverOperator(object):def __init__(self, driver:Chrome):self.driver driverdef get_screenshot_as_file(self):截屏保存:return:返回路径pic_name str.split(str(time.time()), .)[0] str.split(str(time.time()), .)[1] .pngscreent_path os.path.join(SCREENSHOTDIR, pic_name)self.driver.get_screenshot_as_file(screent_path)return screent_pathdef gotosleep(self, **kwargs):time.sleep(3)return True, 等待成功def web_implicitly_wait(self, **kwargs):隐式等待:return:type 存时间try:s kwargs[time]except KeyError:s 10try:self.driver.implicitly_wait(s)except NoSuchElementException:return False, 隐式等待 页面元素未加载完成return True, 隐式等待 元素加载完成def web_element_wait(self, **kwargs):等待元素可见:return:try:type kwargs[type]locator kwargs[locator]except KeyError:return False, 未传需要等待元素的定位参数try:s kwargs[time]except KeyError:s 30try:if type id:WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.ID, locator)))elif type name:WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.NAME, locator)))elif type class:WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.CLASS_NAME, locator)))elif type xpath:WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.XPATH, locator)))elif type css:WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))else:return False, 不能识别元素类型[ type ]except NoSuchElementException:return False, 元素[ locator ]等待出现超时return True, 元素[ locator ]等待出现成功def find_element(self, type, locator, index 0):定位元素:param type::param itor::param index::return:#isinstance(self.driver, selenium.webdriver.Chrome.)type str.lower(type)try:if type id:elem self.driver.find_elements_by_id(locator)[index]elif type name:elem self.driver.find_elements_by_name(locator)[index]elif type class:elem self.driver.find_elements_by_class_name(locator)[index]elif type xpath:elem self.driver.find_elements_by_xpath(locator)[index]elif type css:elem self.driver.find_elements_by_css_selector(locator)[index]else:return False, 不能识别元素类型:[ type ]except Exception:screenshot_path self.get_screenshot_as_file()return False, 获取[ type ]元素[ locator ]失败,已截图[ screenshot_path ].return True, elemdef element_click(self, **kwargs):点击:param kwargs::return:try:type kwargs[type]locator kwargs[locator]except KeyError:return False, 缺少传参try:index kwargs[index]except KeyError:index 0_isOK, _strLOG self.find_element(type, locator, index)if not _isOK: #元素没找到返回失败结果return _isOK, _strLOGelem _strLOGtry:elem.click()except Exception:screenshot_path self.get_screenshot_as_file()return False, 元素[ locator ]点击失败,已截图[ screenshot_path ].return True, 元素[ locator ]点击成功def element_input(self, **kwargs):输入:param kwargs::return:try:type kwargs[type]locator kwargs[locator]text str(kwargs[input])except KeyError:return False, 缺少传参try:index kwargs[index]except KeyError:index 0_isOK, _strLOG self.find_element(type, locator, index)if not _isOK: # 元素没找到返回失败结果return _isOK, _strLOGelem _strLOG# if test ! elem.get_property(type): #校验元素是不是text输入框# screenshot_path self.get_screenshot_as_file()# return False, 元素[ itor ]不是输入框,输入失败,已截图[ screenshot_path ].try:elem.send_keys(text)except Exception:screenshot_path self.get_screenshot_as_file()return False, 元素[ locator ]输入[ text ]失败,已截图[ screenshot_path ].return True, 元素[ locator ]输入[ text ]成功
结语封装了基础类还得实现一个工厂实现统一一个入口执行所有自动化
现在我也找了很多测试的朋友做了一个分享技术的交流群共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源没人解答问题坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化性能安全测试开发等等方面有一定建树的技术大牛
分享他们的经验还会分享很多直播讲座和技术沙龙
可以免费学习划重点开源的
qq群号485187702【暗号csdn11】