更多的文章,访问我的个人博客:www.cesoo.info

在AC上开发selenium脚本

上一篇 / 下一篇  2010-11-25 11:44:56 / 个人分类:自动化测试框架

开发selenium Case

1.1 基于录制回放的selenium case

 51Testing软件测试网|9D DW1RT2Bv#sl

录制一个简单的web计算器功能,exportjunit模式,保存为selJava.java文件,如下:51Testing软件测试网.jk6pm"Z

package com.example.tests;

D)p't'E m U'V X0

import com.thoughtworks.selenium.*;

Z$]gd8p$aL,[o0

import java.util.regex.Pattern;51Testing软件测试网Yj4s4f;z:k%Upj&i

public class selJava extends SeleneseTestCase {

Y7lZ Z"Fg}%@0

      public void setUp() throws Exception {51Testing软件测试网5O!k{se

             setUp("http://change-this-to-the-site-you-are-testing/", "*chrome");

\e3o~7m iBp0

      }51Testing软件测试网.W"{6MM$d#Fz O

      public void testSelJava() throws Exception {51Testing软件测试网,F\A^L)z

             selenium.open("/calc.htm");51Testing软件测试网 d0qPx)h

             selenium.click("//input[@name='alex' and @value='1']");

5e9YMC)c3l K0

             selenium.click("//input[@name='alex' and @value='+']");

;d6f7y6}4ujNyizH0

             selenium.click("//input[@name='alex' and @value='2']");51Testing软件测试网tK];|Rn X!x

             selenium.click("//input[@name='alex' and @value='=']");

(Y&@&h!b&`0

             verifyEquals("3", selenium.getValue("display"));

2r'ZH5A5C K,Yl:h0

      }

q)k9xgi mg0

}

3__:oP4n0

以上Selenium case继承SeleneseTestCaseSeleneseTestCase的父类是Junit TestCase,因此,在默认模式下,Selenium Case实际上是以Junit Runner方式运行的。51Testing软件测试网9n!g;PviLJ

Junit本身是java单元测试框架,并不完全满足我们selenium的测试需求,比如对case之间的依赖关系及数据交互,web测试抓图等等,junit并不能胜任。51Testing软件测试网 V:@ T LF6JTI7I

 51Testing软件测试网YH8bI&|R

1. Selenium代码从Junit模式转化到AC模式

可遵循以下步骤,将junit模式的selenium代码转化成在AC模式:51Testing软件测试网#j Ch)I,K*A2B1N:g\

1.      java环境中的classPath添加ac_framework.jar

9jB[,`~H Q0

2.      case文件头添加import framework.JobDOM.ACSeleniumJob;

?m9CA!n#YE#c`0

selJava的父类改为ACSeleniumJob51Testing软件测试网*o]Es e

public class selJava extends ACSelniumJob

(y1N]:?H U3DnA0

 

BG[zd5W*v0

Ok,到这里,新的selenium case已经完成了(红色为修改处),如下:

1^ @8\Z4s~ d0

package com.example.tests;51Testing软件测试网/~OG-?Y||

import com.thoughtworks.selenium.*;51Testing软件测试网oP9s)l&n~#O w'fo s

import java.util.regex.Pattern;

Rb^8` S ` dz:B#k0H2z0

import framework.JobDOM.ACSeleniumJob

Q `0^R1dVE v-aV+Z0

public class selJava extends ACSeleniumJob {

P\ s6@3Wo)z0

      public void setUp() throws Exception {51Testing软件测试网)A:t6Hgm"}"mQ/G1o jJ"r

              setup("http://change-this-to-the-site-you-are-testing/", "*firefox");

w?F,@2KvG-W!]0

      }51Testing软件测试网Z8CU7P3wu&PRw

      public void testSelJava() throws Exception {51Testing软件测试网[!UP(u{1u8_

             selenium.open("/calc.htm");51Testing软件测试网Og2}!C @

             selenium.click("//input[@name='alex' and @value='1']");

9Zl_]/Ft Qcw0

             selenium.click("//input[@name='alex' and @value='+']");

?.T$P Gi|/dQ0

             selenium.click("//input[@name='alex' and @value='2']");

8\H-P F[ y/J5n0

             selenium.click("//input[@name='alex' and @value='=']");51Testing软件测试网eViePz#EY7Us

             verifyEquals("3", selenium.getValue("display"));51Testing软件测试网W3n.Q Rt8I&\

      }

zuAr yv#\.{0

}

:p"X _$SwDqG*a(Ce0

 51Testing软件测试网p6kRQ$t9xQ

2.Selenium case添加到TestJobFile中,运行AC,获得测试结果

TestJobFile中添加Selenium Job,按如下格式定义51Testing软件测试网;S!e x ]%h

<Seleniumname="selenium_demo" description="Test calc" depends="" >51Testing软件测试网 C6Jy Nu6r S(J5?$a7s

                  <TestData type="xml" location="selenium\config.xml"/>51Testing软件测试网fb0I@/JT

                  <JobInput name="$MAIL_SUBJECT"/>

,o ps Q\*Tx2Z y2M?(l6C0

                  <ClassPath location="selenium\selenium-java-client-driver_self_extended_oracle.jar"/>

*z-RBQ'nY0

                  <ClassPath location="selenium\orajtst.jar"/>

9PV*G+vk rG0

             <ClassPath location=" selenium\qa.jar"/>51Testing软件测试网RL&e-L1f Z

<ClassPath location=" selenium\selJava.class"/>

$\6RQK^9B3o!H0

                  <SelTestCasepath="selJava">

8GNC[/V5V$@0

                           <SelTestname="testSelJava"/>51Testing软件测试网5C#iO]vD!E

                  </SelTestCase>51Testing软件测试网8}'r[g,P \)w9w

</Selenium>51Testing软件测试网6i)wQK^.d4j V~G

运行AC framework,即可执行selenium Job,并获得测试报告51Testing软件测试网[-u7K3q8m0R*Lt

3   将启动selenium server集成到TestJob

我们最常用的是Selenium RC模式,即先启动一个selenium server,然后才能运行selenium脚本。

P pXj(f"RwX,y%T0

java启动selenium server的命令行语句如下:

N e2D\%wd K \0

java –jar selenium-server.jar –port 4444 – proxyInjectionMode –log selServer.txt

JYV!ES0

如何把启动selenium serverjava命令行也集成到AC中来呢?

"{;WlE3p+v)\ b0

这里要用到ACAnt Engine51Testing软件测试网})M"Nd)dtl T u

创建基于Ant EngineTestJobTestJob内容遵循Ant语法,如下:51Testing软件测试网x1T7s"@+g?:?7A

<Antname="Ant_StartSelenium" description="selenium initialization" depends="" daemon="true">

1ro VnZ3~ O|,[0

                  <java fork="true" spawn="true" jar="D:\selenium-server.jar">

PG)rF~ZE0

                           <arg line="-port 4444 "/>

Dt2Q$q;qb VA0

                           <arg line="-proxyInjectionMode"/>51Testing软件测试网)F6Obv k

                           <arg line="-log sel.txt"/>

U [/WJk0

                  </Java>

6XHr\X0

        </Ant>51Testing软件测试网n Q(h:iPN

 

'Mu u6e|0

启动Selenium serverTestJob可与Selenium Test Job做一个dependence的定义,保证每次运行selenium测试的时候,selenium server是处于启动状态的

1O}{(BL.F*wJPC0

 

i(e'a2E.k$Oe*i0

<Antname="Ant_StartSelenium" description="selenium initialization" depends=""driver_type="ANT"daemon="true">

OdD j"` M0

………………………51Testing软件测试网~^,hotH(v.tv

</Ant>51Testing软件测试网jr `d#P#~2W

<Seleniumname="selenium_demo" description="Test calc" depends=" Ant_StartSelenium ">51Testing软件测试网zojh S Pl{j

…………………….

\#V9N7j2iB3TB0

</Selenium>

a+~ t^!R.g@"C0

  

]U5G ^b'O B1m0

4   框架提供的selenium API

pass(String msg):AC汇报当前运行状态,为成功

7}{ j y0|6R0{%G f0

fail(String msg):向AC汇报当前运行状态,为失败

Z%oIk;B;Wu9G;So0

reportWarning(String msg):向AC汇报当前运行状态,为警告51Testing软件测试网*`N y{@&n

getDataProperty(String key):获得测试数据

'R H,o7_'X4^0

getEnvProperty(String key):获得环境变量及输入数据

{voJ%z-M0

getConfProperty(String key):获得配置数据51Testing软件测试网6b#G$Q+qy/K{3n

setOutputValue(String key,String value)向全局数据通道输出数据51Testing软件测试网,g2ih o:D5a?_


!_Ck2u$q S0

im%A/R8v u0

 

-n$a"V.cCV+o``#Q^9Z0

5    Automation 报告

运行TestJob,产生基于html页面的测试报告

p*GmiP'D{"jl0

总览报告51Testing软件测试网:n7l E6hy*H4Q

51Testing软件测试网~U_ u2M"NS6c

细分诊断报告51Testing软件测试网n} s7k0Z*_-J6~2y|Y

51Testing软件测试网+yEv_(P8F


| @VB ok0

7}o2t&tnF&t0


ZA2clE,h)M0

*dJeb,M#f0@0

 

,gS#cWz%Fb0

TAG:

 

评分:0

我来说两句

Open Toolbar