您的位置:首页 > 其它

使用自动化测试框架selenium,批量的进行截图

2014-12-12 14:09 603 查看
最近在做一个自动化的电子邮件系统,实现是在outlook客户端能够接受html的邮件。

但是,由于我做的报表使用fusioncharts做的,fusioncharts框架是一个js图表库,所有的图表都是通过javascript来渲染出来的,

由于outlook不能支持js,所以只能将做好的图表页面截图,放入HTML页面中来发送。

截图的代码如下:

from datetime import datetime
import json
import urllib2

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait

def web_spider(logDebug,url="http://www.baidu.com",enable_proxy=True,proxy_address='http://baidu.com:8080'):
#     logDebug=open(logs_path,'a')
proxy_handler = urllib2.ProxyHandler({"http" : proxy_address})
null_proxy_handler = urllib2.ProxyHandler({})
if enable_proxy:
opener = urllib2.build_opener(proxy_handler)
else:
opener = urllib2.build_opener(null_proxy_handler)
content=""
try:
urllib2.install_opener(opener)
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
req = urllib2.Request(url =url ,headers = headers)
#     f=open("email.txt",'w+')
content=urllib2.urlopen(req).read()
content.decode("utf-8",'ignore').encode('utf-8')

#     f.write(content)
#     f.close()
except Exception,e:
logDebug.write("%s:web spider exception,%s\n" %(datetime.now().strftime('%Y%m%d-%H%M%S'),e))

pass
return content

def get_screenshot(browser,logDebug,url,filename='screenshot.png'):
flag=False
try:
browser.get(url)
wait=WebDriverWait(browser,60)
waitFlag=False
try:
waitFlag=wait.until(lambda browser:
'completed' in browser.find_element_by_id('load_flag').get_attribute("value"),
'wait timeout exception')
except TimeoutException,e:
print e
logDebug.write('%s:TimeoutException:%s\n' %(datetime.now().strftime('%Y%m%d-%H%M%S'),e))
print waitFlag
if waitFlag and filename:
browser.save_screenshot(filename)
flag=True
#             browser.get_screenshot_as_file('pngfile.png')
except Exception,e:
print e
logDebug.write("%s:Screenshot Exception,%s\n" %(datetime.now().strftime('%Y%m%d-%H%M%S'),e))
pass
return flag

def screenshot_batch(logs_path,screenshot_url):
logDebug=open(logs_path,'a')
count=0

report_list=[]
try:
content_json=web_spider(logDebug,screenshot_url)
decodejson=json.loads(content_json)
if 'report_list' in decodejson:
report_list=decodejson['report_list']
else:
return
except Exception,e:
print e
logDebug.write("%s:web spider or json decode exception,%s\n" %(datetime.now().strftime('%Y%m%d-%H%M%S'),e))
return count

try:
if decodejson:
browser = webdriver.Firefox()
browser.maximize_window()

for item in report_list:
report_url=item['report_url'].strip() if item['report_url'] else ''
img_path=item['img_path'].strip() if item['img_path'] else ''
screenshot_flag=False
if report_url!='' and img_path!='':
screenshot_flag=get_screenshot(browser,logDebug,report_url,img_path)
if screenshot_flag:
count+=1
browser.close()
browser.quit()

except Exception,e:
print e
logDebug.write("%s:open Firefox batch exception,%s\n" %(datetime.now().strftime('%Y%m%d-%H%M%S'),e))

logDebug.close()
return count
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: