之前一直都是用eclipse做开发工具,最近开始切换pycharm,想把之前针对eclipse开发的一些好用的插件功能搬到pycharm上。先实现自动配置测试用例插件,来提升自动化工程的易用性,减少测试准备工作耗时,解决使用者的痛点诉求。
开发案例先上效果图,让大家有个直观的感受,如下图
一、开发环境准备第一步:下载插件开发工具Inteelij IDEA,下载地址:https://www.jetbrains.com/idea/download/#section=windows,下载社区版即可
第二步:安装运行Inteelij IDEA,并配置JDK,注意这里用的不是我们平时用于java开发的普通JDK,而是openjdk-18,这里不用担心不知道怎么下载,新建工程时点击下载即可,如下图:
二、创建插件工程点击菜单File - New - Project,弹出如下窗口:
选择IDE Plugin,填写插件工程相关信息即可,注意这里JDK选择openjdk-18,所有信息填写完成后点击Create按钮即可,你的第一个插件工程就创建完成了,如下图:
三、TestCaseConfigAction事件入口如何新建一个Action?鼠标选中"com.example.myfirstplugin",右键 - New - Plugin Devkit - Action,如下图所示:
然后会弹出新建Action的窗口,填写好相关信息,点击Ok即可,如下图:
创建成功后,会自动将Action注册到plugin.xml文件中,如下图:
到此,你的TestCaseConfigAction入口文件就创建成功了,开始正式进入coding环节吧!!!
四、Swing开发UI界面TestCaseConfigUI.java的代码实现:
package com.example.myfirstplugin.testcaseconfigui;
import com.intellij.openapi.project.Project;
import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
public class TestCaseConfigUI {
private JFrame frame;
private JTree tree;
private CheckBoxTreeNode rootNode;
private ArrayList existCasesList;
public void showUI(Project project){
frame =new JFrame("TestCase Config");
frame.setBounds(200,200,650,500);
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxLayout);
File file = new File(project.getBasePath(), "scripts");
if(file.exists()){
if(file.listFiles().length>0){
readExistCases(project);
tree =new JTree();
rootNode =new CheckBoxTreeNode("scripts");
listFiles(file, rootNode);
DefaultTreeModel model =new DefaultTreeModel(rootNode);
tree.addMouseListener(new CheckBoxTreeNodeSelectionListener());
tree.setModel(model);
tree.setCellRenderer(new CheckBoxTreeCellRenderer());
JScrollPane scroll =new JScrollPane(tree);
panel.add(scroll);
}
}
frame.getContentPane().add(panel);
addFrameFooter(frame, panel, project);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setVisible(true);
}
private void readExistCases(Project project){
existCasesList = new ArrayList<>();
File configFile = new File(project.getBasePath(),"project.txt");
if(configFile.exists()){
try {
BufferedReader reader = new BufferedReader(new FileReader(configFile));
for(Object msg:reader.lines().toArray()){
String[] msgSplits=msg.toString().split("/");
existCasesList.add(msgSplits[msgSplits.length-1]);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}
public void listFiles(File file, CheckBoxTreeNode node){
if(file.isDirectory()){
File[] _file= file.listFiles();
for(int i=0; i<_file.length; i++){
CheckBoxTreeNode child =new CheckBoxTreeNode(_file[i].getName());
node.add(child);
listFiles(_file[i], child);
}
}else{
if(existCasesList.contains(node.getUserObject().toString())){
node.setSelected(true);
}
}
}
public void addFrameFooter(JFrame jFrame, JPanel jPanel, Project project){
JPanel panelFooter = new JPanel();
panelFooter.setLayout(new BoxLayout(panelFooter, BoxLayout.X_AXIS));
panelFooter.add(new JPanel());
panelFooter.add(new JPanel());
JPanel panel = new JPanel();
JButton btnSave=new JButton(" Save ");
JButton btnCancel=new JButton("Cancel");
btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(rootNode!=null){
try {
File file = new File(project.getBasePath(),"project.txt");
if(!file.exists()){
file.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file, false));
configSelectedTestcase(rootNode, writer);
writer.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}finally {
jFrame.dispose();
}
}
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
jFrame.dispose();
// System.exit(0);
}
});
panel.add(btnSave);
panel.add(btnCancel);
panelFooter.add(panel);
jPanel.add(panelFooter);
}
private void configSelectedTestcase(CheckBoxTreeNode node, BufferedWriter fileWriter){
if(!node.isLeaf()){
for (int j=0; j
五、插件发布(待更新)
六、完成工程代码
暂时还未发布到github,后续会把链接贴出来,谢谢



