测试之家淘宝店:主营软件测试定制服务 http://shop71136398.taobao.com/ 软件评测试考试论坛:http://www.testdao.com/forum-113-1.html 2012软件评测师考试群:28388329

HTML report 类

上一篇 / 下一篇  2009-08-25 15:05:12 / 个人分类:watir+ruby

将下面的代码保存为CLReport.class.rb作为公共库51Testing软件测试网BR e's a Y D

class CLReport51Testing软件测试网6g/dQCj&_Ywo_

 # Initialize the report class

E:a L nf+r,y,Jk.?)[0

 def initialize()51Testing软件测试网h&n`8G:Z2\!J

   @overallResult = 'PASSED'51Testing软件测试网-J Wig$Vj

   @reportContent1 = ''51Testing软件测试网v5Q|9ex.[

   @reportContent2 = ''

0U!@)y ^4HYN0

 end51Testing软件测试网K"I yV_$s-ETV3x6x

 

Y;J,t7Dg*KG0

 # Create a report

eA&a2f0v8Aag%Vd0

 def createReport(reportName)51Testing软件测试网K9lcn3QMxLlYT%e

   # Get current time

? si R/{ q6O c0

   t = Time.now51Testing软件测试网9p+|B.sB^

 51Testing软件测试网C8NaxG l$G \ X

   # Format the day51Testing软件测试网j.? [Ag g*Y

   if(t.day.to_s.length == 1)

W-j Ugc]]0

     strDay = '0' + t.day.to_s51Testing软件测试网P3X P\$C~1L

   else51Testing软件测试网gS#Y7r[%h;F

     strDay = t.day.to_s

5mQ*^9P Tg,w0

   end51Testing软件测试网+j3N&PB2tp

 51Testing软件测试网? }p'tj&b}b3Z

   # Format the month

J C'u&v\:A6[3L\0

   if(t.month.to_s.length == 1)

(x,w Ks&^7|pZ0

     strMonth = '0' + t.month.to_s51Testing软件测试网.v3e0\mB

   else

l _~w.F|6s0

     strMonth = t.month.to_s

8dH V8f(S Hq,l{ @!{0b0

   end

'?l~P6M7W-{E0

 

O,s5IM s8`0

   # Format the year

(nY"[6L G w0

   strYear = t.year.to_s

'` pPgQ*b;[ o0

 51Testing软件测试网z-G7p o(B u@g

   # Format the hour

Cd#rh u U TjcK0

   if(t.hour.to_s.length == 1)51Testing软件测试网j S.r N6W

     strHour = '0' + t.hour.to_s

,k+v.bj'd0

   else

-hY/OqC Z0

     strHour = t.hour.to_s

v\C-o/S0

   end51Testing软件测试网(g,t:snd6pg9jj

 

@_a)ug V0

   # Format the minutes51Testing软件测试网s@ F J6A'go

   if(t.min.to_s.length == 1)51Testing软件测试网O+Nvk;D&A2Yi

     strMinutes = '0' + t.min.to_s51Testing软件测试网1L7Q(FP)|a2D`)a

   else

h DjZ+zd0

     strMinutes = t.min.to_s51Testing软件测试网_[W&J*_4q

   end

,fc%R vap@"[H0

 

bd$I+Q|&?&[0

   # Format the seconds

"F4abg a D];gq0

   if(t.sec.to_s.length == 1)51Testing软件测试网Y"i EG2W$D`o0Fd

     strSeconds = '0' + t.sec.to_s

'j~Cu~,S(g9Dc0

   elsif (t.sec.to_s.length == 0)

I*q:[-d9u Onis0

     strSeconds = '00'51Testing软件测试网Q}o |1|)iE*d

   else51Testing软件测试网]1J Ob&r9j'u

     strSeconds = t.sec.to_s51Testing软件测试网 a\@)p]Nq

   end

#M\a@L0

 

8^4s1H~$bCr'q0

   # Create the report name51Testing软件测试网\.j(i#LQ ^W0x8_@

   strTime = '_' + strDay + strMonth + strYear + '_' + strHour + strMinutes + strSeconds + '.html'

@*BPbLo0

   strNiceTime = strDay + '-' + strMonth + '-' + strYear + ' @ ' + strHour + ':' + strMinutes + ':' + strSeconds51Testing软件测试网_(aO*h%I8OG

   strTotalReport = reportName + strTime51Testing软件测试网M5D%q1X2O

 51Testing软件测试网 QkO zQO/H O

   # Create the HTML report

G&?0o:J'f/\Rnc0

   strFile = File.open(strTotalReport, 'w')51Testing软件测试网k4S(B(H&du*~h't

 51Testing软件测试网rk,h)p Q8pY$m

   # Format the header of the HTML report

5Z!N,t`%D;z+?Y1O0

   @reportContent1 = '<html>

6xKAD&~B0

     <head>51Testing软件测试网b:Z'_(C(?3\

     <meta. content=text/html; charset=ISO-8859-1 http-equiv=content-type>51Testing软件测试网9`:b"nS{.z*C X

     <title>QA Test Report</title>

z3UvxB,Vg#e-d,M'Y:`Y0

     <style. type=text/css>

:A3A0vR+] t+_-H0

     .title { font-family: verdana; font-size: 30px; font-weight: bold; align: left; color: #045AFD;}51Testing软件测试网TDq H6dr

     .bold_text { font-family: verdana; font-size: 12px; font-weight: bold;}51Testing软件测试网o J8BWT @j

     .normal_text { font-family: verdana; font-size: 12px; font-weight: normal;}51Testing软件测试网1hCFIBf'd

     .small_text { font-family: verdana; font-size: 10px; font-weight: normal; }

c0?!g?QoSv0

     .border { border: 1px solid #045AFD;}

a c!Q9A6x[3IC0

     .border_left { border-top: 1px solid #045AFD; border-left: 1px solid #045AFD; border-right: 1px solid #045AFD;}

7b1N]#Z4j5w1r6kKU0

     .border_right { border-top: 1px solid #045AFD; border-right: 1px solid #045AFD;}51Testing软件测试网d*G;L!Y H

     .result_ok { font-family: verdana; font-size: 12px; font-weight: bold; text-align: center; color: green;}51Testing软件测试网 {$K u)u_X k

     .result_nok { font-family: verdana; font-size: 12px; font-weight: bold; text-align: center; color: red;}

t0u:_l(g#s0

     .overall_ok { font-family: verdana; font-size: 12px; font-weight: bold; text-align: left; color: green;}51Testing软件测试网 M:D"n`~3c4j

     .overall_nok { font-family: verdana; font-size: 12px; font-weight: bold; text-align: left; color: red;}

2Ebj?4f&BF ^q{0

     .bborder_left { border-top: 1px solid #045AFD; border-left: 1px solid #045AFD; border-bottom: 1px solid #045AFD; background-color:#045AFD;font-family: verdana; font-size: 12px; font-weight: bold; text-align: center; color: white;}

`7ps7J CN0

     .bborder_right { border-right: 1px solid #045AFD; background-color:#045AFD;font-family: verdana; font-size: 12px; font-weight: bold; text-align: center; color: white;}

uS{,~4@.z-g0

     </style>

Q;e!Asg;o'x0

     </head>51Testing软件测试网 {9G)QUMS-C

     <body>51Testing软件测试网r GN-p0R4Z!{Qy

     <br>51Testing软件测试网5?cM"r E0Jp.H

     <center>51Testing软件测试网Nz!n$D9\N{R

     <table width=800 border=0 cellpadding=2 cellspacing=2>

,|t V-WF:p ux KB0

     <tbody>51Testing软件测试网zcy;YC3w&x#i

     <tr>

:C(ZK kng0

     <td>51Testing软件测试网%Z _Y%vCmR*T

     <table width=100% border=0 cellpadding=2 cellspacing=2>

8C Yt [iT7}I Y0

     <tbody>51Testing软件测试网 PW!@ V6au

     <tr>51Testing软件测试网I$e.Rsw2qI.Pv

     <td style=width: 150px;>&nbsp;</td>

PG@ `NK)w0

     <td align=right><p class=title>QA Test Report</p></td>

y$jos/JoA0

     </tr>51Testing软件测试网8RvcFCM w

     </tbody>

pa"Swe0

     </table>51Testing软件测试网KEdqX7Rs*|$r W/H

     <br>

2STl!e M l0

     <hr width=100% class=border size=1px>51Testing软件测试网/XM2yu0z2A s

     <br>

@}(| x3XNSm-ddO0

     <br>51Testing软件测试网5ymD5gxr-j_

     <center>

8V0g'nI6jL S&Qj@ Q0

     <table border=0 width=95% cellpadding=2 cellspacing=2>

cI+L0^2a)J|k*L3L0

     <tbody>

~%q6szN.s0

     <tr>

4^"pSY!PB5V h7~,t0

     <td width=20%><p class=bold_text>Report Name</p></td>

B zvNeD2e b0

     <td width=5%><p class=bold_text>:</p></td>

wO.|I'i{B6y9M_2b0

     <td width=75%><p class=normal_text>' + strTotalReport + '</p></td>51Testing软件测试网 g!fW~,Q+Yr

     </tr>

$edY oTX8x'@0

     <tr>51Testing软件测试网6Y3dyn1P-`Cj

     <td width=20%><p class=bold_text>Test Execution</p></td>

p5}!S0NLJ0

     <td width=5%><p class=bold_text>:</p></td>

g)[D~c$V0

     <td width=75%><p class=normal_text>' + strNiceTime + '</p></td>51Testing软件测试网~ fn ^1kk

     </tr>

1Fz3WCm2Sn mh9Hm_0

     <tr>

u4m7c BtBE0

     <td width=20%><p class=bold_text>Overall Result</p></td>

w&ZBZ;JA U8QJ0

     <td width=5%><p class=bold_text>:</p></td>'51Testing软件测试网G:Qv6i}4@n

 51Testing软件测试网qo,]4G9m+o T

   @reportContent2 = '</tr>

Q.~lU8Mq J6K0

     </tbody>51Testing软件测试网;zez2wzWN*J5B

     </table>51Testing软件测试网"o*Kn#]d~&w

     </center>

RF[ B{8gcd+o0

     <br><br>

lI8C'H;v2z:O,j0

     <center>51Testing软件测试网,ZpP(_:z|)j#V H7N

     <table width=95% cellpadding=2 cellspacing=0>

un uXsc x0

     <tbody>51Testing软件测试网i3u\,qql F

     <tr>51Testing软件测试网$O0L0g^bF?Z

     <td class=bborder_left width=30%><p>Test Step</p></td>51Testing软件测试网 K0a YKjK(QD!Sj(M,SV

     <td class=bborder_left width=10%><p>Result</p></td>

Q$J'~ j Rb0?)R0

     <td class=bborder_right width=60%><p>Description</p></td>51Testing软件测试网c5rWBgKI9Al

     </tr>'51Testing软件测试网? ] p'm0t

 

#GNn Z+VD(JU0

   # Close the report51Testing软件测试网,Q$ba o:s+Q7Z)J

   strFile.close51Testing软件测试网&{Xs\'}t(A

 

o(@7WU]%MX WD)p0

   return strTotalReport

uk?+_(q8T-U0

 end51Testing软件测试网Gh Ro$Z"p8O `

 51Testing软件测试网9\f P9r}V

 def addtoReport(reportName, step, result, description)

N8Z4a;X4l}(Y0

   @reportContent2 = @reportContent2 + '<tr><td class=border_left width=30%><p class=normal_text>' + step + '</p></td>'

,[&CQ&zI5lE0

 51Testing软件测试网0?#te IhJ

   # Format the body of the HTML report

ant,F-f^1Ycd0

   if (result == 'PASSED')

Y8L uu'y)h.B0

     @reportContent2 = @reportContent2 + '<td class=border_right width=10%><p class=result_ok>' + result + '</p></td>'51Testing软件测试网/kF&TU/Y*l6T

   else51Testing软件测试网:q%X7BG3Q1^ bh

     @overallResult = 'FAILED'

H2h~l@W"p:P-p7d,OP0

     @reportContent2 = @reportContent2 + '<td class=border_right width=10%><p class=result_nok>' + result + '</p></td>'

0xyI9HBex})n"s0

   end

xz_z(U,GKrj0

 

8Z8AzMs.Rpt0

   @reportContent2 = @reportContent2 + '<td class=border_right width=60%><p class=normal_text>' + description + '</p></td></tr>'51Testing软件测试网 ]9ZD~(m*^N

 end

J6w1dx;Th0

 

1jUou)[){~0

 def finishReport(reportName)

O"?.O t%x^6l-h0

   # Open the HTML report

M)N}bC)[d0

   strFile = File.open(reportName, 'a')

5[ K2bV.A y0

 51Testing软件测试网C kZvv.U`

   # Format the footer of the HTML report51Testing软件测试网@{&v8J4W._

   @reportContent2 = @reportContent2 + '<tr>51Testing软件测试网 `oS0h LM+Y$Y R

     <td class=bborder_left width=30%><p>&nbsp;</p></td>51Testing软件测试网'^*N|-D}C&N

     <td class=bborder_left width=10%><p>&nbsp;</p></td>51Testing软件测试网a%_]z|)h

     <td class=bborder_right width=60%><p>&nbsp;</p></td>

%OV0m*v.Iw(\0

     </tr>

X/@T6Q!_"p/U0

     </table>51Testing软件测试网z T|nX

     <br><br>

)^ Qj3\FQ&`j0

     <hr width=100% class=border size=1px>51Testing软件测试网OWqg1Z+L!\Dj2Y

     <br>51Testing软件测试网!w+D\1?7Xu;Q)ch

     <center><p class=small_text>&copy2008</p></center>51Testing软件测试网$_{0w#glLf}a ^.tG ?

     <br>'

"C7A9s&u2l$k0

 

yCzF%x0

   strFile.puts(@reportContent1)

|5Ae'U5aH0

 51Testing软件测试网 o4]M"z"g)E)Z3L:}

   if (@overallResult == 'PASSED')

]%u:L Csn0

     strFile.puts('<td width=75%><p class=overall_ok>' + @overallResult + '</p></td>')

x1^h y5CM |Xc4R$b3Z0

   else51Testing软件测试网5\#MT'TvQ'hZ

     strFile.puts('<td width=75%><p class=overall_nok>' + @overallResult + '</p></td>')51Testing软件测试网/X@R7TWWj7Cp4}

   end51Testing软件测试网QoD6_}/_9~v

 51Testing软件测试网 AR ? f d6z"P d

   strFile.puts(@reportContent2)

#t,d/H&ncW2Z0

 

y}8y@,B2|F*q{,q0

   # Close the report

#{;u7`Q3v ~AQX!ZR0

   strFile.close

y'rbevtmN/FM0

 end51Testing软件测试网-M eS4@/{.FK

end51Testing软件测试网J{#Id`

 51Testing软件测试网1P+xI5Z2FO\?2n

report测试51Testing软件测试网)?/pvQ6JAp/J

# Load classes51Testing软件测试网g M*lBt(F

require 'rubygems'51Testing软件测试网e2d?#IH

require 'watir'

N gnmR7v0

require 'CLReport.class.rb'51Testing软件测试网"VL*P2Z2P+O

 

MnV@R Bk:pz0

# The main test51Testing软件测试网!V A-RK'?Jca

begin

g I7O T#W+P6~*f c0

 # Create the new report51Testing软件测试网hZ$C*T2F-CTp+Wy"b;p

 r = CLReport.new()

CM\F\;x;n6Q V0

 testReport = r.createReport('E:\\Leggat Script\\Aps\\SystemAdmin\\ExampleScript')

{2L3e8K.[`2B0

 51Testing软件测试网2c~-cn3@

 # Start browser

I D6C.F/DJ%P,s0

  browser = Watir::IE.new

#Sh;J#b0P2O0

 browser.goto('http://www.baidu.com')51Testing软件测试网T@+o2l1~2z'v`6W

 

3?\0xZ'T6H)vU0

 # --- START TEST ---

OY'j&_obw4O0

 51Testing软件测试网2Z8S XZ`q M4ng

 # Do a check and report the result

u2_(?G5D,E/S0

 if (ie.contains_text("hao123"))

Tli#M&Z1j U4G8bM0

   r.addtoReport(testReport, 'Check if contain text hao123', 'PASSED', 'page contains text hao123!')

'C_HY)\'R.@0

 else

J8e1fRGLZ}S.B0

   r.addtoReport(testReport, 'Check if contain text hao123', 'FAILED', 'page not contains text hao123!')51Testing软件测试网 b+Q&hT"S*E*y I9e.I

 end51Testing软件测试网XGn:\/~9g yN?B$`

 51Testing软件测试网c\B6^%V??

 # Do another check and report the result

Se&} P3a z0

 if (ie.contains_text("About Baidu"))

G}nU(J3~0

  r.addtoReport(testReport, 'Check if contain text About Baidu', 'PASSED', 'page contains text About Baidu!')51Testing软件测试网1S*vVlQI.e/{&ua#`

 else

2K+h|"w8_,D n0MA H0

   r.addtoReport(testReport, 'Check if contain text About Baidu', 'FAILED', 'page not contains text About Baidu!')

+WdM)r g.{0

 end51Testing软件测试网,zLL*_%H'Q|V#d

 51Testing软件测试网 I,_p[3i"~

 # --- END TEST ---

)w9uAd"Lr5c#`0

 51Testing软件测试网Au:}k"e G5j

 # Close browser

lmdi9\C|SV0

 browser.close

2If#I#d'c!cfW"l0

 

}!Z.o\ P+`&\i0

 # Finish the report

^S,O S b9g0

 r.finishReport(testReport)51Testing软件测试网rck-_pa

rescue51Testing软件测试网2B@OUhy

 # Send crash info to report

$|%EW?[W8ece0

 r.addtoReport(testReport, 'Test crashed with reason '+$!, 'FAILED', 'Test crashed!')51Testing软件测试网0_*Ti8pV'j~|

 

X(CA1F9M"ar$BV0

 # Finish the report

N^9WoY0

 r.finishReport(testReport)51Testing软件测试网)o R D/AK)VyU6h#r

end

-kp|0x;M,t)RDy%Z0

TAG:

nbkhic的个人空间 引用 删除 nbkhic   /   2009-08-27 16:20:06
使用TestUnit然后调用RSpec可能效果更好。
-DNA's home 引用 删除 huoxingyinzi   /   2009-08-25 15:14:28
发现自己最近特别钟爱于watir这个开源测试工具
我想他是没有QTP或者Robot容易上手的,简单的录制与回放,但是却不明白原理,遇到不识别的控件,不会编程,找不到解决方案,就停止脚步了。
但是waitr是一行一行代码的书写,让我明白每个控件的属性和对象,让更加了解什么是自动化测试框架,,更加明白自动化测试怎么去扩展,明白自动化实现的原理。
至少这是一种转变,一种让测试停留在工具上运行的转变!
测试不仅仅需要测试思想,还有丰厚的计算机知识,和软件工程理论知识,和开发语言基础
 

评分:0

我来说两句

Open Toolbar