码迷,mamicode.com
首页 > 编程语言 > 详细

Python 写了个小程序,耗时一天,结果才100多行

时间:2017-10-25 13:19:37      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:line   code   remove   style   day   com   cut   ref   odi   

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=rC:/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多行

标签:line   code   remove   style   day   com   cut   ref   odi   

原文地址:http://www.cnblogs.com/jin-wen-xin/p/7728134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!