测试用例模块化登录百度云:
#导入网页模块功能:
from selenium import webdriver
import time
#导入活动模块功能:
from selenium.webdriver.common.action_chains import ActionChains
#导入等待模块功能:
from selenium.webdriver.support.ui import WebDriverWait
url = "https://www.baidu.com/"
login_text = "登录"
account = "你的百度云账号"
pwd = "你的百度云密码"
#定义等待时间功能:
def get_ele_times(driver,times,func):
return WebDriverWait(driver,times).until(func)
#定义打开浏览器的功能并返回句柄:
def openBrower():
webdriver_handle = webdriver.Firefox()
return webdriver_handle #返回句柄
#定义加载URL功能:
def openUrl(handle,url):
handle.get(url)
#定义查找元素功能:
def findElement(d,arg):
"""
:param d: 文件句柄
:param arg: 必须是字典
:return:
"""
# text_id = "登录"
# if text_id in arg:
#生成等待时间并找到text_id是登录的:
get_ele_times(d,1,lambda d:d.find_element_by_link_text("登录")).click()
time.sleep(1)
#找到用户名登录的css路径:
d.find_element_by_css_selector("html body div#passport-login-pop.tang-pass-pop-login-merge.tang-pass-pop-login-tpl-mn."
"tang-pass-pop-login-color-blue.tang-pass-pop-login div#TANGRAM__PSP_4__foreground.tang-foreground "
"div#TANGRAM__PSP_4__body.tang-body div#TANGRAM__PSP_4__content.tang-content "
"div#passport-login-pop-dialog div.clearfix div.pass-login-pop-content "
"div.pass-login-pop-form div.tang-pass-footerBar p#TANGRAM__PSP_10__footerULoginBtn."
"tang-pass-footerBarULogin.pass-link").click()
time.sleep(1)
useEle = d.find_element_by_id(arg["userid"])
pwdEle = d.find_element_by_id(arg["pwdid"])
loginEle = d.find_element_by_id(arg["loginid"])
return useEle,pwdEle,loginEle
#定义发送接口函数:
def sendVals(eletuple,arg):
"""
:param eletuple:元组
:param arg: 字典格式:uname、pwd
:return:
"""
listkey = ["uname","pwd"]
i = 0
for key in listkey:
eletuple[i].send_keys("")
eletuple[i].clear()
time.sleep(1)
eletuple[i].send_keys(arg[key])
i += 1
time.sleep(1)
eletuple[2].click()
#定义检查结果功能:
def checkResult(d,text):
#设置抛出异常:
try:
d.find_element_by_link_text("用户名或密码错误!")
print("用户名或密码错误!")
except:
print("登录成功!")
#定义登录测试入口功能:
def login_test():
d = openBrower()
openUrl(d,url) #打开URL功能传入句柄和URL
ele_dict = {"text_id":login_text,"userid":"TANGRAM__PSP_10__userName","pwdid":"TANGRAM__PSP_10__password","loginid":"TANGRAM__PSP_10__submit"}
#定义实际数据字典的形式:
account_dict = {"uname":account,"pwd":pwd,}
ele_tuple = findElement(d,ele_dict) #运行的结果是元组
sendVals(ele_tuple,account_dict)
checkResult(d,"用户名或密码错误!")
#定义主函数:
if __name__ == '__main__':
for i in range(5):
login_test()