robotframework 测试工具添加PDF文件内容匹配插件
robotframework 这个需要了解的请度娘。本文实现的是一个小功能。大体分为如下几个步骤
1)给定一个pdf文件。
2)读取pdf文件内容,并解析为文本内容。
3)通过给定的内容,比对pdf文件内容。
4)输出测试结果。
5)发送结果到指定邮件。
其中读取pdf文件内容,使用的是pdfminer
其他的就是自己包装。
涉及到部分隐私内容,部分代码如下:
# -*- coding: UTF-8 -*- # coding=utf-8 #from __future__ import unicode_literals import sys import os import subprocess import time import re from robot.libraries.BuiltIn import BuiltIn from mailcommon import mailcommon from pdfminer.pdfdocument import PDFDocument from pdfminer.pdfparser import PDFParser from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.pdfdevice import PDFDevice, TagExtractor from pdfminer.pdfpage import PDFPage from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter from pdfminer.cmapdb import CMapDB from pdfminer.layout import LAParams from pdfminer.image import ImageWriter import cStringIO #reload(sys) #sys.setdefaultencoding('utf-8') class pdfContentKeywords(object): ROBOT_LIBRARY_SCOPE = 'Global' root = os.path.abspath(os.path.join(__file__, '..')) def __init__(self): self.builtin = BuiltIn() def handlepdf(self, path, param_dict,email, fail=True): ''' Reads a specified directory pdf file and diff ... `path`: pdf file path `param_dict`: Require comparison data `fail`: If there are differences it will throw an exception and test will fail defaults to True, if False test's will continue ''' param_dict=eval(param_dict) self.builtin.log("pdf: %s" %path) self.builtin.log("param: %s" %param_dict) fail = self.builtin.convert_to_boolean(fail) result=dict if fail: result=self._diffContent(path, param_dict) else: try: result=self._diffContent(path, param_dict) except Exception, e: self.builtin.log(e) msg='' for i in result: print u"result:%s=" % i,result[i] #print self.builtin.log(i) msg+="=>".join([i,str("检验通过" if result[i]==1 else "检验没通过")]) mail_obj=mailcommon() print u"mail MSG:%s" % msg maillist=[] maillist.append(email) mail_obj.send_mail(maillist,'测试结果分析',msg); self.builtin.log("End") def _readPdf(self,path): caching = False password = '' pagenos = set() maxpages = 0 # output option outfile = None outtype = None imagewriter = None rotation = 0 layoutmode = 'normal' codec = 'utf-8' pageno = 1 scale = 1 showpageno = True laparams = LAParams() content='' outfp=cStringIO.StringIO() try: rsrcmgr = PDFResourceManager(caching=caching) device = TextConverter(rsrcmgr, outfp, codec=codec, laparams=laparams, imagewriter=imagewriter) fp = file(path, 'rb') interpreter = PDFPageInterpreter(rsrcmgr, device) for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password, caching=caching, check_extractable=True): page.rotate = (page.rotate+rotation) % 360 interpreter.process_page(page) fp.close() device.close() content= outfp.getvalue() outfp.close() except Exception, e: print "Exception:%s",e self.builtin.log(e) content=content.replace('\n','').replace('\t','').strip('\n') print u"pdf file content:%s"% content#.encode('utf-8').strip() return content def _diffContent(self,path,p_dict): result=dict() try: pdfContent=self._readPdf(path) fail=0 for i in p_dict: print "dict[%s]=" % i,p_dict[i] if re.search(r''+i+p_dict[i] ,pdfContent,re.I | re.M|re.X): fail=1 result[i+p_dict[i]]=fail except Exception, e: print "Exception:%s",e self.builtin.log(e) return result if __name__ == "__main__": d = pdfContentKeywords() print d.handlepdf("C:\\Print.pdf",{"船名/航次:":"CMACGMFIDELIO/B14WESVLC/VALENCIA"},'roger_he@5uzh.com',False) #d._readPdf("")
作者:小羽信息
出处:http://www.cnblogs.com/fer-team/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。