How to use jenkins, ant, selenium, testng to build an automated testing framework

If in your understanding, automated testing is to introduce the webdriver package in eclipse, and then write some test scripts, this is what you call automated testing. In fact, this is not a real automated test. You have seen every time you need to run Do you still need to open eclipse and then choose to run the file? no! Because that is really low! Let’s talk about the manager’s two automated tests: 1. Write the script, put it on the server, and execute it through a scheduled task. This is for tasks that need to be performed every day or some time period, but it is used less. It used to be A set of systems used to regularly check all interface development on the line.

Entering the text, let me talk about our thoughts first:

1. Use eclipse plus webdriver to write our script.

2. It is impossible for all scripts to be placed in the same folder. Some need to be executed multiple times, but some only need to be executed once. We implement this through testng, which can be easily implemented in our testng framework, so we will Added testng to the script.

3. Although testng can plan the script well, it cannot run automatically. We have to open and run it every time, which is troublesome, so we thought of jenkins, which we often use to build projects, so we need to run it now Introduce jenkins on the framework.

4. After the introduction, how to combine them will be explained in detail below

1. Required tools and packages

1. In eclipse, all environment variables must be configured. the

2. The jar package of selenium

3. The jar package of testng

2. Project and directory structure

1. Use eclipse to create a project, then create a package under src, and finally create a testng class below, or an ordinary class with a main function (all in one step, use testng directly)

2. Create a folder lib under the same directory of the project, create a selenium folder under the lib folder, and then create a libs folder under the selenium folder

3. In the same directory of src in eclipse, create a testng.xml file and a build.xml file

The directory structure is as follows:

3. Introduction of jar package

First of all, the above is done, we don’t care, now we need to introduce the selenium package and testng package into the project, otherwise it will not work, first put the downloaded jar packages into the previously created folders one by one , and import it in the build path after putting it in. Remember that the jar package of testng was introduced in the project.

4. Writing test classes and writing testng.xml

1. Write a simple selenium program for the test class created under the package, as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.mushishi;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class NewTest {

@Test

public void f() {

WebDriver driver;

System.setProperty("webdriver.firefox.bin","E:\ \\Firefox\firefox.exe");

driver = new FirefoxDriver();

driver.get("http://www.baidu.com");

}

}

You can debug the program step by step first, right?

2. What is actually placed in the testng.xml file is an execution task. The simplest one is as follows. If you want to add logic execution, you can study it later, and it is all supported. Paste the testng code directly

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
  <test name="Test">
    <classes>
    <!-- The following name is your own package name and then your class name -->
      <class name="com.mushishi.NewTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

The format in testng is fixed, you only need to replace your own package name and class name. After finishing these, you execute this testng to see if the above test script can be executed normally. If not, you need to find out the reason.

5. Writing of build.xml file

build.xml is actually to control testng.xml to execute the script, here we directly paste my xml code.

<?xml version="1.0" encoding="UTF-8"?>
<project name="Demo" default="run" basedir=".">
    <echo message="import libs" />
    <path id="run. classpath">
        <fileset dir="${basedir}">
            <include name="lib/testng.jar" />
            <include name="lib/sikuli-script.jar" />
        </fileset>
        <fileset dir="${basedir}/lib/selenium">
            <include name="selenium-java-2.46.0.jar" />
            <include name="libs/*.jar" />
        </fileset>
    </path>
    <taskdef name="testng" classname="org.testng.TestNGantTask" classpathref="run.classpath" />
    <target name="clean">
        <delete dir="build"/>
    </target>
    <target name="compile" depends="clean">
        <echo message="mkdir"/>
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes" debug="on" encoding="UTF-8" includeAntRuntime="false">
            <classpath refid="run.classpath"/>
        </javac>
    </target>
    <path id="runpath">
         <path refid="run.classpath"/>
         <pathelement location="build/classes"/>
       </path>
    <target name="run" depends="compile">
        <testng classpathref="runpath" outputDir="test-output">
            <xmlfileset dir="${basedir}" includes="testng.xml"/>
            <jvmarg value="-ea" />
        </testng>
    </target>
</project>

The above xml code does not need to be changed, it can be used directly, if someone’s code “

<taskdef name="testng" classname="org.testng.TestNGantTask" classpathref="run.classpath" />” This line will report an error, saying it is useless or something, I have encountered this Sometimes it is because there is a problem with the package in my build path, re-import it, and then import the required package according to the above file path.

Up to now, most of the environment is good. You can switch to the directory where the project is located in the cmd command mode, and run the ant command directly to see if an error is reported. If not, the browser will be launched directly. 

6. Configuration of jenkins

1. Just install Baidu, just one command

2. After completion, select Create Task on the task creation page -> enter the name of the project -> select “Build a free-style software project” -> “Build” -> enter the following picture information in the build. The build file is the directory of your project. If it is a basic service, leave it alone and save it.

3. Now it can be run directly.