ant与selenium联合测试

来源:互联网 发布:南昌九九网络 编辑:程序博客网 时间:2024/06/05 20:27

摘自:http://luyongxin88.blog.163.com/blog/static/925580720112265955438/

介绍seleniumant联合起来开展测试其中还利用junit生产测试报告

(本文建设你对selenium-RC以及ant以及junit有了一定了认识和了解)

1>ant结合selenium启动测试的自动过程如下:

利用selnium IDE工具创建测试脚本(java
修改脚本,准备文件结构

2> 本次实例的目录结构:


 

 首先在c盘下创建ABQ-1文件夹,在此文件夹下,有

build(其中创建classlib文件夹)

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文件夹(这两个文件夹分别是junitselenium-RC的东西)

7> 运行 ant

  首先启动selenium-RC java -jar selenium-server.jar

  再运行antant

 查看运行报告

注意图片中的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,也就是实现了测试用例的多个执行,再看看测试报告

 

 

 

 

 

原创粉丝点击