Suppose we have a Java Swings application, which has been used to run Selenium WebDriver tests using Maven project
The Swings application has 3 components:
- 'Run Tests' button : To start the execution of tests by calling a Windows batch file containing 'mvn test site' command
- 'Stop Tests' button : I need help to implement the functionality of this button
- Text Area: To show the console output of a batch file called through 'Run Tests' button
My scenario is that I start the execution of Selenium WebDriver tests by clicking 'Run Tests' button and its output will be displayed in the Text area attached to the application. Now, suppose in-between the execution of tests I want to click on 'Stop Tests' button and tests execution should stop, just like 'Terminate' button works on Eclipse.
Here is the code I am using
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 code
cd C:\Hazox\AutoTestHazox
C:
mvn test site