🍅 点击文末小卡片 ,免费获取软件测试全套资料,资料在手,涨薪更快
今天我就简单入个门,先介绍通过junit+selenium+Coverlipse+ant来搭建一整套自动化测试框架,开始之前我先提出几个问题,请大家根据下面的问题来阅读本篇文章。
1.如何录制脚本?
2.如何转换成junit脚本?
3.如何生成junit日志?
4.如何回放selenium及查看回放日志?
5.如何查看代码的覆盖率?
一、工具准备
| 工具 | 说明 | 下载 |
| junit | JUnit是一个开发源代码的Java测试框架,用于编写和运行可重复的测试。 | |
| selenium | 先介绍两个重要的组件Selenium-IDE来录制脚本;selenium-rc selenium-remote control缩写,是使用具体的语言来编写测试类 | |
| Coverlipse | Coverlipse这个Eclipse插件能够把JUnit测试的代码覆盖直观化。 | |
| Ant | Ant是一个类似make的工具,大家都已经很熟悉了,这里我们可以利用其中的ant task来生成junit日志 |
二、Junit的安装
1.Eclipse本身会自带Junit.jar包,所一般情况下是不需要额外下载junit的。

2.将junit3的library添加到工程的buildPath中

3.因为junit的日志是通过Ant来生成的,所以一定要将Junit.jar添加到ant_home里

三、selenium的安装
1.安装seleniumIDE,打开火狐浏览器,然后进入工具—>添加附件,搜索seleniumIDE

2.查询出对应的IDE后,点击直接安装,安装结束后重启FireFox,看到下面的菜单说明安装成功

3.安装selenium-rc,先去http://www.openqa.org/selenium/下载selenium包。用命令行来到解压到文件夹下:d:/autoTesting/selenium-server-standalone-2.0b1.jar目录下
4.运行java -jar selenium-server-standalone-2.0b1.jar,启动selenium server。为了在运行时应用运行的浏览器与selenium服务的浏览器分开可在其后面加–multiWindow。

5.在Eclipse创建一个项目,在项目的build path里面加上elenium-server-1.0-beta-1下selenium-server.jar、selenium-java-client-driver-1.0-beta-1下selenium-java-client-driver.jar(这两个在刚解压的包里面)和eclipse/plugins/org.junit_3.8.1下junit.jar。

6.将制定的Jar包导入到工程里,然后你就可以集成并使用相应的API,编写自己的测试CASE了。
四、Coverlipse的安装
1.直接通过Eclipse即可安装,步骤如下
- In Eclipse, click Help -> Software Updates -> Find and Install.
- In the dialog, select Search for new features to install, then Next.
- In the next step, add a New Remote Site. Name it "Coverlipse update site", the URL is "http://coverlipse.sf.net/update/".
- Press Finish. Eclipse now searches for the Coverlipse feature to install and shows that to you.
2.配置Coverlipse以获取代码覆盖
3.一旦单击了Run,Eclipse会运行Coverlipse并在源代码(如图7所示)中嵌入标记,该标记显示了具有相关JUnit测试的代码部分
4.Coverlipse生成的具有嵌入类标记的报告

5.正如您所见,使用Coverlipse Eclipse插件可以更快地确定代码覆盖率。例如,这种实时数据功能有助于在将代码签入CM系统前更好地进行测试。
五、ANT安装,eclipse自带,只需要配置环境变量ant_home即可。
六、创建一个案例
1.创建一个工程testSelenium安装下面目录结构

2.录制脚本,打开Firefox浏览器,进入selenium IDE菜单

3.输入相应录制的地址,点击红色按钮,开始录制
4.将脚本转换成junit代码,然后将其拷贝到测试类中做为测试CASE编码的雏形。
六、如何查看日志,这里日志分两类:
Junit日志,通过junit写的断言,和标准输出,这些操作产生的日志记录。
Selenium日志,当运行junit脚本时,selenium相关的脚本就会产生回放日志,例如打开界面的url,标准输入,输出等信息。
虽然这两种日志没有交集,需要分开查看。但一般情况下我们只需要观察Selenium日志已经足够用了,与其相比Junit日志更适用于编码阶段。
1.Junit日志,只需要配置脚本build-selenium.xml,如下
<project name="seleniumTest"default="junit" basedir=".">
<propertyenvironment="env" />
<conditionproperty="ia.home" value="${env.IA_HOME}">
<issetproperty="env.IA_HOME" />
</condition>
<propertyname="run.classpath" value="../class">
</property>
<propertyname="run.srcpath" value="../testSelenium">
</property>
<propertyname="test.xml" value="../xml">
</property>
<propertyname="test.report" value="../report">
</property>
<propertyname="lib.dir" value="../lib" />
<pathid="compile.path">
<filesetdir="${lib.dir}">
<includename="junit.jar" />
<includename="ant.jar" />
</fileset>
</path>
<targetname="init">
<deletedir="${run.classpath}" />
<mkdirdir="${run.classpath}" />
<deletedir="${test.report}" />
<mkdirdir="${test.report}" />
<deletedir="${test.xml}" />
<mkdirdir="${test.xml}" />
</target>
<targetname="compile" depends="init">
<javacdestdir="${run.classpath}" srcdir="${run.srcpath}" />
</target>
<targetname="junit" depends="compile">
<junitprintsummary="false">
<classpathpath="${run.classpath}">
<pathrefid="compile.path" />
</classpath>
<formattertype="xml" />
<batchtesttodir="${test.xml}">
<filesetdir="${run.classpath}">
<includename="**/Test*.class" />
<includename="**/*Test.class" />
</fileset>
</batchtest>
</junit>
<junitreporttodir="${test.xml}">
<filesetdir="${test.xml}">
<includename="TEST-*.xml" />
</fileset>
<reportformat="frames" todir="${test.report}" />
</junitreport>
</target>
</project>
2.运行ant脚本以后,就可以生成相应的junit日志。

3.selenium日志
当运行junit脚本时,selenium相关的脚本就会产生回放日志,但默认记录的东西可读性太差了,所以我们使用loggingSelenium(LoggingSelenium - Usage) ,可以将每个case可以生成记录selenium命令的html格式的result了。
4.plugin的下载地址:
Download logging-selenium-1.2.jar (LoggingSelenium)
5.安装方法:只需要将下载的logging-selenium-1.2.jar导入到工程里即可。

6.编写代码如下
@Before
public void setUp() {
finalString resultPath ="absolute-path-to-where-your-result-will-be-written";
finalString resultHtmlFileName = resultPath + File.separator +"result.html";
finalString resultEncoding = "UTF-8"
loggingWriter = LoggingUtils.createWriter(resultHtmlFileName,resultEncoding);
LoggingResultsFormatter htmlFormatter =
newHtmlResultFormatter(loggingWriter, resultEncoding);
htmlFormatter.setScreenShotBaseUri(""); // this is for linkingto the screenshots
htmlFormatter.setAutomaticScreenshotPath(resultPath);
// wrapHttpCommandProcessor from remote-control
LoggingCommandProcessor myProcessor =
new LoggingCommandProcessor(newHttpCommandProcessor(your-configs), htmlFormatter);
selenium= new LoggingDefaultSelenium(myProcessor);
selenium.start();
}
@After
public void tearDown() {
selenium.stop();
try {
if(null != loggingWriter) {
loggingWriter.close();
}
} catch(IOException e) {
//do nothing
}
}
7.运行成功以后在指定的目录中生成相应的reports

七、框架优势
1.记录测试的过程,所见即是所得,selenium的所有内部程序都是用Javascipt编写的,比较灵活;
2.可以通过selenium IDE录制脚本,脚本可以回放,可以作为junit编码的雏形;
3.支持多种操作系统;
4.支持多种编码语言。JAVA,.NET, Perl,Python, Ruby
八、框架劣势
1.selenium的录制工具只能安装在firefox浏览器上, 如果系统界面不被firefox支持,那就要花费一定的时间去手写case。 不过最近听说有一个工具叫360WebTester ,可以支持IE的录制,而且是国产的评价还不错,有时间我要研究一下。

最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。



793

被折叠的 条评论
为什么被折叠?



