如何在Swings应用程序中实现Eclipse的'Terminate'按钮功能

时间:2016-12-20 07:15:18

标签: java eclipse swing selenium

假设我们有一个Java Swings应用程序,它已用于使用Maven项目运行Selenium WebDriver测试

Swings应用程序有3个组件:

  1. '运行测试'按钮:通过调用包含'mvn test site'命令的Windows批处理文件开始执行测试
  2. '停止测试'按钮:我需要帮助才能实现此按钮的功能
  3. 文本区域:显示通过“运行测试”按钮
  4. 调用的批处理文件的控制台输出

    我的方案是通过单击“运行测试”按钮开始执行Selenium WebDriver测试,其输出将显示在应用程序附带的文本区域中。现在,假设在执行测试之间,我想点击“停止测试”按钮,测试执行应该停止,就像'终止'按钮在Eclipse上工作一样。

    以下是我正在使用的代码

    startExecutionButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {  
    
                            try {
    
                            //startExecutionButton.setEnabled(false);
                            startExecutionButton.setText("Tests Running...");
                            startExecutionButton.setBackground(Color.GRAY);
                            String applicationURL = applicationUrlTextField.getText().trim();
                            String username = usernameTextField.getText().trim();
                            String password = passwordTextField.getText().trim();
                            String spreadsheetPath = spradsheetLocationTextField.getText().trim();
                            String testsType= testsTypeComboBox.getSelectedItem().toString();
                            String country = countryComboBox.getSelectedItem().toString();
    
                            // Create and overwrite properties file
                            List<String> lines = (List) Arrays.asList("applicationURL="+applicationURL, "username="+username,"password="+password,"sheetName="+testsType+"-"+country,"searchString=GHS_CLASS_"+country,"maxWaitTimeToPOLLElement=5","maxWaitTimeToFindElement=40","host=localhost","browserName=firefox","Time_Limit_To_Generate_Chemical=15");
                            Path configPath = Paths.get(getFileLocation("config.properties"));
                            try {
                                if(!configPath.toString().equals("")){
                                    Files.deleteIfExists(configPath);
                                }       
                                Files.write(configPath, lines, Charset.forName("UTF-8"));
    
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
    
                            // Overwrite the spreadsheet
                            Path spreadsheet = Paths.get(getFileLocation("GenerateChemicalTest.xlsx"));
                            try {
                                if(!spreadsheet.toString().equals("")){
                                Files.deleteIfExists(spreadsheet);
                                }
                                Files.copy(new File(spreadsheetPath).toPath(), spreadsheet);                
    
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            printLog();
                    } catch (HeadlessException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
    
    
            }
    
           });
    
    
            private void printLog() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
    
                // Open command prompt and start tests execution
            //  Process p;
                try {   
                    Runtime rt = Runtime.getRuntime();
                       Process pr = rt.exec("C:\\mavenTest.bat");
    
                         BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    
                         String line=null;
                         //System.out.println(probuilder.environment().toString());
                      while ((line = input.readLine()) != null) {
                        System.out.println(line);
                      }
                      input.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                } 
            }
        });
        thread.start();
    }
    

    Click here for 'Swings application UI'

    mavenTest.bat代码

    cd C:\Hazox\AutoTestHazox
    C:
    mvn test site
    

0 个答案:

没有答案