之前一直都是用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按钮即可,你的第一个插件工程就创建完成了,如下图:
如何新建一个Action?鼠标选中"com.example.myfirstplugin",右键 - New - Plugin Devkit - Action,如下图所示:
然后会弹出新建Action的窗口,填写好相关信息,点击Ok即可,如下图:
创建成功后,会自动将Action注册到plugin.xml文件中,如下图:
到此,你的TestCaseConfigAction入口文件就创建成功了,开始正式进入coding环节吧!!!
TestCaseConfigUI.java的代码实现:
package com.example.myfirstplugin.testcaseconfigui; import com.intellij.openapi.project.Project; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.tree.DefaultTreeModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.ArrayList; public class TestCaseConfigUI { private CheckBoxTreeNode rootNode; private ArrayListexistCasesList; private int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width; private int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; private int frameWidth = screenWidth/2; private int frameHeight = screenHeight/2; public void showUI(Project project){ JFrame frame =new JFrame("TestCase Config"); // Toolkit toolkit=Toolkit.getDefaultToolkit(); // 获取Toolkit对象 // Image icon = toolkit.getImage("D:\\shao\\android.png"); // 获取图片对象 // frame.setIconImage(icon); // set the window show in the middle frame.setBounds((screenWidth-frameWidth)/2,(screenHeight-frameHeight)/2,frameWidth,frameHeight); 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); JTree 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); } addFrameFooter(frame, panel, project); }else{ JLabel label = new JLabel("Has No Any Testcases, Please Create."); label.setAlignmentX(Component.CENTER_ALIGNMENT); label.setBorder(new EmptyBorder(frameHeight/2-80, 0, frameHeight/2, 0)); panel.add(label); } frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); frame.setVisible(true); frame.setResizable(false); } private void readExistCases(Project project){ /* function :read existed testcases from project.txt, for set status of these testcases is selected. params project :project object, can get project path from it. */ 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){ /* function :Add scripts to CheckBoxTreeNode by recursion */ 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){ /* function :add the footer part of the frame params jFrame :the main window of frame params jPanel :the footer's parent widget params project :for get project information */ 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){ /* function :write selected testcases to config file when click the button of save. params node :tree node object params fileWriter :the object of writing testcases */ if(!node.isLeaf()){ for (int j=0; j 五、插件发布(本地发布)
将插件工程打包,点击菜单Run - Run - Run Plugin,工程会自动运行编译,编译成功后会在工程的/build/libs路径下生成一个.jar文件,如下图:
pycharm安装使用此插件,点击File - Settings - Plugins,弹出插件安装窗口,然后按下图提示操作即可:
最后来看一下效果:
六、工程代码
Gitee下载链接:https://gitee.com/chshao/scriptplugin