from selenium importwebdriverimportselenium.webdriver.support.ui as uifrom selenium.webdriver.common.by importByfrom selenium.webdriver.support.ui importWebDriverWaitfrom selenium.common.exceptions importNoSuchElementException,WebDriverExceptionimportselenium.webdriver.support.expected_conditions as ECimporttimefrom selenium.webdriver.chrome.options importOptionsimportioimportosimportreimporturllib2from datetime importdatetime
projectList=[‘*‘,‘**‘]
serverURL=‘****‘userProfile=‘C:/Users/***a‘consoleText=‘consoleText‘logPath=‘**‘
defwrite_file(data,fileName):
file_name= logPath+fileName+r‘.txt‘append_write= ‘a‘
ifos.path.exists(file_name):
append_write= ‘w‘f=open(file_name, append_write)
f.writelines(data)
f.close()returnfile_namedefget_latest_version(driver,projectList,serverURL):
version=[]for i inprojectList:### 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])returnversiondefclick_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();defget_test_summary(file_obj):
isSummary=False
result=[]for line infile_obj:if notisSummary:if ‘Test Summary‘ inline:
isSummary=Trueelif ‘End of Summary‘ inline:break
else:
result.append(line)returnresultdefget_common_issues(list1,list2):
common_issues=[]
result= [l for l in list1 if l in list2 and ‘Failed: 0‘ not inl]for ele inresult:if ele != ‘\r\n‘ and ele != ‘\n‘:
common_issues.append(ele)printelereturncommon_issuesdefget_server_log_path(file_obj):for line infile_obj:if ‘GQL Log is located at‘ inline:return line.split()[-1]defget_summary_list(version):
summary_list=[]for os_ele inversion:
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()returnsummary_listdefwrite_summary_log(summary_log_name, common_issues,baseUrl):for com_is incommon_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:continuef= open(case_log_path,‘r‘)
data=f.read()
f.close()
match= re.findall(r‘(?:(?!Test Case: ).)*Test Case: Fail‘,data,re.DOTALL)for i inmatch:ifos.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]printbaseUrl
summary_log_name= logPath + datetime.today().strftime(‘%Y-%m-%d‘) + r‘.txt‘
ifos.path.exists(summary_log_name):
os.remove(summary_log_name)
write_summary_log(summary_log_name, common_issues,baseUrl)