ant与selenium联合测试
来源:互联网 发布:南昌九九网络 编辑:程序博客网 时间:2024/06/05 20:27
摘自:http://luyongxin88.blog.163.com/blog/static/925580720112265955438/
介绍selenium与ant联合起来开展测试其中还利用junit生产测试报告
(本文建设你对selenium-RC以及ant以及junit有了一定了认识和了解)
1>用ant结合selenium启动测试的自动过程如下:
★利用selnium IDE工具创建测试脚本(java)
★修改脚本,准备文件结构
2> 本次实例的目录结构:
首先在c盘下创建ABQ-1文件夹,在此文件夹下,有
build(其中创建class、lib文件夹)
report
resource
src(其中创建baidu文件)
以及build.xml文件
3> 利用selenium IDE工具录制访问baidu.com进行搜索的java脚本
最初利用selenium录制的baidu.java
import java.util.regex.Pattern;
public class baidu extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.baidu.com/");
selenium.start();
}
@Test
public void testBaidu() throws Exception {
selenium.open("/index.php?tn=monline_5_dg");
selenium.click("lg");
selenium.type("kw", "1");
selenium.click("su");
selenium.waitForPageToLoad("30000");
selenium.click("kw");
selenium.type("kw", "2");
selenium.click("su");
selenium.waitForPageToLoad("30000");
selenium.click("kw");
selenium.type("kw", "3");
selenium.click("su");
selenium.waitForPageToLoad("30000");
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
}
4> 根据已有模板,修改这个java文件将部分测试参数分离出来,分为baidu.java与 setting.properties
新的baidu.java
package baidu;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.*;
import com.thoughtworks.selenium.SeleneseTestCase;
public class baidu extends SeleneseTestCase {
private static String PROPERTIES_FILE = "setting.properties";
private static Properties props = null;
private void initProperties() { //这里是读取C:\setting.properties文件
String path = baidu.class.getResource("/").getPath();
path = path + PROPERTIES_FILE;
System.out.print(path);
props = new Properties();
try {
props.load(new FileInputStream(new File(path)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String getConfigParam(String key) { //读取文件后,根据相应的条件读出具体的值
return props.getProperty(key);
}
public void setUp() throws Exception {
if (props == null) {
initProperties();
}
String baseUrl = getConfigParam("baseURL");
System.out.print(baseUrl);
setUp(baseUrl, "*chrome");
}
public void tearDown() throws Exception {
//selenium.stop();
}
public void testTabs() throws Exception {
if (props == null) {
initProperties();
}
selenium.open("/");
selenium.type("kw", getConfigParam("search_text"));
selenium.captureEntirePageScreenshot("C:\\login.png","");
selenium.click("su");
selenium.waitForPageToLoad("30000");
selenium.click("kw");
selenium.type("kw", "2");
selenium.click("su");
selenium.waitForPageToLoad("30000");
selenium.click("kw");
selenium.type("kw", "3");
selenium.captureEntirePageScreenshot("C:\\selenium.png","");
selenium.click("su");
selenium.waitForPageToLoad("30000");
}
}
setting.properties
baseURL=http://www.baidu.com
search_text=test
5> 编写build.xml
build.xml
<project name="Test" default="runtests" basedir=".">
<property name="build.class" value="build/class"/>
<target name="compiletests">
<mkdir dir="build/class"/>
<javac srcdir="src" destdir="${build.class}">
<classpath>
<fileset dir="build/lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
<copy todir="${build.class}">
<fileset dir="resource">
<include name="*.properties"/>
</fileset>
</copy>
</target>
<target name="runtests" depends="compiletests">
<junit fork="yes" printSummary="yes">
<classpath>
<pathelement location="build/lib/selenium-java-client-driver.jar" />
<pathelement location="build/lib/junit-4.0.jar" />
<pathelement location="${build.class}" />
</classpath>
<formatter type="xml" />
<batchtest todir="report">
<fileset dir="src">
<include name="**/*.java"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="report">
<fileset dir="report">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="report" />
</junitreport>
</target>
</project>
6> 将文件各自放到文件夹中
将 baidu.java放到src/baidu.com文件夹
将setting.properties放到resource文件夹
将build.xml放到 ABQ-1文件夹
将junit-4.0.jar selenium-java-client-driver.jar放到build/lib文件夹(这两个文件夹分别是junit和selenium-RC的东西)
7> 运行 ant
首先启动selenium-RC :java -jar selenium-server.jar
再运行ant:ant
查看运行报告
注意图片中的system.out连接可以打开我们在baidu.java中利用system.out.print打印出的内容
==========
其上实际上是在介绍以selenium+ant+junit实现一个简单的功能测试框架。但就只运行一个测试用例显然不能显示出做为框架的效果。所以下面我们再用selenium录制其他脚本,并同上修改后放入src
8>录制一个利用google搜索的脚本并修改成我们的模板,并将google.java放入是src/google文件夹下,代码如下
google.java
package google ;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.*;
import com.thoughtworks.selenium.SeleneseTestCase;
public class google extends SeleneseTestCase {
private static String PROPERTIES_FILE = "setting.properties";
private static Properties props = null;
private void initProperties() {
String path = google.class.getResource("/").getPath();
path = path + PROPERTIES_FILE;
System.out.print(path);
props = new Properties();
try {
props.load(new FileInputStream(new File(path)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String getConfigParam(String key) {
return props.getProperty(key);
}
public void setUp() throws Exception {
if (props == null) {
initProperties();
}
String baseUrl = getConfigParam("baseURL");
System.out.print(baseUrl);
setUp("http://www.google.com", "*chrome");
}
public void tearDown() throws Exception {
//selenium.stop();
}
public void testTabs() throws Exception {
if (props == null) {
initProperties();
}
selenium.open("/");
selenium.type("q", "1");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
selenium.type("q", "2");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
}
}
9>在运行ant
我们看到在运行了baidu.java后,继续运行google.java,也就是实现了测试用例的多个执行,再看看测试报告
- ant与selenium联合测试
- 如何利用 Ant 构建 Selenium 测试
- 基于Ant+Selenium+TestNG的测试框架配置
- 使用Selenium/Ant做Web应用远程自动化测试
- Eclipse+TestNG+ant+selenium++jenkins+svn自动化测试框架搭建
- Eclipse+TestNG+ant+selenium++jenkins+svn自动化测试框架搭建
- jenkins、ant、selenium、testng搭建自动化测试框架
- Selenium 与 Android自动化测试
- selenium进阶之基础--web自动测试框架(ant+junit+selenium)
- selenium+testng+gitblit+jenkins+ant自动化测试系列八:ant安装
- ANT的配置与安装与测试
- java web + testNG + ant + Selenium整合对IE浏览器的自动化测试二(一个简单的Selenium测试)
- Selenium + ANT +TestNG
- selenium+TestNG+ANT
- Selenium自动测试与原理 笔记
- selenium与webdriver测试弹出窗口
- Firefix与selenium IDE 测试初步
- 4.Selenium与TestNG测试框架结合
- android系统如何获取手机信息
- ios 开发中 动态库 与静态库的区别
- Hadoop架设过程中实现普通用户权限下SSH无密码登录
- 用md5算法检测文件的完整性
- Windows Socket五种I/O模型——代码全攻略
- ant与selenium联合测试
- awk合并行、linux shell 字符串操作、sed字符替换
- PTA是什么?BT-WIFI共存
- 浅谈移动电子商务在民航业的应用优势
- c/c++ 柔性数组 -- 结构体变长的妙用——0个元素的数组 (转载)
- ubuntu下编译Android出现的问题 总结
- tcp三次握手协议
- 【一闪一闪亮晶晶 漫天都是小星星】 poj2352&&hdu2428 杭电数据有问题,注意下!!
- 还使用SVN1.6版本的Android开发者注意了。ADT与SVN的兼容性错误。ADT更新的高发性问题