自动测试脚本,多用于regression testing
需要安装Python 和 Selenium
http://selenium-python.readthedocs.io/Python 用的IDE: AptanaStudio
Selenium 默认浏览器是firefox, 如果用Chrome或者IE 需要自己下 web driver
IE
http://stackoverflow.com/questions/24925095/selenium-python-internet-explorer如果用的是IE浏览器,输入速度非常慢。需要下面的代码优化一下
caps = DesiredCapabilities.INTERNETEXPLORER
caps['requireWindowFocus'] = True
driver_ie="C:\\Users\\yaqi.zhang\\Downloads\\\IEDriverServer_Win32_2.42.0\\IEDriverServer.exe";
driver=webdriver.Ie(driver_ie,capabilities=caps)
- 一般用来寻找tag的语句如下
我所用到的所有库
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
by_name
driver.find_element_by_name('btnOK').click()
by_id
driver.find_element_by_id('btnOK').click()
by_xpath
driver.find_element_by_xpath("//input[@value='Save']").click()
在table中选择
tr=driver.find_element_by_xpath("//tbody[@id='ListBody']/tr[1]")
td=tr.find_elements(By.TAG_NAME, "td")[2]
if td.text =='':
return
通过css选元素
tr = driver.find_element_by_css_selector('tr.Selectable')
tr.click()
time.sleep (sleep_sec)
处理Alter
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
def AcceptAlert(driver):
try:
WebDriverWait(driver, 3).until(EC.alert_is_present(),
'Timed out waiting for PA creation ' +
'confirmation popup to appear.')
alert = driver.switch_to_alert()
alert.accept()
print ("alert accepted")
except TimeoutException:
print ("no alert")
处理弹出的其他窗口
main_window_handle = None
while not main_window_handle:
main_window_handle = driver.current_window_handle
driver.find_element_by_name('CMD_TEMPLATES').click()
time.sleep(sleep_sec)
signin_window_handle = None
while not signin_window_handle:
for handle in driver.window_handles:
if handle != main_window_handle:
signin_window_handle = handle
break
driver.switch_to.window(signin_window_handle)
driver.find_element_by_name('bcnClose').click()
driver.switch_to.window(main_window_handle)
所寻找的网页元素不存在
try:
tr = driver.find_element_by_css_selector('tr.Selectable')
tr.click()
time.sleep (sleep_sec)
tr=driver.find_element_by_xpath("//tbody[@id='ListBody']/tr[1]")
td=tr.find_elements(By.TAG_NAME, "td")[2]
if td.text =='':
print('There is no policy')
return
else:
tr.click()
time.sleep (sleep_sec_short)
driver.find_element_by_name('btnOK').click()
print('Choose existing policy')
except NoSuchElementException:
print('there is no item')
return
以上是比较基础的东西,我用了一个礼拜写完完整的测试脚本。Regression 从原来的十天,下降到一天半到两天。
1万+



被折叠的 条评论
为什么被折叠?



