Jenkins, ant, selenium, testng build 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 true automated test. You have seen that it needs to be run every time Do I still need to open eclipse and select the file to run? no! Because that is really low! Let’s talk about two types of automated tests for managers: 1. Write the script, put it on the server, and execute it through scheduled tasks. This is for tasks that need to be executed every day or during certain periods of time, but it is rarely used. It used to be A system developed to regularly check all online interfaces. Second, this is what this article is about.

Entering the text, let’s first talk about our thoughts:

1. Use eclipse plus webdriver to write our script.

2. All scripts cannot 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. This can be easily achieved in our testng framework, so we Add testng to the script.

3. Although testng can plan the script very 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 do it now Introducing jenkins on the framework.

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

1. Required tools and packages

1. All environment variables must be configured in eclipse.?

2. selenium jar package

3. 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 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, let’s not worry about it for now. Now we need to introduce the selenium package and testng package into the project, otherwise it will not be used. First, put the downloaded jar packages into the previously created folders in sequence. , put it in and introduce it in the build path. Remember that I introduced the testng jar package into the project.

4. Writing test classes and writing testng.xml

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

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 logical execution, you can study it later. It is supported. Paste the testng code directly

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
  <test name="Test">
    <classes>
    <!-- The name below 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 you have done this, execute this testng to see if you can execute the above test script normally. If not, you need to find out the reason.

5. Preparation of build.xml file

Build.xml actually controls 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 and can be used directly. If someone else’s code “

” This line will report an error, saying it is useless or something. When I encountered this, it was because of the error in my build path. If there is a problem with the package, re-introduce it, and then import the required package according to the above file path.

At present, the environment is mostly 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 no error is reported, the browser will be started directly.
6. Jenkins configuration

1. Just install Baidu, just one command

2. After completion, select Create Task on the Create Task page->Enter the name of the project->When selecting, 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 everything else alone and save it.

3. Let’s run in this good year.

Summary:

Thank you to everyone who reads my article carefully! ! !

As someone who has experienced it, I also hope that everyone will avoid some detours. If you don’t want to experience the feeling of not being able to find information, no one to answer questions, and giving up after a few days of study, here I will share with you some learning about automated testing. Resources, I hope they can help you on your way forward.

How to obtain video documents:
This document and video material should be the most comprehensive and complete preparation warehouse for friends who want to engage in [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you! All of the above can be shared, click below to join the group and you can receive it yourself.