51Testing软件测试论坛

标题: 使用AutomationCenter管理运行Selenium脚本 [打印本页]

作者: sunshinelius    时间: 2010-11-29 09:47
标题: 使用AutomationCenter管理运行Selenium脚本
本帖最后由 sunshinelius 于 2010-11-29 09:55 编辑

1. 开发selenium Case
1. 基于录制回放的selenium case

录制一个简单的web计算器功能,exportjunit模式,保存为selJava.java文件,如下:


  1. package com.example.tests;import com.thoughtworks.selenium.*;import java.util.regex.Pattern;public class selJava extends SeleneseTestCase {       public void setUp() throws Exception {               setUp("http://change-this-to-the-site-you-are-testing/", "*chrome");       }       public void testSelJava() throws Exception {               selenium.open("/calc.htm");              selenium.click("//input[@name='alex' and @value='1']");              selenium.click("//input[@name='alex' and @value='+']");              selenium.click("//input[@name='alex' and @value='2']");              selenium.click("//input[@name='alex' and @value='=']");              verifyEquals("3", selenium.getValue("display"));       }}
复制代码

以上Selenium case继承SeleneseTestCaseSeleneseTestCase的父类是Junit TestCase,因此,在默认模式下,Selenium Case实际上是以Junit Runner方式运行的。

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



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

可遵循以下步骤,将junit模式的selenium 代码转化成在AC模式:

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

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

selJava 的父类改为ACSeleniumJob

public class selJava extends ACSelniumJob


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


  1. package com.example.tests;import com.thoughtworks.selenium.*;import java.util.regex.Pattern;import framework.JobDOM.ACSeleniumJob;public class selJava extends ACSeleniumJob {       public void setUp() throws Exception {               setup("http://change-this-to-the-site-you-are-testing/", "*firefox");       }       public void testSelJava() throws Exception {              selenium.open("/calc.htm");              selenium.click("//input[@name='alex' and @value='1']");              selenium.click("//input[@name='alex' and @value='+']");              selenium.click("//input[@name='alex' and @value='2']");              selenium.click("//input[@name='alex' and @value='=']");              verifyEquals("3", selenium.getValue("display"));       }}
复制代码


3. 在测试任务定义文件中添加Selenium case,运行AC,获得测试结果

TestJobFile中添加Selenium Job,按如下格式定义


  1. <Selenium name="selenium_demo" description="Test calc" depends="" >                   <TestData type="xml" location="selenium\config.xml"/>                   <JobInput name="$MAIL_SUBJECT"/>                   <ClassPath location="selenium\selenium-java-client-driver_self_extended_oracle.jar"/>                   <ClassPath location="selenium\orajtst.jar"/>              <ClassPath location=" selenium\qa.jar"/><ClassPath location=" selenium\selJava.class"/>                   <SelTestCase path="selJava">                                  <SelTest name=" testSelJava "/>                   </SelTestCase></Selenium>
复制代码

运行AC framework,即可执行selenium Job,并获得测试报告

4. 使用AC的Ant Engine实现启动selenium serve自动化

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

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

  1. java –jar selenium-server.jar –port 4444 – proxyInjectionMode –log selServer.txt
复制代码

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

这里要用到ACAnt Engine

创建基于Ant EngineTestJobTestJob内容遵循Ant语法,如下:

  1. <Ant name="Ant_StartSelenium" description="selenium initialization"  depends="" daemon="true">                   <java fork="true" spawn="true" jar="D:\selenium-server.jar">                               <arg line="-port 4444 "/>                               <arg line="-proxyInjectionMode"/>                              <arg line="-log sel.txt"/>                   </Java>         </Ant>
复制代码

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



  1. <Ant name="Ant_StartSelenium" description="selenium initialization"  depends="" driver_type="ANT" daemon="true">………………………</Ant><Selenium name="selenium_demo" description="Test calc" depends=" Ant_StartSelenium " >…………………….</Selenium>
复制代码


5    框架提供的selenium API

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

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

reportWarning(String msg):向AC汇报当前运行状态,为警告

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

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

getConfProperty(String key):获得配置数据

setOutputValue(String key,String value) 向全局数据通道输出数据



6    Automation 报告

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

总览报告

细分诊断报告


作者: msnshow    时间: 2011-1-4 22:13
关注!
作者: sunshinelius    时间: 2011-3-1 23:08
AC在线文档:
http://www.cesoo.info/acdocs/zh_cn/index.html
作者: maxb2005    时间: 2011-3-3 15:51
楼主没公开AC的源代码,不方便“测友”之间的交流,可惜!
作者: sunshinelius    时间: 2011-3-4 13:51
呵呵,目前一共7万多行java源代码,今年开始,陆续在本人网站公布。如果在AC上有二次开发的需求,只需发邮件给我,我单独回复。
作者: vvlin214    时间: 2011-9-14 13:58
关注!
作者: daisy_0829    时间: 2012-4-17 15:14
关注
作者: 无花果果糖    时间: 2012-7-31 13:26
关注
作者: xiemojia    时间: 2013-8-22 10:52
顶刘老师的AutomationCenter框架,我10年的时候还参加过您的那次测试沙龙,这本书就是那次我看到的。
作者: Miss_love    时间: 2013-11-15 13:43
关注
作者: mildshark    时间: 2014-3-31 18:30
从哪里可以下载AC?
作者: 快乐天    时间: 2014-4-9 13:44
看看不知道说点什么好,还是保持沉默吧.  www.nx89103.com
作者: langhai5212    时间: 2014-4-17 16:51
报告类似Junit或者Testng自己生成的报告,没什么意思
作者: 猫星人    时间: 2015-3-24 14:41
哎哟,我支持了。。
作者: 吖吖淘乐    时间: 2015-8-25 08:58
你好,请教个问题
我们公司程序大多是java开发的,主要是支持ie浏览器
自动化测试选择Selenium
我电脑上除了安装Selenium Remote Control,Eclipse,还需要安装别的吗?
像Xpath,Firebug与Selenium Remote Control是什么关系?
作者: 吖吖淘乐    时间: 2015-8-25 09:04
你好,请教一个问题
我们公司的程序大多都是java开发的,浏览器主要是用ie
自动化测试选Selenium,想问我的电脑上除了安装Selenium Remote Control,Eclipse之外,还需安装别的吗?
Xpath、Firebug与Selenium Remote Control有什么关系?
作者: fhhh_eyou    时间: 2015-12-11 18:02
值得关注,很好呀!




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2