2019-12-31:自动化测试用例编写
使用selenium,cmd安装命令:pip install selenium
笔者安装的版本是 3.141.0,查看Python安装的各种包的命令:pip list安装selenium3.0以上需要安装浏览器对应驱动:
1.Chrome浏览器驱动
2.FireFox浏览器驱动
3.Opera浏览器驱动
4.IE浏览器驱动把这些驱动丢到Python.exe同目录下
接着尝试一下下面代码,对应浏览器打开则驱动安装成功
from selenium import webdriver
driver = webdriver.Chrome()
- 对工作中要处理的重复步骤归纳并编写流程代码:出于隐私处理了用户名和密码,以及涉及的网址
'''
@CreatTime : 2019/12/31 11:34:37
@Author : FireRain.wyt
@Description : open create projoct + admin audit project
'''
# here put the import lib
from selenium import webdriver
import time
openuser='user'
project_name=time.strftime("%p%m%d", time.localtime()) # use localtime as project name
opencase_time=time.strftime("%Y-%m-%d", time.localtime())
localtime_detail=time.strftime("%b %a %Y-%m-%d %H:%M:%S", time.localtime())
text_template='This is a word template. As a testers, I am tried of repetitive input. In order to free my hands, I create this template at '
open_url= 'testurl'
jszc_url= 'testurl'
GD= webdriver.Chrome()
GD.get(open_url)
GD.find_element_by_name("username").send_keys("user")
GD.find_element_by_name("password").send_keys("pass")
GD.find_element_by_xpath("//*[@id='app']/div/div[1]/div/form/button").click()
GD.get(jszc_url)
time.sleep(2) # 此处 隐式等待 不起作用 改用sleep等待页面加载完全 再查找 新建项目 按钮
GD.execute_script('document.getElementsByTagName("button")[12].click()') # 由于xpath无法找到button,换用js查找
time.sleep(1)
# element框架下拉选择没有使用option 用的是自定义li样式,需要点击加载数据再继续
# fill form
GD.implicitly_wait(5)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[1]/div[2]/div/div/div[1]/input").click()
GD.find_element_by_xpath("/html/body/div[2]/div[1]/div[1]/ul/li[1]").click()
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[1]/div[3]/div/div[1]/input").send_keys(project_name)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[2]/div[1]/div/div/input").send_keys(openuser)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[2]/div[1]/div/div/input").send_keys(openuser)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[2]/div[2]/div/div/div[1]/input").click()
GD.find_element_by_xpath("/html/body/div[3]/div[1]/div[1]/ul/li[1]").click()
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[2]/div[3]/div/div/div[1]/input").click()
GD.find_element_by_xpath("/html/body/div[4]/div[1]/div[1]/ul/li[1]").click()
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[1]/div[5]/div/div/input").send_keys(opencase_time)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[2]/div[5]/div/div/input").send_keys(opencase_time)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[1]/div[6]/div/div/input").send_keys("3")
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[2]/div[6]/div/div/input").send_keys(opencase_time)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[1]/div[1]/div[7]/div/div/input").send_keys(opencase_time)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[2]/div/div[1]/div/div/textarea").send_keys(text_template,localtime_detail)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[2]/div/div[2]/div/div/textarea").send_keys(text_template,localtime_detail)
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[2]/div/div[3]/div/div/textarea").send_keys(text_template,localtime_detail)
# submit form
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[4]/div/label/span[1]/span").click()
GD.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/section/div/main/form/div[5]/div/button[1]").click()
time.sleep(2)
GD.quit()
附:今天是2019最后一天,公司发了元旦下午茶,汉堡王套餐,开心✿✿ヽ(°▽°)ノ✿