Skip to content

Commit 1f28f26

Browse files
committed
feat: added verification when selecting new driver
1 parent 4e06868 commit 1f28f26

File tree

3 files changed

+83
-21
lines changed

3 files changed

+83
-21
lines changed

tool/.idea/inspectionProfiles/Project_Default.xml

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tool/src/main/java/migt/ExecuteTrack.java

+35-17
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,20 @@ public ExecuteTrack(boolean isHeadless,
5454
}
5555

5656
/**
57-
* Registers the execute track listener, used to communicate with the ExecuteTrack thread
57+
* Init the driver for the selected browser.
5858
*
59-
* @param listener the listener
59+
* @param chrome_selected True to select Chrome or False to select Firefox
60+
* @param port the port to be used
61+
* @param driver_path the absolute path to the driver executable to be used
62+
* @return the driver object
6063
*/
61-
public void registerExecuteTrackListener(ExecuteTrackListener listener) {
62-
this.listener = listener;
63-
}
64-
65-
/**
66-
* Runs the session track
67-
*/
68-
@Override
69-
public void run() {
70-
WebDriver driver;
71-
int TIMEOUT = 10;
64+
public static WebDriver init_driver(
65+
boolean chrome_selected,
66+
String port,
67+
String driver_path,
68+
boolean headless) {
7269

70+
WebDriver driver = null;
7371
if (chrome_selected) {
7472
ChromeOptions options = new ChromeOptions();
7573
options.addArguments("ignore-certificate-errors");
@@ -80,14 +78,13 @@ public void run() {
8078
proxy.setHttpProxy("localhost:" + port);
8179
proxy.setSslProxy("localhost:" + port);
8280
options.setCapability(CapabilityType.PROXY, proxy);
83-
//options.setHeadless(isHeadless);
81+
if (headless) options.addArguments("--headless");
8482

8583
System.setProperty("webdriver.chrome.driver", driver_path);
8684
try {
8785
driver = new ChromeDriver(options);
8886
} catch (SessionNotCreatedException e) {
8987
e.printStackTrace();
90-
return;
9188
}
9289

9390
} else {
@@ -98,17 +95,38 @@ public void run() {
9895
proxy.setHttpProxy("localhost:" + port);
9996
proxy.setSslProxy("localhost:" + port);
10097
options.setCapability(CapabilityType.PROXY, proxy);
101-
//options.setHeadless(isHeadless);
98+
if (headless) options.addArguments("--headless");
10299

103100
System.setProperty("webdriver.gecko.driver", driver_path);
104101
try {
105102
driver = new FirefoxDriver(options);
106103
} catch (SessionNotCreatedException e) {
107104
e.printStackTrace();
108-
return;
109105
}
110106
}
111107

108+
return driver;
109+
}
110+
111+
/**
112+
* Registers the execute track listener, used to communicate with the ExecuteTrack thread
113+
*
114+
* @param listener the listener
115+
*/
116+
public void registerExecuteTrackListener(ExecuteTrackListener listener) {
117+
this.listener = listener;
118+
}
119+
120+
/**
121+
* Runs the session track
122+
*/
123+
@Override
124+
public void run() {
125+
WebDriver driver;
126+
int TIMEOUT = 10;
127+
128+
driver = init_driver(chrome_selected, port, driver_path, false);
129+
112130
WebElement currentElement = null;
113131
int act_window_index = 0;
114132

tool/src/main/java/migt/Main.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.json.JSONArray;
99
import org.json.JSONException;
1010
import org.json.JSONObject;
11+
import org.openqa.selenium.WebDriver;
1112

1213
import javax.swing.*;
1314
import javax.swing.table.DefaultTableCellRenderer;
@@ -215,6 +216,24 @@ public void init() {
215216
txt_err_debug_tab = new JTextArea();
216217
}
217218

219+
public boolean is_driver_path_valid() {
220+
WebDriver driver = ExecuteTrack.init_driver(
221+
!btnselectChrome.isEnabled(),
222+
Integer.toString(DEFAULT_PORT),
223+
DRIVER_PATH,
224+
true);
225+
226+
boolean res = driver != null;
227+
if (!res) {
228+
System.out.println("The selenium driver executable is not working");
229+
} else {
230+
driver.quit();
231+
}
232+
233+
driver = null; // Free up
234+
return res;
235+
}
236+
218237
/**
219238
* Set a redirect of the stdout and stderr to the txtboxes in the debug tab of the GUI
220239
*/
@@ -869,10 +888,14 @@ private void setup_tab_butons() {
869888
if (returnVal == JFileChooser.APPROVE_OPTION) {
870889
File file = driverSelector.getSelectedFile();
871890
DRIVER_PATH = file.getPath();
872-
editConfigFile("last_driver_path", DRIVER_PATH);
873-
lbldriver.setText("Driver Selected");
874-
btndriverSelector.setBackground(Color.GREEN);
875-
btnTestTrack.setEnabled(true);
891+
if (is_driver_path_valid()) {
892+
editConfigFile("last_driver_path", DRIVER_PATH);
893+
lbldriver.setText("Driver Selected");
894+
btndriverSelector.setBackground(Color.GREEN);
895+
btnTestTrack.setEnabled(true);
896+
} else {
897+
lbldriver.setText("Driver:selected executable seems to not work, check logs");
898+
}
876899
} else if ((returnVal == JFileChooser.ERROR) || (returnVal == JFileChooser.ERROR_OPTION)) {
877900
lbldriver.setText("Driver:error during file selection");
878901
System.out.println("error during file selection");

0 commit comments

Comments
 (0)