完善融宝UI自动化框架
继续总结自动化测试API
"29.模拟键盘单个按键/组合按键操作,"
# import time
# from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
# url="https://www.sogou.com"
# driver=webdriver.Chrome()
# driver.get(url)
# driver.find_element_by_id("query").send_keys("自动")
# # 删除多输入的一个 动
# driver.find_element_by_id("query").send_keys(Keys.BACK_SPACE)
# time.sleep(3)
# # 输入空格键+“动化”
# driver.find_element_by_id("query").send_keys(Keys.SPACE)
# driver.find_element_by_id("query").send_keys("动化")
# time.sleep(3)
# # ctrl+a 全选输入框内容
# driver.find_element_by_id("query").send_keys(Keys.CONTROL, 'a')
# time.sleep(3)
# # ctrl+x 剪切输入框内容
# driver.find_element_by_id("query").send_keys(Keys.CONTROL, 'x')
# time.sleep(3)
# # ctrl+v 粘贴内容到输入框
# driver.find_element_by_id("query").send_keys(Keys.CONTROL, 'v')
# time.sleep(3)
# # 点击键盘向下箭头
# driver.find_element_by_id("query").send_keys(Keys.ARROW_DOWN)
# time.sleep(3)
# driver.find_element_by_id("query").send_keys(Keys.ARROW_DOWN)
# # 通过回车键来代替单击操作
# driver.find_element_by_id("query").send_keys(Keys.ENTER)
"30.模拟组合按键操作,"
"""第一种方式,设置并读取剪切板,键盘按下;键盘释放"""
# from selenium.webdriver.common.keys import Keys
# from selenium import webdriver
# from selenium.webdriver import ActionChains
# import win32con
# import win32clipboard as w
# import time
# import win32api
#
# def setText(sourceStr):
# "设置剪切板内容"
# w.OpenClipboard()
# w.EmptyClipboard()
# w.SetClipboardData(win32con.CF_UNICODETEXT,sourceStr)
# w.CloseClipboard()
#
# def getText():
# "读取剪切板内容"
# w.OpenClipboard()
# content=w.GetClipboardData(win32con.CF_TEXT)
# w.CloseClipboard()
# return content
#
# VK_CODE={"enter":0x0D,"ctrl":0x11,"a":0x41,"v":0x56,"x":0x58}
#
# def keyDown(keyName):
# "键盘键按下"
# win32api.keybd_event(VK_CODE[keyName],0,0,0)
#
# def keyUp(keyName):
# "键盘键抬起"
# win32api.keybd_event(VK_CODE[keyName],0,win32con.KEYEVENTF_KEYUP,0)
#
# # 程序代码:先将要剪切的内容设置到剪切板中,然后再剪切出来,粘贴;
# url="http:www.sogou.com"
# driver=webdriver.Chrome()
# driver.get(url)
# # 定义即将要被设置到剪切板中的内容
# content="测试之路"
# # 将content变量中的内容设置到剪切板中
# setText(content)
# # 从剪切板中读取设置到剪切板中的内容
# getText=getText()
#
# driver.find_element_by_id('query').click()
# # 按下Ctrl+v组合键
# keyDown("ctrl")
# keyDown("v")
# # 释放Ctrl+v组合键
# keyUp("ctrl")
# keyUp("v")
#
# time.sleep(2)
# driver.find_element_by_id("stb").click()
# 或者:
# 全选
# keyDown("ctrl")
# keyDown("a")
# keyUp("ctrl")
# keyUp("a")
# 剪切
# keyDown("ctrl")
# keyDown("x")
# keyUp("ctrl")
# keyUp("x")
# 粘贴
# keyDown("ctrl")
# keyDown("v")
# keyUp("ctrl")
# keyUp("v")
"""第二种方式:全选、剪切、复制"""
# from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
# from selenium.webdriver import ActionChains
#
# url="http://www.sogou.com"
# driver=webdriver.Chrome()
# driver.get(url)
# driver.find_element_by_id("query").send_keys("测试开发")
# # 全选
# ActionChains(driver).key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform()
# # 剪切
# ActionChains(driver).key_down(Keys.CONTROL).send_keys("x").key_up(Keys.CONTROL).perform()
# driver.get("http://www.baidu.com")
# driver.find_element_by_id("kw").click()
# # 复制
# ActionChains(driver).key_down(Keys.CONTROL).send_keys("v").key_up(Keys.CONTROL).perform()
# "31.模拟鼠标右键,未实践成功;不过是一个思路,可用键盘事件另外的操作实现"
# from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
# import win32con
# import win32clipboard as w
# from selenium.webdriver import ActionChains
# import time
#
# def setText(sourceStr):
# "设置剪切板内容"
# w.OpenClipboard()
# w.EmptyClipboard()
# w.SetClipboardData(win32con.CF_UNICODETEXT,sourceStr)
# w.CloseClipboard()
#
# url="http://www.sogou.com"
# driver=webdriver.Ie()
# driver.get(url)
# setText("测试开发")
# driver.maximize_window()
# searchBox=driver.find_element_by_id("query")
# searchBox.click()
# # 执行鼠标右键点击操作
# # time.sleep(2)
# ActionChains(driver).context_click(searchBox).perform()
# # time.sleep(2)
#
# # 发送粘贴命令,字符P指代粘贴操作
# ActionChains(driver).send_keys("P").perform()
# driver.find_element_by_id("stb").click()
"32.模拟鼠标左键按下与释放"
# from selenium import webdriver
# from selenium.webdriver import ActionChains
# import time
#
# url="http://127.0.0.1/test_mouse.html"
# driver=webdriver.Chrome()
# driver.get(url)
# div=driver.find_element_by_id("div1")
# for i in range(2):
# # 在定位元素上执行按下鼠标左键,并保持
# ActionChains(driver).click_and_hold(div).perform()
# time.sleep(2)
# # 释放一直按下的鼠标左键
# ActionChains(driver).release(div).perform()
# time.sleep(2)
"33.保持鼠标停留在某个元素上"
# from selenium import webdriver
# from selenium.webdriver import ActionChains
# import time
# url="http://127.0.0.1/test_mouse_hover.html"
# driver=webdriver.Chrome()
# driver.get(url)
# # find_element_by_partial_link_text就是选择这个元素的link text中一部分字段,
# # 相当于模糊匹配,不过字段选择要具有唯一性
# link1=driver.find_element_by_partial_link_text("指过来1")
# link2=driver.find_element_by_partial_link_text("指过来2")
# p=driver.find_element_by_xpath('//p[contains(text(),"消失了")]')
# ActionChains(driver).move_to_element(link1).perform()
# time.sleep(2)
# ActionChains(driver).move_to_element(p).perform()
# time.sleep(2)
# ActionChains(driver).move_to_element(link2).perform()
# time.sleep(2)
# ActionChains(driver).move_to_element(p).perform()
# time.sleep(2)
"34.判断页面元素是否存在,封装函数"
# from selenium import webdriver
# from selenium.common.exceptions import NoSuchElementException
#
# def isElementPresent(driver,by,value):
# "判断元素是否存在,存在则返回True,不存在则返回False"
# try:
# driver.find_element(by,value)
# except NoSuchElementException:
# return False
# else:
# return True
#
# """
# 参数by 可以为
# ID = "id"
# XPATH = "xpath"
# LINK_TEXT = "link text"
# PARTIAL_LINK_TEXT = "partial link text"
# NAME = "name"
# TAG_NAME = "tag name"
# CLASS_NAME = "class name"
# CSS_SELECTOR = "css selector"
# """
#
# driver=webdriver.Chrome()
# driver.get("https://www.baidu.com")
# res=isElementPresent(driver,"id","kw")
# if res == True:
# print ("查找的元素存在页面上")
# else:
# print ("查找的元素不存在页面上")
"35.隐式等待,针对页面的,等待页面加载完成,设定最长等待时间,超出则引发TimeoutException"
# from selenium import webdriver
# import time
#
# driver=webdriver.Chrome()
# driver.implicitly_wait(5)
# driver.get("https://www.baidu.com")
"36.显示等待,针对元素的"
"""
selenium.webdriver.support.wait.WebDriverWait(类)
__init__
driver: 传入WebDriver实例,即我们上例中的driver
timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)
poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒
ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,
则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。
until
method: 在等待期间,每隔一段时间调用这个传入的方法,直到返回值不是False
message: 如果超时,抛出TimeoutException,将message传入异常
until_not 与until相反,until是当某元素出现或什么条件成立则继续执行,until_not是当某元素消失或什么条件不成立则继续执行,参数也相同,不再赘述。
method
message
"""
# from selenium import webdriver
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.common.by import By
# from selenium.common.exceptions import NoSuchElementException,TimeoutException
# import traceback
# import time
#
# driver=webdriver.Chrome()
# driver.get("https:www.sogou.com")
# wait=WebDriverWait(driver,5,0.2)
# wait.until(EC.title_is("搜狗搜索引擎 - 上网从搜狗开始"))
# try:
# wait.until(EC.visibility_of(driver.find_element_by_id("query"))).send_keys("显示等待")
# # wait.until(EC.element_to_be_clickable((By.ID,"stb"))).click()
# wait.until(lambda x:x.find_element_by_id("stb")).click()
# except NoSuchElementException:
# print ("元素不存在")
# except TimeoutException:
# print ("超时")
# except Exception:
# print (traceback.print_exc())
# else:
# print ("搜索成功")
# driver.quit()
"37.使用title属性识别或关键字识别来操作新弹出的浏览器窗口"
# import time
# import unittest
# import traceback
# from selenium import webdriver
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.common.by import By
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.common.exceptions import TimeoutException,NoSuchElementException
#
# class VisitSogouByChrome(unittest.TestCase):
#
# def setUp(self):
# self.driver=webdriver.Chrome()
# self.wait=WebDriverWait(self.driver,10)
#
# def test_identifyWindowHandleByTitle(self):
# url="http://127.0.0.1/test_popup_window.html"
# self.driver.get(url)
# self.wait.until(EC.element_to_be_clickable((By.XPATH,'//a[text()="sogou 搜索"]'))).click()
# all_handles=self.driver.window_handles
# # print (self.driver.current_window_handle)
# try:
# for handle in all_handles:
# self.driver.switch_to.window(handle)
# time.sleep(2)
# # 利用关键字来识别
# if "搜狗搜索" in self.driver.page_source:
# # 利用页面title属性来识别
# # if self.driver.title=="搜狗搜索引擎 - 上网从搜狗开始":
# self.wait.until(lambda x:x.find_element_by_id("query")).send_keys("自动化")
# self.wait.until(EC.element_to_be_clickable((By.ID,"stb"))).click()
# time.sleep(2)
# except TimeoutException:
# print ("超时")
# except NoSuchElementException:
# print ("未找到元素")
# except:
# print (traceback.format_exc())
# else:
# self.driver.switch_to.window(all_handles[0])
# time.sleep(2)
# self.assertEqual(self.driver.title,"你喜欢的水果","网页标题不同")
#
# def tearDown(self):
# self.driver.quit()
#
# if __name__=="__main__":
# unittest.main()
"38.操作frame中的页面元素"
# import time
# from selenium import webdriver
# from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.common.by import By
#
# url="http://127.0.0.1/frameset.html"
# driver=webdriver.Chrome()
# driver.get(url)
# # 1.用frame的索引来定位,第一个是0
# driver.switch_to.frame(0)
# driver.find_element_by_xpath('//input[@type="button"]').click()
# alertWindow=WebDriverWait(driver,10).until(EC.alert_is_present())
# time.sleep(2)
# alertWindow.accept()
# # 从frame中切回主文档
# driver.switch_to.default_content()
#
# # 2.用frame的id来定位
# driver.switch_to.frame("middleframe")
# inputBox=WebDriverWait(driver,5).until(EC.visibility_of(driver.find_element_by_xpath('//input[@type="text"]')))
# inputBox.send_keys("测试")
# driver.switch_to.default_content()
# time.sleep(2)
#
# # 3.用WebElement对象来定位;通过标签名找到页面中所有的frame元素,然后通过索引进入该frame
# driver.switch_to.frame(driver.find_elements_by_tag_name("frame")[2])
# WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.ID,"java"))).click()
# time.sleep(2)
# driver.switch_to.default_content()
"39.使用frame中的html源码内容操作frame"
# from selenium import webdriver
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
#
# url="http://127.0.0.1/frameset.html"
# driver=webdriver.Chrome()
# driver.get(url)
# frames=driver.find_elements_by_tag_name("frame")
# # print (frames) # 返回的是列表类型
# for frame in frames:
# driver.switch_to.frame(frame)
# if "中间 frame" in driver.page_source:
# print ("切换成功")
# 操作frame中的页面元素
# inputBox=WebDriverWait(driver,5).until(EC.visibility_of(driver.find_element_by_xpath('//input[@type="text"]')))
# inputBox.send_keys("测试")
# break
# else:
# driver.switch_to.default_content()
"40.操作javascript的alert弹窗"
import time
from selenium import webdriver
from selenium.common.exceptions import NoAlertPresentException
url="http://127.0.0.1/test_alert.html"
driver=webdriver.Chrome()
driver.get(url)
driver.find_element_by_id("button").click()
try:
# 获取alert对象
alert=driver.switch_to.alert
print (alert.text) # 获取alert框的提示语
alert.accept() # 关闭alert框
time.sleep(2)
except NoAlertPresentException:
print ("alert框未被找到")
else:
driver.quit()