自动化工具 测试平台从搭建到放弃 (一)

tttttttttggggg · 2017年12月11日 · 最后由 Mr_Peace 回复于 2018年08月26日 · 1537 次阅读

前言

工具主要使用了 spring boot(后台)+thymeleaf(模板引擎)+bootstrap(前端框架),使用的都是轻量级框架,
做这个工具的目的主要是为了方便自己使用,也会部署在公司内网服务上,
另外也希望通过记录的方式加深自己的记忆和对项目的理解。
第一篇先介绍设备管理

主体框架

pom

建好项目结构弄 pom.xml , 加入 thymeleaf,加入数据库方面的依赖,所需依赖很少

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tool</groupId>
    <artifactId>plaform</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>plaform</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <!--以下两项需要如果不配置,解析themleaft 会有问题-->
        <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 热部署(代码有更改之后,重新启动应用) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

在 src /main/resource 建立 application.properties 文件

server.port=9002
spring.mvc.static-path-pattern=/static/**

mongodb.thirdparty.uri=mongodb://test:123456@121.201.8.120:27014
mongodb.thirdparty.dbName=tt

templates 中新建 html 页面

<!DOCTYPE HTML>
<!-- thymeleaf 导入 -->
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>测试平台</title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="../static/css/mystyle.css"/>
</head>
<body style="margin-left: 50px">


<div class="navdiv">
    <nav class="navbar navbar-default navbar-fixed-top head-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand btn-lg" href="#"><span class="glyphicon glyphicon-home"></span>首页</a>
            </div>

            <ul class="nav navbar-nav navbar-right">
                <li class="active"><a href="#"><span class="glyphicon glyphicon-lock"></span>设备列表</a></li>
                <li><a><span class="glyphicon glyphicon-th-list"></span>测试平台</a></li>
                <li><a><span class="glyphicon glyphicon-asterisk"></span>Mock平台</a></li>
                <li><a><span class="glyphicon glyphicon-road"></span>接口测试平台</a></li>
                <li><a><span class="glyphicon glyphicon-cog"></span>注册</a></li>
            </ul>
        </div>
    </nav>
</div>


<div class="search ">
    <form class="form-inline" th:action="@{/search}" method="post" th:object="${testPlaform}">
        <div class="form-group">
            <label class="sr-only" for="exampleInputAmount">Iphone8</label>
            <div class="input-group">
                <div class="input-group-addon">手机型号</div>
                <input type="text" class="form-control" id="exampleInputAmount" placeholder="" th:field="*{mobile}">

            </div>
        </div>
        <button type="submit" class="btn btn-primary">查询</button>
    </form>
</div>

<div class="data table-responsive">
    <table class="table table-bordered table table-hover">
        <thead>
        <tr>
            <th class="col-1">
                编号
            </th>
            <th class="col-2">
                厂家
            </th>
            <th class="col-2">
                型号
            </th>
            <th class="col-2">
                UUID
            </th>
            <th class="col-2">
                保管人
            </th>

            <th class="col-3">
                备注
            </th>
        </tr>
        </thead>

        <tbody>
        <!--dddd-->
        <tr th:each="mobile: ${mobileInfoList}">
            <td>1</td>
            <td th:text="${mobile.plaform}">mobile</td>
            <td th:text="${mobile.mobile}">iphone 7</td>
            <td th:text="${mobile.uuid}">TYKHJKHJK</td>
            <td th:text="${mobile.owner}">xiaoming</td>
            <td th:text="${mobile.remark}">未借出</td>
        </tr>
        </tbody>
    </table>
</div>

<script src="../static/js/jquery.min.js.min.js"></script>
<script src="../static/js/bootstrap.min.js"></script>

</body>
</html>

编写数据库查询 dao

package com.tool.plaform.dao;

import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCursor;
import com.tool.plaform.domain.TestPlaform;
import com.tool.plaform.utils.MongoDbUtils;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

public class TestMobileDao {

    public List<TestPlaform> findAllTestMobile(){
        BasicDBObject query=new BasicDBObject();
        MongoCursor<Document> cursor=MongoDbUtils.find("TestMobile",query);
        List<TestPlaform>  list=parseTestPlaform(cursor);
        return list;
    }

    public List<TestPlaform> findTestMobileByPlaform(String plaform){

        BasicDBObject query=new BasicDBObject();
        query.append("型号",plaform);
        MongoCursor<Document> cursor=MongoDbUtils.find("TestMobile",query);
        System.out.println(cursor);
        List<TestPlaform> list=parseTestPlaform(cursor);
        return list;
    }

    public List<TestPlaform> parseTestPlaform(MongoCursor<Document> cursor){
        List<TestPlaform> list=new ArrayList<>();
        try {
            while (cursor!=null && cursor.hasNext()) {
                Document doc = cursor.next();
                if(doc.containsKey("_id")){
                    TestPlaform aa=setTestMobileObj(doc);
                    list.add(aa);
                }
            }
        } catch (Exception e) {

        } finally {
            if (cursor!=null)
                cursor.close();
        }
        return  list;
    }


    public TestPlaform setTestMobileObj(Document doc ){
        TestPlaform testPlaform=new TestPlaform();
        String mobile=doc.getString("型号");
        String plaform=doc.getString("厂家");
        String uuid=doc.getString("uuid");
        String owner=doc.getString("保管人");
        String remark=doc.getString("备注");
        testPlaform.setMobile(mobile);
        testPlaform.setOwner(owner);
        testPlaform.setPlaform(plaform);
        testPlaform.setRemark(remark);
        testPlaform.setUuid(uuid);
        return  testPlaform;
    }

}

编写接口 controller

package com.tool.plaform.controller;

import com.tool.plaform.dao.TestMobileDao;
import com.tool.plaform.domain.TestPlaform;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class PlaformController {

    TestMobileDao dao = new TestMobileDao();

    @RequestMapping("/")
    public String index(Model model) {
        List<TestPlaform> testPlaforms = dao.findAllTestMobile();
        model.addAttribute("mobileInfoList", testPlaforms);
        return "index";
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String searchByPlaform(TestPlaform testPlaform, Model model) {
        List<TestPlaform> testPlaforms = dao.findTestMobileByPlaform(testPlaform.getPlaform());
        model.addAttribute("mobileInfoList", testPlaforms);
        return "index";
    }

    @ModelAttribute
    TestPlaform setTestPlaform() {
        return new TestPlaform();
    }

}

发布

发布测试访问 http://localhost:9002/,大功告成
接口和 mock 测试的前端页面打算使用 testerhome 上几个大神已经写好的页面,后台自己从新写一遍,
下一篇介绍具体过程

共收到 16 条回复 时间 点赞

github 代码位置是哪里啊?

bauul 回复

😅

bauul 回复

更新了

CC 回复

😊

tttttttttggggg 回复

点赞三连👍 👍 👍

想看后面的 ‘放弃’

同类的框架太多,只能当做练手

可以慢慢丰富,我也是一直致力于做一些业务工具,满足日常测试,方便快捷,提高效率

Jerry li 回复

哈哈!等着,

无为 回复

是的,要弄点平台上没有的东西分享一下,等我下一篇文章,基于 powermock 的单元测试

悠然 回复

高见

仅楼主可见
仅楼主可见

发现缺少 com.tool.plaform.dao.UserMapper;

liqi 回复

UserMapper 是 mybatis 的映射类,mybatis 要求必须放在 resources 这个文件夹下面,你在这个文件夹找一下

可否 share 一下源码地址哈,观摩学习下😀

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册