1.一般的错误类型
2.查看错误提示,是一个div,以id为例找到该元素
3.修改测试用例
def checkResult(driver,err_id): try: err = driver.find_element_by_id(err_id) print ("ACCOUNT AND PWD ERROR!") print (err.text) except: print ("ACCOUNT AND PWD RIGHT!")
4.新增日志接口,保存数据
#coding:UTF-8 import time class Loginfo(object): """dpath = '',mode = 'w'tring for Loginfo""" def __init__(self, path = '',mode = 'w'): fname = path + time.strftime('%y-%m-%d',time.gmtime()) #文件名字为年月日 self.log = open(path + fname +'.txt',mode) def log_write(self,msg): self.log.write(msg) def log_close(self): self.log.close() if __name__ == '__main__': log = Loginfo() #打开文件 log.log_write('test Loginfo 测试') #写入 log.log_close() #关闭
执行上述代码,可看到同一目录下生成了一个以今天的年月日为标题的文本文件
把代码引入登录脚本中
from selenium import webdriver import time from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from login import get_webinfo from login import get_userinfo from log_module import Loginfo def openBrower(): #配置浏览器 webdriver_handle = webdriver.Firefox() return webdriver_handle def openUrl(handle,url): #打开url handle.get(url) def get_ele_times(driver,times,func): return WebDriverWait(driver,times).until(func) #等待方法 def findElement(driver,arg): ''' arg must be dict 1.login:登录入口 2.user_xpath:用户名 3.pwd_xpath:密码 4.login_xpath:登录按钮 return useEle,pwdEle,loginEle ''' ele_login = get_ele_times(driver,10,lambda driver:driver.find_element_by_xpath(arg['login'])) ele_login.click() useEle = driver.find_element_by_xpath(arg['user_xpath']) pwdEle = driver.find_element_by_xpath(arg['pwd_xpath']) loginEle = driver.find_element_by_xpath(arg['login_xpath']) return useEle,pwdEle,loginEle def sendVals(eletuple,arg): ''' ele tuple account:uname,pwd ''' listkey = ['uname','pwd'] i = 0 for key in listkey: eletuple[i].send_keys('') eletuple[i].clear() eletuple[i].send_keys(arg[key]) i+=1 eletuple[2].click() def checkResult(driver,err_id,arg,log):
time.sleep(2) try: err = driver.find_element_by_id(err_id) print ("ACCOUNT AND PWD ERROR!") msg = '%s %s:error:%s'%(arg['uname'],arg['pwd'],err.text) #保存用户名和密码,错误信息 log.log_write(msg) except: print ("ACCOUNT AND PWD RIGHT!") msg = '%s %s:pass:'%(arg['uname'],arg['pwd']) #保存用户名和密码
log.log_write(msg) def login_test(ele_dict,user_list): driver = openBrower() log = Loginfo() openUrl(driver,ele_dict['url']) driver.maximize_window() ele_tuple = findElement(driver,ele_dict) #接收字典的值 for arg in user_list: sendVals(ele_tuple,arg) checkResult(driver,ele_dict['errorid'],arg,log) log.close() if __name__ == '__main__': url = 'http://www.maiziedu.com/' account = 'xxx' pwd = 'maizi123456' ''' ele_dict = {'url':url,'login':'/html/body/div[2]/div/div/div/div/a[2]','user_xpath':'//*[@id="id_account_l"]', 'pwd_xpath':'//*[@id="id_password_l"]','login_xpath':'//*[@id="login_btn"]', 'errorid':'该账号不正确'} user_list = [{'uname':account,'pwd':pwd}] ''' ele_dict = get_webinfo(r'C:UsersxxxDesktopwebinfo.txt') user_list = get_userinfo(r'C:UsersxxxDesktopuserinfo.txt') #file webinfo/userinfo ele_dict = get_webinfo(path) user_list = get_userinfo(path) login_test(ele_dict,user_list)
5.多用户测试
先增加注销操作
def checkResult(driver,err_id,arg,log): result = False try: err = driver.find_element_by_id(err_id) print ("ACCOUNT AND PWD ERROR!") msg = 'uname=%s pwd=%s:error:%s '%(arg['uname'],arg['pwd'],err.text) log.log_write(msg) except: print ("ACCOUNT AND PWD RIGHT!") msg = 'uname=%s pwd=%s:pass '%(arg['uname'],arg['pwd']) log.log_write(msg) result = True return result def logout(driver,ele_dict): driver.find_element_by_class_name(ele_dict['usermenu']).click() driver.find_element_by_link_text(ele_dict['logout']).click() def login_test(ele_dict,user_list): driver = openBrower() log = Loginfo() openUrl(driver,ele_dict['url']) driver.maximize_window() ele_tuple = findElement(driver,ele_dict) #接收字典的值 for arg in user_list: sendVals(ele_tuple,arg) result = checkResult(driver,ele_dict['errorid'],arg,log) if result: logout(driver,ele_dict) #logout ele_tuple = findElement(driver,ele_dict) #login log.log_close()
如果想测试用户名或密码为空的情况,要修改userinfo(uname=1;pws=2),以‘;’为分隔