Python实战之Selenium自动化测试web登录(2)
#!/usr/bin/env python3 # -*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains import time from userdata import get_webinfo, get_userinfo, XlUserInfo from log_module import Loginfo, Xlloginfo def get_ele_times(driver, times, func): return WebDriverWait(driver, times).until(func) def openBrower(): ''' :return: webdriver handle ''' webdriver_handle = webdriver.Firefox() return webdriver_handle # def openUrl(handle, url): # handle.get(url) # handle.maximize_window() def openUrl(handle, arg): handle.get(arg['url']) handle.maximize_window() def findElement(d, arg): ''' 1.test_id: 2.USERID 3.pwdid 4.loginid :param d: :param arg: :return: ''' if 'text_id' in arg: ele_login = get_ele_times(d, 10, lambda d: d.find_element_by_link_text(arg['text_id'])) ele_login.click() userEle = d.find_element_by_id(arg['userid']) pwdEle = d.find_element_by_id(arg['pwdid']) loginEle = d.find_element_by_id(arg['loginid']) # sign_out # signOutEle = d.find_element_by_id(arg['signOutid']) # print(signOutEle) return userEle, pwdEle, loginEle def findOutele(d, arg): try: signOutEle = d.find_element_by_id(arg['signOutid']) return signOutEle except: print("You are not login.") def sendVal(eletuple, arg): ''' :param eletuple: :param arg: :return: ''' list_key = ['uname','pwd'] i = 0 for key in list_key: eletuple[i].send_keys('') eletuple[i].clear() # print(arg[key]) eletuple[i].send_keys(arg[key]) i += 1 eletuple[2].click() def checkResult(d, err_id, arg, log): result = False time.sleep(3) try: err = d.find_element_by_id(err_id) print("Account and pwd error") # msg = "account:{},password:{}==>error:{}".format(arg['uname'],arg['pwd'], err.text) log.log_write(arg['uname'], arg['pwd'], 'Error', err.text) print(err.text) except: print("Account and pwd Right") # msg = "Account:{},password:{}==>pass!".format(arg['uname'], arg['pwd']) log.log_write(arg['uname'], arg['pwd'], 'Pass') result = True return result def logout(d, ele_dict): d.find_element_by_class_name(ele_dict['logout']).click() def login_test(ele_dict,user_list): d = openBrower() # log = Loginfo() log = Xlloginfo() log.log_init('sheet1', 'uname', 'pwd','result', 'message') openUrl(d, ele_dict) ele_tuple = findElement(d, ele_dict) # print(user_list[0]) for user_info in user_list: sendVal(ele_tuple, user_info) result = checkResult(d, ele_dict['errorid'], user_info, log) # sign_out # findOutele(d, ele_dict).click() if result: # logout logout(d, ele_dict) #login ele_tuple = findElement(d, ele_dict) else: ele_tuple = findElement(d, ele_dict) log.log_close() if __name__ == '__main__': ''' ele_dict = { 'url': 'http://www.maiziedu.com', 'uname':'maizi_test@139.com', 'pwd': 'abc123456', 'login_text':'Login', 'text_id': login_text, 'userid': 'id_account_l', 'pwdid': 'id_password_l', 'loginid': 'login_btn', 'signOutid':'sign_out', } user_list = [{'uname':account,'pwd':pwd},] ''' # file webinfo/userinfo get_webinfo(path),user_list = get ele_dict = get_webinfo(r'webinfo.txt') # user_list = get_userinfo(r'userinfo.txt') xinfo = XlUserInfo(r'userinfo.xlsx') user_list = xinfo.get_sheetinfo_by_index(0) login_test(ele_dict,user_list)
Log_Module
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import time import xlsxwriter class Loginfo(object): def __init__(self, path = '', mode= 'a'): self.file_time = time.strftime('%Y-%m-%d', time.gmtime()) self.log_time = time.strftime('%H-%M-%S') fname = "{}{}".format(path, self.file_time) self.log = open("{}{}.txt".format(path, fname), mode) def log_init(self, sheetname, *title): pass def log_write(self, msg): self.log.write("\n{}+{}".format(self.log_time, msg)) def log_close(self): self.log.close() class Xlloginfo(object): def __init__(self, path = ''): fname = '{}{}'.format(path,time.strftime('%Y-%m-%d', time.gmtime())) self.row = 0 self.xl = xlsxwriter.Workbook(path+fname+'.xls') self.style = self.xl.add_format({'bg_color':'red'}) def xl_write(self, *args): col = 0 style = '' if 'Error' in args: style = self.style for val in args: self.sheet.write_string(self.row, col, val) col += 1 self.row += 1 def log_init(self, sheetname, *title): self.sheet = self.xl.add_worksheet(sheetname) self.sheet.set_column('A:E', 30) self.xl_write(*title) def log_write(self, *args): self.xl_write(*args) def log_close(self): self.xl.close() if __name__ == '__main__': # log = Loginfo() # log.log_write('hahaha') # log.log_close() xinfo = Xlloginfo() xinfo.log_init('test12','uname','pwd', 'result', 'info') xinfo.log_close()
UserData
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import codecs import xlrd, xlsxwriter def get_webinfo(path): web_info = {} with codecs.open(path,'r','utf-8') as config: for line in config: result = [ele.strip() for ele in line.split('=')] # print("Result,",result) web_info.update(dict([result])) return web_info def get_userinfo(path): user_info = [] with codecs.open(path,'r','utf-8') as config: for line in config: user_dict = {} result = [ele.strip() for ele in line.split(';')] for info in result: account = [ele.strip() for ele in info.split('=')] # print(account) # print("Dict",dict([account])) user_dict.update(dict([account])) user_info.append(user_dict) return user_info class XlUserInfo(object): def __init__(self, path= ''): self.xl = xlrd.open_workbook(path) def floattostr(self, val): if isinstance(val, float): val = str(int(val)) return val def get_sheet_info(self): listkey = ['uname','pwd'] infolist = [] for row in range(1, self.sheet.nrows): info = [self.floattostr(val) for val in self.sheet.row_values(row)] temp = zip(listkey, info) infolist.append(dict(temp)) return infolist def get_sheetinfo_by_name(self, name): self.sheet = self.xl.sheet_by_name(name) return self.get_sheet_info() def get_sheetinfo_by_index(self, index): self.sheet = self.xl.sheet_by_index(index) return self.get_sheet_info() if __name__ == '__main__': # userinfo = get_userinfo(r'userinfo.txt') # print(userinfo) # for user in userinfo: # print(user) # webinfo = get_webinfo(r'webinfo.txt') # print(webinfo) xinfo = XlUserInfo(r'userinfo.xlsx') info = xinfo.get_sheetinfo_by_index(0) print(info) info = xinfo.get_sheetinfo_by_name('Sheet1') print(info)
posted on 2017-08-14 16:14 William126 阅读(498) 评论(0) 编辑 收藏 举报