Remote automation test of Web application with Selenium/Ant

The Client side mainly starts JUnit's TestCase through an ant build file, then starts the test method in TestCase, connects and activates the server side for automatic test. The code of the Client-side core test unit is as follows:

package com.tail.p2test;

import junit.framework.Test;

import junit.framework.TestCase;

import junit.framework.TestSuite;

import junit.textui.TestRunner;

import com.thoughtworks.selenium.DefaultSelenium;

import com.thoughtworks.selenium.Selenium;

public class DemoTest extends TestCase {

private Selenium selenium;

public void setUp() throws Exception {

String url = "http://localhost:8080/";

selenium = new DefaultSelenium("localhost", 4444, "*chrome", url);



protected void tearDown() throws Exception {



public void testNew() throws Exception {


selenium.type("username", "admin");

selenium.type("password", "123");"//input[@value='Log In']");



for (int second = 0;; second++) {

if (second >= 60) fail("timeout");

try { if (selenium.isElementPresent("signLabel")) break; } catch (Exception e) {}



// omit lines



Of course, the application can run directly in Eclipse, but in order to be more flexible, we consider using ant script to control the operation of the client. One of the advantages of using ant script here is that it can easily and quickly output the test report. In this case, the purpose of outputting the report is the report directory.

If for software testing, interface testing, automation testing, interview experience exchange. If you are interested, you can add software test exchange: 1085991341, and there will be technical exchanges with peers.

ant's Build.xml script is detailed as follows:

<?xml version="1.0"?>

<project name="portal" default="junit" basedir=".">

<property name="source.dir" value="src" />

<property name="build.dir" value="build" />

<property name="lib.dir" value="lib" />

<property name="classes.dir" value="${build.dir}/classes" />

<property name="report.dir" value="report" />

<!-- ================================================================== -->

<!-- C L E A N                                                          -->

<!-- ================================================================== -->

<target name="clean">

<delete dir="${classes.dir}" />

<mkdir dir="${classes.dir}" />

<delete dir="${report.dir}" />

<mkdir dir="${report.dir}" />


<!-- ================================================================== -->

<!-- C O M P I L E                                                      -->

<!-- ================================================================== -->

<target name="compile" depends="clean">

<!-- local project jars -->

<patternset id="lib.includes.compile">

<include name="*.jar" />


<fileset dir="${lib.dir}" id="lib.compile">

<patternset refid="lib.includes.compile" />


<pathconvert targetos="windows" property="libs.compile" refid="lib.compile" />

<!-- compile -->

<javac srcdir="${source.dir}" destdir="${classes.dir}" classpath="${libs.compile}" includes="**/*.java" debug="true">



<!-- ================================================================== -->

<!-- J U N I T                                                          -->

<!-- ================================================================== -->

<target name="junit" depends="compile">

<junit printsummary="on" fork="true" haltonfailure="false" failureproperty="tests.failed" showoutput="true">


<pathelement path="${classes.dir}" />

<fileset dir="${lib.dir}">

<include name="**/*.jar" />



<formatter type="xml" />

<batchtest todir="${report.dir}">

<fileset dir="${classes.dir}">

<include name="**/*Test.*" />




<junitreport todir="${report.dir}">

<fileset dir="${report.dir}">

<include name="TEST-*.xml" />


<report format="frames" todir="${report.dir}" />


<fail if="tests.failed">




Later, you can easily run the entire test by executing a simple ant command in the work directory.

The above content hopes to be helpful to you. Friends who have been helped are welcome to like and comment.

Tags: Programming Selenium Junit xml Eclipse

Posted on Thu, 07 May 2020 10:36:10 -0400 by Dowdy