Python 写了个小程序,耗时一天,结果才100多行
Posted 金大鑫要坚持
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 写了个小程序,耗时一天,结果才100多行相关的知识,希望对你有一定的参考价值。
from selenium import webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import NoSuchElementException,WebDriverException import selenium.webdriver.support.expected_conditions as EC import time from selenium.webdriver.chrome.options import Options import io import os import re import urllib2 from datetime import datetime projectList=[‘*‘,‘**‘] serverURL=‘****‘ userProfile=‘C:/Users/***a‘ consoleText=‘consoleText‘ logPath=‘**‘ def write_file(data,fileName): file_name = logPath+fileName+r‘.txt‘ append_write = ‘a‘ if os.path.exists(file_name): append_write = ‘w‘ f = open(file_name, append_write) f.writelines(data) f.close() return file_name def get_latest_version(driver,projectList,serverURL): version=[] for i in projectList: ### get latest version ID. Such as 31,32 url = serverURL+str(i) driver.get(url) click_submit(driver) attr = driver.find_elements_by_css_selector("div.build-icon>a")[0].get_attribute("href") buildId = attr.split(‘/‘)[-2] ### get log content with encoding utf-8 url = url+r‘/‘+buildId+r‘/‘+consoleText driver.get(url) time.sleep(2) content = driver.page_source.encode("utf-8") ### write file to local, named with like rhel-buildId ### list format: os, logPath(local),buildId version.append([i,write_file(content,i+‘#‘+buildId),buildId]) return version def click_submit(driver): if len(driver.find_elements_by_css_selector("input[name=‘j_password‘]")) > 0: driver.find_element_by_css_selector("span[name=‘Submit‘] button").click(); def get_test_summary(file_obj): isSummary = False result = [] for line in file_obj: if not isSummary: if ‘Test Summary‘ in line: isSummary = True elif ‘End of Summary‘ in line: break else: result.append(line) return result def get_common_issues(list1,list2): common_issues = [] result = [l for l in list1 if l in list2 and ‘Failed: 0‘ not in l] for ele in result: if ele != ‘\r\n‘ and ele != ‘\n‘: common_issues.append(ele) print ele return common_issues def get_server_log_path(file_obj): for line in file_obj: if ‘GQL Log is located at‘ in line: return line.split()[-1] def get_summary_list(version): summary_list = [] for os_ele in version: file_obj = open(os_ele[1],‘r‘) summary_list.append(get_test_summary(file_obj)) os_ele.append(get_server_log_path(file_obj)) file_obj.close() return summary_list def write_summary_log(summary_log_name, common_issues,baseUrl): for com_is in common_issues: com_is = com_is.split(‘:‘)[0] try: response = urllib2.urlopen(baseUrl+r‘/‘+com_is) case_log_path = write_file(response,com_is+datetime.today().strftime(‘%Y-%m-%d‘)) #print ‘case log path:‘,case_log_path except: continue f = open(case_log_path,‘r‘) data = f.read() f.close() match = re.findall(r‘(?:(?!Test Case: ).)*Test Case: Fail‘,data,re.DOTALL) for i in match: if os.path.exists(summary_log_name): append_write = ‘a‘ else: append_write = ‘w‘ f = open(summary_log_name,append_write) f.writelines(‘#########‘+com_is+‘#########‘) f.write(‘\r\n‘) if i.startswith(‘est Case:‘): i = i[i.find(‘\n‘):] for parsed in [line for line in i.split(‘\n‘) if line.strip() !=‘‘]: f.writelines(parsed) f.write(‘\r\n‘) f.close() print ‘Summary file put in ‘, summary_log_name options = webdriver.ChromeOptions() options.add_argument(‘--user-data-dir=‘+userProfile) driver = webdriver.Chrome(executable_path=r‘C:/Python27/chromedriver.exe‘,chrome_options=options) version = get_latest_version(driver,projectList,serverURL) driver.quit() summary_list = get_summary_list(version) common_issues = get_common_issues(summary_list[0],summary_list[1]) baseUrl = version[0][-1] print baseUrl summary_log_name = logPath + datetime.today().strftime(‘%Y-%m-%d‘) + r‘.txt‘ if os.path.exists(summary_log_name): os.remove(summary_log_name) write_summary_log(summary_log_name, common_issues,baseUrl)
以上是关于Python 写了个小程序,耗时一天,结果才100多行的主要内容,如果未能解决你的问题,请参考以下文章