Java JTable获取Xml文件中测试数据并实现监听

在单独执行android相关测试脚本时,往往需要通过ide运行,或者执行adb命令,对此可以编写相关的测试工具提高效率。对此,我用java控件展示xml的测试数据,大致思路如下:
1)自定义xml文件数据,填写测试用例数据,包含测试用例id,测试用例名,测试执行的设备名
2)xml解析获取数据,并将数据放入Jtable
3)将Jtable放入JScrollPane中,显示下拉避免数据显示不下

效果图如下:


Java JTable获取Xml文件中测试数据并实现监听_第1张图片
image.png

以下是详细代码:
1.新建Xml文件,将xml文件放到src路径下,内容如下



 
 111
 aaa 
 
 
 
 222
 bbb
 
 
 
 333
 ccc
 
 
 
 444
 ddd
 
 
 
 555
 eee
 
 
 
 666
 fff
 
 
 
 777
 ggg
 


2.xml解析类代码如下:

package TestCase;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
public class ReadXmlFile {
    Listcase_list;
    
    //读取xml文件中的测试数据,返回list集合
  
 public List  XmlToList_id() throws Exception {
        case_list=new ArrayList<>();
    
         File xmlFile = new File("src/Testcase.xml");
           
         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
   
         DocumentBuilder builder = builderFactory.newDocumentBuilder();
   
         Document doc = builder.parse(xmlFile);
   
         doc.getDocumentElement().normalize();
   
         System.out.println("Root element: "+doc.getDocumentElement().getNodeName());
   
         NodeList nList = doc.getElementsByTagName("case");
   
         for(int i = 0 ; i();
    
         File xmlFile = new File("src/Testcase.xml");
           
         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

         DocumentBuilder builder = builderFactory.newDocumentBuilder();

         Document doc = builder.parse(xmlFile);

         doc.getDocumentElement().normalize();

         System.out.println("Root element: "+doc.getDocumentElement().getNodeName());

         NodeList nList = doc.getElementsByTagName("case");

         for(int i = 0 ; i();
    
         File xmlFile = new File("src/Testcase.xml");
           
         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

         DocumentBuilder builder = builderFactory.newDocumentBuilder();

         Document doc = builder.parse(xmlFile);

         doc.getDocumentElement().normalize();

         System.out.println("Root element: "+doc.getDocumentElement().getNodeName());

         NodeList nList = doc.getElementsByTagName("case");

         for(int i = 0 ; i

3.Jpanal代码如下:

package TestCase;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Vector;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableModel;



public class JpanelTestCase extends JPanel {
    //[]columnNames={"用例编号","用例名","用例步骤"};
    //String[][]tablesValues={{"001","222","333"},{"002","555","666"},{"003","888","999"},{"004","555","666"},{"005","888","999"},{"006","555","666"},{"007","888","999"},{"008","555","666"},{"009","888","999"},{"010","555","666"},{"011","888","999"},{"012","555","666"},{"013","888","999"},{"014","555","666"},{"015","888","999"},{"016","555","666"},{"017","888","999"},{"018","555","666"},{"019","888","999"},{"020","555","666"},{"021","888","999"},{"022","555","666"},{"023","888","999"},{"024","555","666"},{"025","888","999"},{"026","555","666"},{"027","888","999"},{"028","555","666"},{"029","888","999"},{"030","555","666"},{"031","888","999"}};
    JTable table;
    JScrollPane jScrollPane;
    DefaultTableModel tableModel;
    JOptionPane optionPane;
    Vectorvector=new Vector<>();
    
    JButton flush_case;
    
    public JpanelTestCase(){
        setBorder(BorderFactory.createTitledBorder("分组框")); //设置面板边框,实现分组框的效果,此句代码为关键代码

        setBorder(BorderFactory.createLineBorder(Color.WHITE));
        
        setBounds(0,0, 520,400);
        setLayout(null);
    
        try {
            initView();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        addView();
        clickListenser();
    
    }

    private void initView() throws Exception {
        //tableModel=new DefaultTableModel(tablesValues,columnNames);
        // TODO Auto-generated method stub
        removeAll();
        setVisible(true);
        
        flush_case=new JButton("刷新用例");
        flush_case.setBounds(200,145, 100, 30);
        
        
        

        
        //tableModel.setColumnIdentifiers(new String[]{"用例Id","用例名称","用例设备"});
        ReadXmlFile readXmlFile=new ReadXmlFile();
        List list_id=readXmlFile.XmlToList_id();
        List list_casename=readXmlFile.XmlToList_casename();
        List list_devicesid=readXmlFile.XmlToList_devicesid();

        
        System.out.println(list_id);
        int list_size=list_id.size();
        //tableModel=new DefaultTableModel(new String[]{"用例Id","用例名称","用例设备"},list_size);
        tableModel=new DefaultTableModel();
        tableModel.setColumnIdentifiers(new String[]{"用例id","用例名称","用例设备"});
        
        //int index;

        System.out.println("list长度:"+list_size);
        
        
        
        for(int i=0;i

4.主类代码如下:

package TestCase;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.WindowConstants;



public class TestMain extends JFrame {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestMain();

    }
    
    public TestMain(){
        setTitle("测试");
        setLayout(null);
        
        //声明整个Jframe窗体大小
        setBounds(0,0, 800,500);
        JpanelTestCase jpanelDevices=new JpanelTestCase();
        
        Container container=getContentPane();
        
        //将Jpanal面板添加到Jframe容器中
        container.add(jpanelDevices);
        
        //设置布局未默认布局
        setLayout(null);
        
        //设置
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

}

你可能感兴趣的:(Java JTable获取Xml文件中测试数据并实现监听)