﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-arthask183-文章分类-测试工具</title><link>http://www.cnitblog.com/arthask183/category/7702.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 01 Oct 2011 17:30:16 GMT</lastBuildDate><pubDate>Sat, 01 Oct 2011 17:30:16 GMT</pubDate><ttl>60</ttl><item><title>如何用QTP解决图片验证码（解析QuickTest文本识别机制）？【转载】</title><link>http://www.cnitblog.com/arthask183/articles/50240.html</link><dc:creator>出现</dc:creator><author>出现</author><pubDate>Wed, 15 Oct 2008 01:23:00 GMT</pubDate><guid>http://www.cnitblog.com/arthask183/articles/50240.html</guid><wfw:comment>http://www.cnitblog.com/arthask183/comments/50240.html</wfw:comment><comments>http://www.cnitblog.com/arthask183/articles/50240.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/arthask183/comments/commentRss/50240.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/arthask183/services/trackbacks/50240.html</trackback:ping><description><![CDATA[<span style="COLOR: rgb(255,1,2)"><br>本文出自</span><a style="COLOR: rgb(255,1,2)" href="http://www.51testing.com/?35/action_viewpro_showpro_1.html"><u>songfun</u></a><span style="COLOR: rgb(255,1,2)">的51Testing软件测试博客，<a onclick="javascript:tagshow(event, '%D7%AA%D4%D8');" href="javascript:;" target=_self><u><strong>转载</strong></u></a>请保留出处及链接：</span><a style="COLOR: rgb(255,1,2)" href="http://www.51testing.com/?songfun" target=_blank><u>http://www.51testing.com/?songfun</u></a><br><br>&nbsp;&nbsp;&nbsp;大家在使用<a onclick="javascript:tagshow(event, 'QTP');" href="javascript:;" target=_self><u><strong>QTP</strong></u></a>进行<a onclick="javascript:tagshow(event, '%D7%D4%B6%AF%BB%AF%B2%E2%CA%D4');" href="javascript:;" target=_self><u><strong>自动化测试</strong></u></a>的过程中经常会遇到图片验证码的问题——大家所关心的就是如何解决此类问题。<br>&nbsp;&nbsp;&nbsp;这里我们首先要去了解为什么会有图片验证码。其实验证码的本质作用就是防止有人利用工具（灌水机、注册机，当然也不小心包括了我们的自动化测试工具）恶意猜解登陆或者不停的注册和灌水的。因此如果我们完全寄希望于通过GUI识别来获取内容是不切实际的——先打好预防针，免得读者希望太大，失望更大，呵呵！<br>&nbsp;&nbsp;&nbsp;下面说说验证码的解决思路：其实解决图片验证码的思路有很多，我这里主要结合QTP9.5的新特性给大家介绍其中一种解决方案，就是利用它的OCR机制抓取文本内容。<br><span style="DISPLAY: none">&nbsp;&nbsp;&nbsp;</span>在QTP9.5中，对象识别能力有了进一步改善，其中针对文本识别方面进行了优化，引入了ABBYY公司的OCR解决方案——这个相关的功能体现在QTP菜单的&#8220;Tools--&gt;Options--&gt;General--Use text recognition mechanisms in this order&#8221;里，详细内容后面会有具体介绍。<br><span style="DISPLAY: none">&nbsp;&nbsp;&nbsp;</span>先来看看<a href="http://www.abbyy.cn/company/" target=_blank><u>ABBYY</u></a>是何许公司，登录他们的官方网站可以看到一段相关介绍：&#8220;ABBYY是世界OCR（光学字符识别）、ICR（手写体识别）和语言软件的领航者。ABBYY 致力于人工智能(AI)和语言软件开发。提供全套文档识别，转换和数据捕获<a onclick="javascript:tagshow(event, '%BC%BC%CA%F5');" href="javascript:;" target=_self><u><strong>技术</strong></u></a>的产品解决方案。&#8221;如果你使用过图像文档转换的软件，一定会听说过FineReader OCR Professional ，其实它就是ABBYY公司的产品，用官方的说法就是&#8220;将通过扫描仪、MFP 或数码相机生成的图像快速转换为可编辑和可搜索的电子格式，而且识别率很高&#8221;，说白了就是可以借助它先进的OCR机制&#8220;读&#8221;出图片里的文本内容，并转换为PDF之类的文档。<br><span style="DISPLAY: none">&nbsp;&nbsp;&nbsp;</span>有了ABBYY这么强大的背后支持，QTP自然底气十足，那么QTP到底如何以OCR机制识别文本呢？我们首先先了解一下什么是OCR。打开&#8220;<a href="http://baike.baidu.com/view/17761.html?wtp=tt" target=_blank><u>百度百科_OCR</u></a>&#8221;，它的说明：&#8220;OCR（Optical Character Recognition，光学字符识别），是属于图型识别（Pattern Recognition，PR）的一门学问。其目的就是要让计算机知道它到底看到了什么，尤其是文字资料。 由于OCR是一门与识别率拔河的技术，因此如何除错或利用辅助信息提高识别正确率，是OCR最重要的课题，ICR（Intelligent Character Recognition）的名词也因此而产生。而根据文字资料存在的媒体介质不同，及取得这些资料的方式不同，就衍生出各式各样、各种不同的应用。&#8221;这里有个关键词：&#8220;正确率&#8221;，也就是&#8220;识别率&#8221;——既然不能够总是100%，我们自然不可能完全寄希望于通过QTP能够每次100%正确的去识别图片里的文本。尤其是&#8220;道高一尺魔高一丈&#8221;的今天，验证码加入了大量的干扰素，如扭曲、变形、错位、随机背景花纹，给OCR识别增加了很多难度——本来就不希望被软件识别到嘛。<br><span style="COLOR: rgb(255,1,2)">本文出自</span><a style="COLOR: rgb(255,1,2)" href="http://www.51testing.com/spacecp.php?35/action_viewpro_showpro_1.html"><u>songfun</u></a><span style="COLOR: rgb(255,1,2)">的51Testing软件测试博客，转载请保留出处及链接：</span><a style="COLOR: rgb(255,1,2)" href="http://www.51testing.com/spacecp.php?songfun" target=_blank><u>http://www.51testing.com/?songfun</u></a><br><span style="DISPLAY: none">&nbsp;&nbsp;&nbsp;</span>了解了OCR之后，我们再来看看QTP对应的这个设置。如前面所说，通过QTP菜单的&#8220;Tools--&gt;Options&#8221;选中到&#8220;General--Use text recognition mechanisms in this order&#8221;，这里的四个选项就是对应的不同设置。我们看看帮助的描述（我做了翻译）：<br>=================================<br><span style="DISPLAY: none">使用文本识别机制<br></span><br>指定QTP在采用 &#8220;文本&#8221;或者&#8220;文本区域&#8221; 的 检查点或输出值 的步骤时，捕获文本内容所使用的文本识别机制。<br><span style="DISPLAY: none">&nbsp;&nbsp;&nbsp;</span>以下有三种识别方式：<br><span style="DISPLAY: none">&nbsp;&nbsp;&nbsp;</span>1、先使用<a onclick="javascript:tagshow(event, 'Windows');" href="javascript:;" target=_self><u><strong>Windows</strong></u></a> API，再使用OCR（默认）。<br><span style="DISPLAY: none">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>指示QTP首先尝试以基于Windows API的机制从对象上直接获取文本内容。如果未获取到文本（比如，文本属于图片的一部分），QTP就会使用OCR的机制尝试获取这段文本。<br>&nbsp;&nbsp;&nbsp; 强烈建议在使用中日韩（象形文字）、英的语言环境下采用这个设置。<br>&nbsp;&nbsp;&nbsp;2、先使用OCR，再使用Windows API。<br>&nbsp;&nbsp;&nbsp; 指示QTP首先尝试使用OCR机制从对象上去获取文本。如果未获取到文本，QTP就会以Windows API的机制去获取文本内容。<br>&nbsp;&nbsp;&nbsp;3、仅使用Windows API方式。<br>&nbsp;&nbsp;&nbsp;&nbsp; 指示QTP仅采用基于Windows API的机制从对象上获取文本内容。<br>&nbsp;&nbsp;&nbsp;4、仅使用OCR的方式。<br>&nbsp;&nbsp;&nbsp; 指示QTP仅采用基于OCR的机制从对象上获取文本内容。<br>&nbsp;&nbsp;&nbsp; 在使用Windows <a onclick="javascript:tagshow(event, 'Vista');" href="javascript:;" target=_self><u><strong>Vista</strong></u></a>要使用这种方式。<br><br>=================================<br><span style="COLOR: rgb(255,1,2)">本文出自</span><a style="COLOR: rgb(255,1,2)" href="http://www.51testing.com/spacecp.php?35/action_viewpro_showpro_1.html"><u>songfun</u></a><span style="COLOR: rgb(255,1,2)">的51Testing软件测试博客，转载请保留出处及链接：</span><a style="COLOR: rgb(255,1,2)" href="http://www.51testing.com/spacecp.php?songfun" target=_blank><u><a href="http://www.51testing.com/?songfun51Testing">http://www.51testing.com/?songfun</u></a></a><br>上面的内容已经解释的很明确了，接下来我们通过TextArea Output Value看看效果。<br><br>如下图所示，QTP针对几张图片的识别效果：<br>（一）、内容是51Testing的，QTP获取正确；内容是51Testing的G风格彩字，QTP获取错误（显示为IC_CHECK_PATTERN）<img title=点击图片可在新窗口打开 style="WIDTH: 400px; CURSOR: pointer" src="http://www.51testing.com/attachments/2008/10/35_200810101910132.gif" border=0><br>&nbsp;<img title=点击图片可在新窗口打开 style="WIDTH: 400px; CURSOR: pointer" src="http://www.51testing.com/attachments/2008/10/35_200810101910131.jpg" border=0><br><img title=点击图片可在新窗口打开 style="CURSOR: pointer" src="http://www.51testing.com/attachments/2008/10/35_200810101910133.gif" border=0><br>（二）、内容是songfun的普通文本，QTP获取正确；内容是songfun的G风格彩字，QTP获取错误（也显示为IC_CHECK_PATTERN）<br><img title=点击图片可在新窗口打开 style="WIDTH: 400px; CURSOR: pointer" src="http://www.51testing.com/attachments/2008/10/35_200810101910135.gif" border=0><br><img title=点击图片可在新窗口打开 style="CURSOR: pointer" src="http://www.51testing.com/attachments/2008/10/35_200810101910134.gif" border=0><br>&nbsp;<img title=点击图片可在新窗口打开 style="WIDTH: 400px; CURSOR: pointer" src="http://www.51testing.com/attachments/2008/10/35_200810101910136.jpg" border=0><br><span style="DISPLAY: none"><br></span>&nbsp;有兴趣大家可以自己做一些图片，甚至可以用QQ的验证码图片来试验一下，看看OCR效果。 
<img src ="http://www.cnitblog.com/arthask183/aggbug/50240.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/arthask183/" target="_blank">出现</a> 2008-10-15 09:23 <a href="http://www.cnitblog.com/arthask183/articles/50240.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>