使用自动化测试框架selenium,批量的进行截图
2014-12-12 14:09
603 查看
最近在做一个自动化的电子邮件系统,实现是在outlook客户端能够接受html的邮件。
但是,由于我做的报表使用fusioncharts做的,fusioncharts框架是一个js图表库,所有的图表都是通过javascript来渲染出来的,
由于outlook不能支持js,所以只能将做好的图表页面截图,放入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
相关文章推荐
- 使用selenium和webdriver进行自动化测试
- linux无界面下—使用selenium进行自动化测试
- 使用分层的 Selenium 框架进行复杂 Web 应用的自动测试
- 使用PHPUnit + Selenium进行自动化测试
- 使用selenium进行web测试项目框架
- 使用selenium进行自动化测试
- 使用Selenium进行的自动化测试
- Selenium grid:使用远程chrome浏览器进行远程自动化测试
- 分层 Selenium 框架进行复杂 Web 应用程序自动化测试
- 使用分层的Selenium框架进行复杂 Web 应用的自动测试
- Arquillian测试框架快速上手教程(四)- 使用Arquillian + Drone + Selenium + Graphene 进行Web自动化测试
- 使用Selenium通过浏览器对网站进行自动化测试
- 使用 Selenium Grid2 来提高全球化测试中自动化截图的执行效率
- 如何在linux命令行无界面下,使用selenium进行自动化测试
- 使用分层的 Selenium 框架进行复杂 Web 应用的自动测试
- 使用分层的 Selenium 框架进行复杂 Web 应用的自动测试
- 使用VS2010的CodedUI来做自己的自动化测试框架
- 使用Selenium进行验收测试
- 使用脚本设计自动化测试框架的原则