Selenium自动化测试框架的搭建

张开发
2026/4/18 7:47:35 15 分钟阅读

分享文章

Selenium自动化测试框架的搭建
点击文末小卡片免费获取软件测试全套资料资料在手涨薪更快今天我就简单入个门先介绍通过junitseleniumCoverlipseant来搭建一整套自动化测试框架开始之前我先提出几个问题请大家根据下面的问题来阅读本篇文章。1.如何录制脚本2.如何转换成junit脚本?3.如何生成junit日志?4.如何回放selenium及查看回放日志?5.如何查看代码的覆盖率?一、工具准备工具说明下载junitJUnit是一个开发源代码的Java测试框架用于编写和运行可重复的测试。http://www.junit.orgselenium先介绍两个重要的组件Selenium-IDE来录制脚本selenium-rc selenium-remote control缩写是使用具体的语言来编写测试类SeleniumCoverlipseCoverlipse这个Eclipse插件能够把JUnit测试的代码覆盖直观化。DownloadAntAnt是一个类似make的工具,大家都已经很熟悉了这里我们可以利用其中的ant task来生成junit日志Apache Ant - Binary Distributions二、Junit的安装1.Eclipse本身会自带Junit.jar包所一般情况下是不需要额外下载junit的。2.将junit3的library添加到工程的buildPath中3.因为junit的日志是通过Ant来生成的所以一定要将Junit.jar添加到ant_home里三、selenium的安装1.安装seleniumIDE打开火狐浏览器然后进入工具—添加附件搜索seleniumIDE2.查询出对应的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启动seleniumserver。为了在运行时应用运行的浏览器与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.一旦单击了RunEclipse会运行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 nameseleniumTestdefaultjunit basedir. propertyenvironmentenv / conditionpropertyia.home value${env.IA_HOME} issetpropertyenv.IA_HOME / /condition propertynamerun.classpath value../class /property propertynamerun.srcpath value../testSelenium /property propertynametest.xml value../xml /property propertynametest.report value../report /property propertynamelib.dir value../lib / pathidcompile.path filesetdir${lib.dir} includenamejunit.jar / includenameant.jar / /fileset /path targetnameinit deletedir${run.classpath} / mkdirdir${run.classpath} / deletedir${test.report} / mkdirdir${test.report} / deletedir${test.xml} / mkdirdir${test.xml} / /target targetnamecompile dependsinit javacdestdir${run.classpath} srcdir${run.srcpath} / /target targetnamejunit dependscompile junitprintsummaryfalse classpathpath${run.classpath} pathrefidcompile.path / /classpath formattertypexml / batchtesttodir${test.xml} filesetdir${run.classpath} includename**/Test*.class / includename**/*Test.class / /fileset /batchtest /junit junitreporttodir${test.xml} filesetdir${test.xml} includenameTEST-*.xml / /fileset reportformatframes todir${test.report} / /junitreport /target /project2.运行ant脚本以后就可以生成相应的junit日志。3.selenium日志当运行junit脚本时selenium相关的脚本就会产生回放日志但默认记录的东西可读性太差了所以我们使用loggingSeleniumLoggingSelenium - 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的录制而且是国产的评价还不错有时间我要研究一下。最后感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走这些资料对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴我走过了最艰难的路程希望也能帮助到你凡事要趁早特别是技术行业一定要提升技术功底。

更多文章