当前位置:   article > 正文

Python+Selenium WebUI自动化框架 -- 基础操作封装_webui自动化多窗口切换如何封装

webui自动化多窗口切换如何封装

前言:

封装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格式,参数只需要传 url='www.baidu.com' ,方法调用只用 opr.open_url(url='www.baidu.com'),打开了浏览器,他会返回webdriver的句柄,调用处接收到全流程操作网站元素。

暂时还未封装IE 、火狐,留给各位朋友们实现吧,让我们一起学习

3、def close_browser(self, **kwargs)关闭浏览器,齐活,一并封装了

  1. import os
  2. import time
  3. from selenium import webdriver
  4. from common.getconf import Config
  5. from common.getfiledir import BASEFACTORYDIR
  6. class BrowserOperator(object):
  7. def __init__(self):
  8. self.conf = Config()
  9. self.driver_path = os.path.join(BASEFACTORYDIR, 'chromedriver.exe')
  10. def open_url(self, **kwargs):
  11. """
  12. 打开网页
  13. :param url:
  14. :return: 返回 webdriver
  15. """
  16. try:
  17. url = kwargs['locator']
  18. except KeyError:
  19. return False, '没有URL参数'
  20. try:
  21. type = self.conf.get('base', 'browser_type') #从配置文件里取浏览器的类型
  22. if type == 'chrome':
  23. #处理chrom弹出的info
  24. # chrome_options = webdriver.ChromeOptions()
  25. # #option.add_argument('disable-infobars')
  26. # chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
  27. # self.driver = webdriver.Chrome(options=chrome_options, executable_path=self.driver_path)
  28. self.driver = webdriver.Chrome(executable_path=self.driver_path)
  29. self.driver.maximize_window()
  30. self.driver.get(url)
  31. elif type == 'IE':
  32. print('IE 浏览器')
  33. else:
  34. print('火狐浏览器')
  35. except Exception as e:
  36. return False, e
  37. return True, self.driver
  38. def close_browser(self, **kwargs):
  39. """
  40. 关闭浏览器
  41. :return:
  42. """
  43. self.driver.quit()
  44. return True, '关闭浏览器成功'

二、webdriveroperator.py代码如下

1、def __init__(self, driver:Chrome),初始化浏览器返回的deriver句柄,

2、内容不一 一 介绍了,实现了所有页面的操作,定义成功与否判断、日志返回等细节。各位看官细细品尝,细节都在代码里,每个方法注释大体可以说明了这个方法意义,很容易看懂。

还有很多UI操作没有搬运上来,留给各位朋友们去实现吧,让我们一起学习

  1. import os
  2. import time
  3. from selenium.common.exceptions import NoSuchElementException
  4. from selenium.webdriver import Chrome
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.webdriver.common.by import By
  8. from common.getfiledir import SCREENSHOTDIR
  9. class WebdriverOperator(object):
  10. def __init__(self, driver:Chrome):
  11. self.driver = driver
  12. def get_screenshot_as_file(self):
  13. """
  14. 截屏保存
  15. :return:返回路径
  16. """
  17. pic_name = str.split(str(time.time()), '.')[0] + str.split(str(time.time()), '.')[1] + '.png'
  18. screent_path = os.path.join(SCREENSHOTDIR, pic_name)
  19. self.driver.get_screenshot_as_file(screent_path)
  20. return screent_path
  21. def gotosleep(self, **kwargs):
  22. time.sleep(3)
  23. return True, '等待成功'
  24. def web_implicitly_wait(self, **kwargs):
  25. """
  26. 隐式等待
  27. :return:
  28. type 存时间
  29. """
  30. try:
  31. s = kwargs['time']
  32. except KeyError:
  33. s = 10
  34. try:
  35. self.driver.implicitly_wait(s)
  36. except NoSuchElementException:
  37. return False, '隐式等待 页面元素未加载完成'
  38. return True, '隐式等待 元素加载完成'
  39. def web_element_wait(self, **kwargs):
  40. """
  41. 等待元素可见
  42. :return:
  43. """
  44. try:
  45. type = kwargs['type']
  46. locator = kwargs['locator']
  47. except KeyError:
  48. return False, '未传需要等待元素的定位参数'
  49. try:
  50. s = kwargs['time']
  51. except KeyError:
  52. s = 30
  53. try:
  54. if type == 'id':
  55. WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.ID, locator)))
  56. elif type == 'name':
  57. WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.NAME, locator)))
  58. elif type == 'class':
  59. WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.CLASS_NAME, locator)))
  60. elif type == 'xpath':
  61. WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.XPATH, locator)))
  62. elif type == 'css':
  63. WebDriverWait(self.driver, s, 0.5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))
  64. else:
  65. return False, '不能识别元素类型[' + type + ']'
  66. except NoSuchElementException:
  67. return False, '元素[' + locator + ']等待出现超时'
  68. return True, '元素[' + locator + ']等待出现成功'
  69. def find_element(self, type, locator, index = 0):
  70. """
  71. 定位元素
  72. :param type:
  73. :param itor:
  74. :param index:
  75. :return:
  76. """
  77. #isinstance(self.driver, selenium.webdriver.Chrome.)
  78. type = str.lower(type)
  79. try:
  80. if type == 'id':
  81. elem = self.driver.find_elements_by_id(locator)[index]
  82. elif type == 'name':
  83. elem = self.driver.find_elements_by_name(locator)[index]
  84. elif type == 'class':
  85. elem = self.driver.find_elements_by_class_name(locator)[index]
  86. elif type == 'xpath':
  87. elem = self.driver.find_elements_by_xpath(locator)[index]
  88. elif type == 'css':
  89. elem = self.driver.find_elements_by_css_selector(locator)[index]
  90. else:
  91. return False, '不能识别元素类型:[' + type + ']'
  92. except Exception:
  93. screenshot_path = self.get_screenshot_as_file()
  94. return False, '获取[' + type + ']元素[' + locator + ']失败,已截图[' + screenshot_path + '].'
  95. return True, elem
  96. def element_click(self, **kwargs):
  97. """
  98. 点击
  99. :param kwargs:
  100. :return:
  101. """
  102. try:
  103. type = kwargs['type']
  104. locator = kwargs['locator']
  105. except KeyError:
  106. return False, '缺少传参'
  107. try:
  108. index = kwargs['index']
  109. except KeyError:
  110. index = 0
  111. _isOK, _strLOG = self.find_element(type, locator, index)
  112. if not _isOK: #元素没找到,返回失败结果
  113. return _isOK, _strLOG
  114. elem = _strLOG
  115. try:
  116. elem.click()
  117. except Exception:
  118. screenshot_path = self.get_screenshot_as_file()
  119. return False, '元素['+ locator +']点击失败,已截图[' + screenshot_path + '].'
  120. return True, '元素['+ locator +']点击成功'
  121. def element_input(self, **kwargs):
  122. """
  123. 输入
  124. :param kwargs:
  125. :return:
  126. """
  127. try:
  128. type = kwargs['type']
  129. locator = kwargs['locator']
  130. text = str(kwargs['input'])
  131. except KeyError:
  132. return False, '缺少传参'
  133. try:
  134. index = kwargs['index']
  135. except KeyError:
  136. index = 0
  137. _isOK, _strLOG = self.find_element(type, locator, index)
  138. if not _isOK: # 元素没找到,返回失败结果
  139. return _isOK, _strLOG
  140. elem = _strLOG
  141. # if 'test' != elem.get_property('type'): #校验元素是不是text输入框
  142. # screenshot_path = self.get_screenshot_as_file()
  143. # return False, '元素['+ itor +']不是输入框,输入失败,已截图[' + screenshot_path + '].'
  144. try:
  145. elem.send_keys(text)
  146. except Exception:
  147. screenshot_path = self.get_screenshot_as_file()
  148. return False, '元素['+ locator +']输入['+ text +']失败,已截图[' + screenshot_path + '].'
  149. return True, '元素['+ locator +']输入['+ text +']成功'

结语:封装了基础类,还得实现一个工厂,实现统一一个入口执行所有自动化

  1. ​现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
  2. 如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
  3. 可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
  4. 分享他们的经验,还会分享很多直播讲座和技术沙龙
  5. 可以免费学习!划重点!开源的!!!
  6. qq群号:485187702【暗号:csdn11

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/821027
推荐阅读
相关标签
  

闽ICP备14008679号