设计用例切割页面、元素源码
1 # -*- encoding: utf-8 -*- 2 3 from tkinter import * 4 from tkinter.messagebox import * 5 from conversionDef import filecls 6 import tkinter.filedialog 7 from newfile import newAnfile 8 9 def openfile(): #将元素层.txt另存 10 checks_isspace = str(readnewfile_entry.get()) 11 if checks_isspace.isspace() == False or checks_isspace != "": 12 readnewfile_entry.delete(0,'end') 13 14 nfiles = tkinter.filedialog.asksaveasfilename() 15 nfiles_checks = nfiles.split("/") 16 print(nfiles_checks[-1]) 17 if ".txt" in nfiles_checks[-1] or ".robot" in nfiles_checks[-1]: 18 try: 19 for nfile in nfiles: 20 readnewfile_entry.insert(INSERT,nfile) 21 22 print(nfiles) 23 #写入方法weizhi 24 newAnfile.yuansuinput(nfiles) 25 showinfo('提示',"已经在"+nfiles+"路径下生成资源文件") 26 except Exception: 27 showinfo('提示','请确认是否已生成元素层.txt文件') 28 else: 29 showinfo('提示','不是txt或robot文本格式') 30 31 def openymfile(): 32 checks_isspace = str(readnewfile1_entry.get()) 33 if checks_isspace.isspace() == False or checks_isspace != "": 34 readnewfile1_entry.delete(0,'end') 35 36 nfiles = tkinter.filedialog.asksaveasfilename() 37 nfiles_checks = nfiles.split("/") 38 print(nfiles_checks[-1]) 39 if ".txt" in nfiles_checks[-1] or ".robot" in nfiles_checks[-1]: 40 try: 41 for nfile in nfiles: 42 readnewfile1_entry.insert(INSERT,nfile) 43 44 print(nfiles) 45 #写入方法weizhi 46 newAnfile.yemianinput(nfiles) 47 48 showinfo('提示',"已经在"+nfiles+"路径下生成资源文件") 49 except Exception: 50 showinfo('提示','请确认是否已生成页面层.txt文件') 51 else: 52 showinfo('提示','不是txt或robot文本格式') 53 54 def openurlfile(): #获取文件并读取 55 urls = tkinter.filedialog.askopenfilename() 56 print(urls) 57 checks_isspace = str(readfile_entry.get()) 58 if checks_isspace.isspace() == False or checks_isspace != "": 59 readfile_entry.delete(0,"end") 60 try: 61 for url in urls: 62 readfile_entry.insert(INSERT,url) 63 64 printinfo() 65 except Exception: 66 showinfo('提示',"请输入正确的文件路径和文件名\n"+"可能输入的文件路径带有特殊字符或空格回车等") 67 68 69 70 def printinfo(): #读取文件 71 url = str(readfile_entry.get()) 72 print(url) 73 input_entry.delete('1.0',END) #每次赋值前,都清空文本 74 try: 75 contests = filecls.readfilerobot(url) 76 for contest in contests: 77 ncontest = contest 78 print(ncontest) 79 input_entry.insert(INSERT,ncontest) 80 except Exception: 81 showinfo('提示',"读取文件失败,请检查文件是否损坏或无权限读取") 82 83 def inputyuansu(): #生成文件 84 listtext = [] 85 listtext1 = [] 86 list_text = input_entry.get("0.0",END).split("\n") 87 for ltexts in list_text: 88 listtext1.append(ltexts+"\n") 89 90 filecls.writefilerobot(listtext1) 91 92 showinfo('提示',"元素层.txt已生成,请到CaseToKeywords.exe同级文件夹下查找") 93 94 def inputyemian(): 95 listtext = [] 96 listtext1 = [] 97 list_text = input_entry.get("0.0",END).split("\n") 98 for ltexts in list_text: 99 listtext1.append(ltexts+"\n") 100 filecls.writeymfilerobot(listtext1) 101 102 showinfo('提示',"页面层.txt已生成,请到CaseToKeywords.exe同级文件夹下查找") 103 104 105 106 def helpinfo(): 107 showinfo("帮助","例如\n" 108 +"*** Settings ***\n" 109 +"\n" 110 +"*** Test Cases ***\n" 111 +"登录系统\n" 112 +" ##打开浏览器,${url},\n" 113 +" open browser ${url} chrome\n" 114 +" ##输入账号,${zhanghao},\n" 115 +" input text xpath=//input[contains(@id,'username')] ${zhanghao}\n" 116 +" ##输入密码,${zhanghao},\n" 117 +" input text xpath=//input[contains(@id,'password')] ${mima}\n" 118 +"\n" 119 +"转换成\n" 120 +"*** Settings ***\n" 121 +"\n" 122 +"*** KeyWords ***\n" 123 +"登录系统\n" 124 +"打开浏览器\n" 125 +" [Arguments] ${url} \n" 126 +" open browser ${url} chrome\n" 127 +"输入账号\n" 128 +" [Arguments] ${zhanghao}\n" 129 +" input text xpath=//input[contains(@id,'username')] ${zhanghao}\n" 130 +"输入密码\n" 131 +" [Arguments] ${zhanghao}\n" 132 +" input text xpath=//input[contains(@id,'password')] ${mima}\n") 133 134 #初始化Tk() 135 myWindow = Tk() 136 #设置标题 137 myWindow.title('robotframework用例转关键字') 138 #设置窗口大小 139 #width = 380 140 #height = 300 141 width = 720 142 height = 600 143 #获取屏幕尺寸以计算布局参数,使窗口居屏幕中央 144 screenwidth = myWindow.winfo_screenwidth() 145 screenheight = myWindow.winfo_screenheight() 146 alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2) 147 myWindow.geometry(alignstr) 148 #设置窗口是否可变长、宽,True:可变,False:不可变 149 myWindow.resizable(width=False, height=True) 150 151 152 Label(myWindow,text="请输入robotframework的用例").grid(row=0,column=0) 153 readfile_entry = Entry(myWindow) 154 readfile_entry.grid(row=0,column=2) 155 input_entry = Text(myWindow,width=80,height=40) 156 input_entry.place(x=60,y=60) 157 Button(myWindow,text="查找并读取文本",command=openurlfile).grid(row=0,column=5) 158 #Button(myWindow,text="读取文本",command=printinfo).grid(row=0,column=6) 159 160 Button(myWindow,text="生成元素层关键字",command=inputyuansu).grid(row=0,column=6) 161 Button(myWindow,text="生成页面层关键字",command=inputyemian).grid(row=0,column=7) 162 Button(myWindow,text="帮助说明",command=helpinfo).grid(row=0,column=8) 163 164 readnewfile_entry = Entry(myWindow)# 新增放置关键字 165 readnewfile_entry.place(x=20,y=35) 166 #readnewfile_entry.grid(row=1,column=0) 167 Button(myWindow,text="选择保存元素位置",command=openfile).place(x=180,y=30) 168 169 170 readnewfile1_entry = Entry(myWindow)# 新增放置关键字 171 #readnewfile1_entry.grid(row=1,column=5) 172 readnewfile1_entry.place(x=300,y=35) 173 Button(myWindow,text="选择保存页面位置",command=openymfile).place(x=450,y=30) 174 175 #进入消息循环 176 myWindow.mainloop()
1 # -*- encoding: utf-8 -*- 2 3 class filecls(object): 4 #实例化类,并且加上classmethod的方法 才能被其他文本调用 5 @classmethod 6 def readfilerobot(self,url): 7 self.url = url 8 try: 9 file_read = open(self.url,'r',encoding='utf-8') 10 file_read_contests = file_read.readlines() 11 file_read.close() 12 except UnicodeDecodeError: 13 file_read = open(self.url,'r') 14 file_read_contests = file_read.readlines() 15 file_read.close() 16 return file_read_contests 17 18 @classmethod 19 def writefilerobot(self,listlines): 20 self.listlines = listlines 21 file_write = open("元素层.txt",'w',encoding='utf-8') 22 list_write = [] 23 for contests in self.listlines: 24 print(contests) 25 if contests== "*** Test Cases ***\n": 26 contests = "*** Keywords ***\n" 27 28 if "##" in contests : 29 list_guanjianzi = contests.split("##") 30 #if list_guanjianzi[0].isspace() == True or list_guanjianzi[0] == "": 31 #list_write.append(list_guanjianzi[0]+"\n") 32 33 34 list_yuansu = list_guanjianzi[1].split(",") 35 print(list_yuansu) 36 if len(list_yuansu) == 3: 37 list_write.append("\n"+list_yuansu[0]+"\n") 38 39 #if list_yuansu[1] != "/": 40 if list_yuansu[1].isspace() == False and list_yuansu[1] != "": 41 list_write.append(" [Arguments]\t"+list_yuansu[1]+"\n") 42 43 #if list_yuansu[2] != "/\n": 44 if list_yuansu[2].isspace() == False and list_yuansu[2] != "": 45 list_write.append(" [Return]\t"+list_yuansu[2]) 46 47 contests = "" #将 xxx##关键字,1,1 = 空值 48 list_write.append(contests) 49 50 list_write.append(contests) 51 52 file_write.writelines(list_write) 53 54 file_write.close() 55 56 @classmethod 57 def writeymfilerobot(self,listlines): 58 self.listlines = listlines 59 file_write = open("页面层.txt",'w',encoding='utf-8') 60 list_write = [] 61 j = 0 62 for contests in self.listlines: 63 if contests== "*** Settings ***\n": 64 list_write.append(contests+"\n"+"\n") 65 66 if contests == "*** Test Cases ***\n": 67 contests = "*** Keywords ***\n" 68 list_write.append(contests) 69 contests_next = self.listlines[j+1] 70 list_write.append(contests_next) 71 72 if "##" in contests: 73 list_guanjianzi = contests.split("##") 74 75 #list_guanjianzi[1] = "关键字 ${a} ${b}\n" 76 list_yuansu = list_guanjianzi[1].split(",") 77 print(list_yuansu) 78 if len(list_yuansu) == 3: 79 #list_yuansu = ["关键字","${a}","${b}\n"] 80 #if list_yuansu[1] != "/" and list_yuansu[2] != "/\n": 81 if (list_yuansu[1].isspace() == False and list_yuansu[1] != "") and (list_yuansu[2].isspace() == False and list_yuansu[2] != ""): 82 list_yuansu_re = list_yuansu[2].split("\n") 83 list_write.append(" "+list_yuansu_re[0]+"\t"+list_yuansu[0]+"\t"+list_yuansu[1]+"\n") 84 85 #if list_yuansu[1] != "/" and list_yuansu[2] == "/\n": 86 if (list_yuansu[1].isspace() == False and list_yuansu[1] != "") and (list_yuansu[2].isspace() == True or list_yuansu[2] == ""): 87 list_write.append(" "+list_yuansu[0]+"\t"+list_yuansu[1]+"\n") 88 89 #if list_yuansu[1] == "/" and list_yuansu[2] != "/\n": 90 if (list_yuansu[1].isspace() == True or list_yuansu[1] == "") and (list_yuansu[2].isspace() == False and list_yuansu[2] != ""): 91 list_yuansu_re = list_yuansu[2].split("\n") 92 list_write.append(" "+list_yuansu_re[0]+"\t"+list_yuansu[0]+"\n") 93 94 #if list_yuansu[1] == "/" and list_yuansu[2] == "/\n": 95 if (list_yuansu[1].isspace() == True or list_yuansu[1] == "") and (list_yuansu[2].isspace() == True or list_yuansu[2] == ""): 96 list_write.append(" "+list_yuansu[0]+"\n") 97 98 j = j+1 99 100 file_write.writelines(list_write) 101 102 file_write.close()
1 # -*- encoding: utf-8 -*- 2 3 class newAnfile(): 4 @classmethod 5 def yuansuinput(self,url): 6 self.url = url 7 8 file_read = open("元素层.txt",'r',encoding='utf-8') 9 file_read_contests = file_read.readlines() 10 file_read.close() 11 12 file_write = open(self.url,'w', encoding= 'utf-8') 13 file_write.writelines(file_read_contests) 14 file_write.close() 15 16 @classmethod 17 def yemianinput(self,url): 18 self.url = url 19 20 file_read = open("页面层.txt",'r',encoding='utf-8') 21 file_read_contests = file_read.readlines() 22 file_read.close() 23 24 file_write = open(self.url,'w', encoding= 'utf-8') 25 file_write.writelines(file_read_contests) 26 file_write.close()
一切博文基本原创,谢谢