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);

selenium.start();

}

protected void tearDown() throws Exception {

selenium.stop();

}

public void testNew() throws Exception {

selenium.setTimeout("100000");

selenium.open("/login.action");

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

selenium.type("password", "123");

selenium.click("//input[@value='Log In']");

selenium.waitForPageToLoad("100000");

Thread.sleep(10000);

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

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

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

Thread.sleep(1000);

}

// omit lines

...

selenium.open("/main.action");

}

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}" />

</target>

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

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

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

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

<!-- local project jars -->

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

<include name="*.jar" />

</patternset>

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

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

</fileset>

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

<!-- compile -->

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

</javac>

</target>

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

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

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

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

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

<classpath>

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

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

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

</fileset>

</classpath>

<formatter type="xml" />

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

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

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

</fileset>

</batchtest>

</junit>

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

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

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

</fileset>

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

</junitreport>

<fail if="tests.failed">

</fail>

</target>

</project>

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