【python+selenium-01】基础版本演变为关键字驱动
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【python+selenium-01】基础版本演变为关键字驱动,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3708字,纯文字阅读大概需要6分钟。
内容图文
![【python+selenium-01】基础版本演变为关键字驱动](/upload/InfoBanner/zyjiaocheng/608/338675f6c9c04d3ba4598f23d5cb01aa.jpg)
最初(基础)版本的UI自动化脚本样式如下:
(以下均以百度首页为例)
from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.implicitly_wait(30) driver.get("http://www.baidu.com") driver.find_element_by_id("kw").send_keys("python") driver.find_element_by_id("su").click() sleep(3) driver.quit()
通过selenium支持的基础功能,定位元素然后对于的send_keys() 或者 click()操作。
关键字驱动
什么是关键字驱动?
基于selenium底层的方法,我们在写自动化代码时,实际上反复的调用find_element_by_id,*by_xpath,.send_keys()或者 .click(),诸如此类的方法反反复复的调用。
而每次调用的区别就在于什么操作,以什么方式,定位什么元素,需要传什么参数。
所以我们做了封装,只需要传入有区别的参数进去即可。
为什么要实现关键字驱动?
- 减少代码冗余;
- 减少代码过多重复;
- 重点突出"关键字",便于维护;
- 增强可阅读性;
封装好的类:
from selenium import webdriver import time # 浏览器初始化 def open_browser(name, url): if name == "chrome": driver = webdriver.Chrome() print("starting for Chrome!") elif name == "firefox": driver = webdriver.Firefox() print("starting for Firefox!") elif name == "ie": driver = webdriver.Ie() print("starting for IE!") driver.get(url) driver.maximize_window() return driver class WebUIInit(object): # 初始化 def __init__(self, name, url): self.driver = open_browser(name, url) # 关闭浏览器 def quit(self): self.driver.quit() # 依据不同的定位方法,进行输入操作 def send_keys(self, locator_type, locator, text): if locator_type == "id": self.driver.find_element_by_id(locator).send_keys(text) elif locator_type == "name": self.driver.find_element_by_name(locator).send_keys(text) elif locator_type == "xpath": self.driver.find_element_by_xpath(locator).send_keys(text) # 依据不同的定位方法,进行点击操作 def click(self, locator_type, locator): if locator_type == "id": self.driver.find_element_by_id(locator).click() elif locator_type == "name": self.driver.find_element_by_name(locator).click() elif locator_type == "xpath": self.driver.find_element_by_xpath(locator).click() # 定义强制等待 def sleep(self, seconds): time.sleep(seconds) # 定义隐式等待 def wait(self, seconds): self.driver.implicitly_wait(seconds) # 切换至新窗口 def switch_to_new_window(self): handles = self.driver.window_handles self.driver.switch_to_window(handles[1]) # 关闭旧窗口 def close_old_window(self): self.driver.close() # 切换至旧窗口 def switch_to_old_window(self): handles = self.driver.window_handles self.driver.switch_to_window(handles[0]) # 切换新窗口,关闭旧窗口 def switch_to_new_and_close_old_window(self): handles = self.driver.window_handles self.driver.close() self.driver.switch_to_window(handles[1]) # 获取元素文本内容进行断言校验 def assert_by_text(self, locator_type, locator, text): if locator_type == "id": element_text = self.driver.find_element_by_id(locator).text elif locator_type == "name": element_text = self.driver.find_element_by_name(locator).text elif locator_type == "xpath": element_text = self.driver.find_element_by_xpath(locator).text if element_text == text: print("daladala") else: print(element_text + " VS " + text) print("wrong!") # 切换iframe窗体 def switch_to_iframe(self, locator_type, locator): if locator_type == "id": self.driver.switch_to.frame(self.driver.find_element_by_id(locator)) elif locator_type == "name": self.driver.switch_to.frame(self.driver.find_element_by_name(locator)) elif locator_type == "xpath": self.driver.switch_to.frame(self.driver.find_element_by_xpath(locator)) # 切换回默认iframe窗体 def switch_to_default(self): self.driver.switch_to_default_content()
基于关键字驱动的封装类,再次实现百度首页的UI自动化测试代码:
from auto.tool.webui_autotest import * from time import sleep driver = WebUIInit("firefox", "http://www.baidu.com") driver.send_keys("id", "kw", "python") driver.click("id", "su") sleep(3) driver.quit()
内容总结
以上是互联网集市为您收集整理的【python+selenium-01】基础版本演变为关键字驱动全部内容,希望文章能够帮你解决【python+selenium-01】基础版本演变为关键字驱动所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。