在单独执行android相关测试脚本时,往往需要通过ide运行,或者执行adb命令,对此可以编写相关的测试工具提高效率。对此,我用java控件展示xml的测试数据,大致思路如下:
1)自定义xml文件数据,填写测试用例数据,包含测试用例id,测试用例名,测试执行的设备名
2)xml解析获取数据,并将数据放入Jtable
3)将Jtable放入JScrollPane中,显示下拉避免数据显示不下
效果图如下:
image.png
以下是详细代码:
1.新建Xml文件,将xml文件放到src路径下,内容如下
<?xml version="1.0" encoding="ISO-8859-1"?>
<testcase>
<case id="001">
<name>111</name>
<devicesId>aaa</devicesId>
</case>
<case id="002">
<name>222</name>
<devicesId>bbb</devicesId>
</case>
<case id="003">
<name>333</name>
<devicesId>ccc</devicesId>
</case>
<case id="004">
<name >444</name>
<devicesId>ddd</devicesId>
</case>
<case id="005">
<name >555</name>
<devicesId>eee</devicesId>
</case>
<case id="006">
<name >666</name>
<devicesId>fff</devicesId>
</case>
<case id="007">
<name >777</name>
<devicesId>ggg</devicesId>
</case>
</testcase>
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 {
List<String>case_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<nList.getLength();i++){
Node node = nList.item(i);
System.out.println("Node name: "+ node.getNodeName());
Element ele = (Element)node;
// System.out.println("----------------------------");
if(node.getNodeType() == Element.ELEMENT_NODE){
// System.out.println("case id: "+ ele.getAttribute("id"));
case_list.add(ele.getAttribute("id"));
//System.out.println("case name: "+ ele.getElementsByTagName("name").item(0).getTextContent());
// case_list.add(ele.getElementsByTagName("name").item(0).getTextContent());
// System.out.println("devicesId: "+ele.getElementsByTagName("devicesId").item(0).getTextContent());
// case_list.add(ele.getElementsByTagName("devicesId").item(0).getTextContent());
// System.out.println("-------------------------");
}
}
return case_list;
}
public List XmlToList_casename() 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<nList.getLength();i++){
Node node = nList.item(i);
System.out.println("Node name: "+ node.getNodeName());
Element ele = (Element)node;
// System.out.println("----------------------------");
if(node.getNodeType() == Element.ELEMENT_NODE){
// System.out.println("case id: "+ ele.getAttribute("id"));
//case_list.add(ele.getAttribute("id"));
//System.out.println("case name: "+ ele.getElementsByTagName("name").item(0).getTextContent());
case_list.add(ele.getElementsByTagName("name").item(0).getTextContent());
// System.out.println("devicesId: "+ele.getElementsByTagName("devicesId").item(0).getTextContent());
// case_list.add(ele.getElementsByTagName("devicesId").item(0).getTextContent());
// System.out.println("-------------------------");
}
}
return case_list;
}
public List XmlToList_devicesid() 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<nList.getLength();i++){
Node node = nList.item(i);
System.out.println("Node name: "+ node.getNodeName());
Element ele = (Element)node;
// System.out.println("----------------------------");
if(node.getNodeType() == Element.ELEMENT_NODE){
// System.out.println("case id: "+ ele.getAttribute("id"));
//case_list.add(ele.getAttribute("id"));
//System.out.println("case name: "+ ele.getElementsByTagName("name").item(0).getTextContent());
// case_list.add(ele.getElementsByTagName("name").item(0).getTextContent());
// System.out.println("devicesId: "+ele.getElementsByTagName("devicesId").item(0).getTextContent());
case_list.add(ele.getElementsByTagName("devicesId").item(0).getTextContent());
// System.out.println("-------------------------");
}
}
return case_list;
}
}
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;
Vector<String>vector=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<list_size;i++){
String id=null;
String name=null;
String devicesId=null;
id=(String) list_id.get(i);
//vector.add(id);
name=(String)list_casename.get(i);
devicesId=(String)list_devicesid.get(i);
tableModel.addRow(new Object[]{id,name,devicesId});
}
table=new JTable(tableModel);
jScrollPane=new JScrollPane(table);
jScrollPane.setBounds(10,10,500,100);
}
private void addView() {
// TODO Auto-generated method stub
add(jScrollPane);
add(flush_case);
}
private void clickListenser() {
//点击表格条目的监听事件
// TODO Auto-generated method stub
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int r= table.getSelectedRow();
int c= table.getSelectedColumn();
//得到选中的单元格的值,表格中都是字符串
Object value= table.getValueAt(r, 0);
String info="运行用例"+value.toString();
// optionPane.showMessageDialog(null,info);
//弹出提示框
int result = optionPane.showConfirmDialog(null, info,"111", JOptionPane.DEFAULT_OPTION);
if(result==JOptionPane.OK_OPTION){
//点击确定的按钮
System.out.println(info);
}
}
});
//点击刷新按钮执行的方法
flush_case.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new InitTestData().start();
}
});
}
//起一个线程刷新测试用例数据
class InitTestData extends Thread{
JpanelTestCase JpanelTestCase=new JpanelTestCase();
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
initView();
addView();
clickListenser();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
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);
}
}
网友评论