python与selenium自动化基础-测试用例数据数据分离
分离前:
from selenium import webdriver import time def test(): d = webdriver.Firefox() d.get('https://www.imooc.com/') d.find_element_by_link_text("登录").click() time.sleep(5) #ele1 = d.find_element_by_xpath('/html/body/div[9]/div[2]/div/form/div[1]/input') ele1 = d.find_element_by_name("email") ele1.clear() ele1.send_keys("xxxxx") ele2 = d.find_element_by_name("password") ele2.clear() ele2.send_keys("yyyyy") d.find_element_by_css_selector('input[type="button"]').click() test()
分离后:
from selenium import webdriver import time def openBrower(): webdriver_handle = webdriver.Firefox() return webdriver_handle def openUrl(handle,url): handle.get(url) def findElement(d,arg): ele_login = d.find_element_by_link_text(arg['login_text']) ele_login.click() time.sleep(5) userEle = d.find_element_by_name(arg['userid']) pwdEle = d.find_element_by_name(arg['pwdid']) loginEle = d.find_element_by_css_selector('input[type="button"]') return userEle,pwdEle,loginEle def sendVals(eletuple,arg): listkey = ['uname','pwd'] i = 0 for key in listkey: eletuple[i].clear() eletuple[i].send_keys(arg[key]) i+=1 eletuple[2].click() def checkResult(d,text): try: d.find_element_by_link_text(text) print("Account and Pwd Error!") except: print("Account and Pwd Right!") def login_test(ele_dict,user_list): d = openBrower() openUrl(d,ele_dict['url']) ele_tuple = findElement(d,ele_dict) for arg in user_list: sendVals(ele_tuple, arg) checkResult(d,ele_dict['errorid']) # 当.py文件被直接运行时,if __name__ == '__main__'之下的代码块将被运行;当.py文件以模块形式被导入时,if __name__ == '__main__'之下的代码块不被运行 if __name__ == '__main__': url = 'https://www.imooc.com/' login_text = "登录" account = 'xxxxx' pwd = 'yyyyy' id_account_1 = 'email' id_password_1 = 'password' ele_dict = {'url':url,'login_text':login_text,'userid':id_account_1,\ 'pwdid':id_password_1,'errorid':'该账号格式不正确'} user_list = [{'uname':account,'pwd':pwd}] login_test(ele_dict,user_list)
从文件导入数据:
from selenium import webdriver import time from userdata import get_webinfo,get_userinfo def openBrower(): webdriver_handle = webdriver.Firefox() return webdriver_handle def openUrl(handle,url): handle.get(url) def findElement(d,arg): ele_login = d.find_element_by_link_text(arg['login_text']) ele_login.click() time.sleep(5) userEle = d.find_element_by_name(arg['userid']) pwdEle = d.find_element_by_name(arg['pwdid']) loginEle = d.find_element_by_css_selector('input[type="button"]') return userEle,pwdEle,loginEle def sendVals(eletuple,arg): listkey = ['uname','pwd'] i = 0 for key in listkey: eletuple[i].clear() eletuple[i].send_keys(arg[key]) i+=1 eletuple[2].click() def checkResult(d,text): try: d.find_element_by_link_text(text) print("Account and Pwd Error!") except: print("Account and Pwd Right!") def login_test(ele_dict,user_list): d = openBrower() openUrl(d,ele_dict['url']) ele_tuple = findElement(d,ele_dict) for arg in user_list: sendVals(ele_tuple, arg) checkResult(d,ele_dict['errorid']) if __name__ == '__main__': ele_dict = get_webinfo(r'G:\yaya\pycharm\webinfo') user_list = get_userinfo(r'G:\yaya\pycharm\userinfo') login_test(ele_dict,user_list)
userinfo
uname=xxxxx pwd=yyyyy
webinfo
url = https://www.imooc.com/ login_text = 登录 userid = email pwdid = password errorid = 该账号格式不正确
userdata.py
def get_webinfo(path): web_info = {} config = open(path,encoding='UTF-8') for line in config: result = [ele.strip() for ele in line.split('=')] web_info.update(dict([result])) return web_info def get_userinfo(path): user_info = [] config = open(path,encoding='UTF-8') for line in config: user_dict = {} result = [ele.strip() for ele in line.split(' ')] for r in result: account = [ele.strip() for ele in r.split('=')] user_dict.update(dict([account])) user_info.append(user_dict) return user_info if __name__ == '__main__': webinfo = get_webinfo(r'G:\yaya\pycharm\webinfo') for key in webinfo: print(key,webinfo[key]) userinfo = get_userinfo(r'G:\yaya\pycharm\userinfo') for l in userinfo: print(l)
完