From 1f28f26c30f711570c6485fe15a5df973eca02ff Mon Sep 17 00:00:00 2001 From: Matteo Bitussi Date: Thu, 10 Oct 2024 12:33:39 +0200 Subject: [PATCH] feat: added verification when selecting new driver --- .../inspectionProfiles/Project_Default.xml | 21 ++++++++ tool/src/main/java/migt/ExecuteTrack.java | 52 +++++++++++++------ tool/src/main/java/migt/Main.java | 31 +++++++++-- 3 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 tool/.idea/inspectionProfiles/Project_Default.xml diff --git a/tool/.idea/inspectionProfiles/Project_Default.xml b/tool/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..9fe6c47 --- /dev/null +++ b/tool/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,21 @@ + + + + \ No newline at end of file diff --git a/tool/src/main/java/migt/ExecuteTrack.java b/tool/src/main/java/migt/ExecuteTrack.java index b6a96ab..1c68189 100644 --- a/tool/src/main/java/migt/ExecuteTrack.java +++ b/tool/src/main/java/migt/ExecuteTrack.java @@ -54,22 +54,20 @@ public ExecuteTrack(boolean isHeadless, } /** - * Registers the execute track listener, used to communicate with the ExecuteTrack thread + * Init the driver for the selected browser. * - * @param listener the listener + * @param chrome_selected True to select Chrome or False to select Firefox + * @param port the port to be used + * @param driver_path the absolute path to the driver executable to be used + * @return the driver object */ - public void registerExecuteTrackListener(ExecuteTrackListener listener) { - this.listener = listener; - } - - /** - * Runs the session track - */ - @Override - public void run() { - WebDriver driver; - int TIMEOUT = 10; + public static WebDriver init_driver( + boolean chrome_selected, + String port, + String driver_path, + boolean headless) { + WebDriver driver = null; if (chrome_selected) { ChromeOptions options = new ChromeOptions(); options.addArguments("ignore-certificate-errors"); @@ -80,14 +78,13 @@ public void run() { proxy.setHttpProxy("localhost:" + port); proxy.setSslProxy("localhost:" + port); options.setCapability(CapabilityType.PROXY, proxy); - //options.setHeadless(isHeadless); + if (headless) options.addArguments("--headless"); System.setProperty("webdriver.chrome.driver", driver_path); try { driver = new ChromeDriver(options); } catch (SessionNotCreatedException e) { e.printStackTrace(); - return; } } else { @@ -98,17 +95,38 @@ public void run() { proxy.setHttpProxy("localhost:" + port); proxy.setSslProxy("localhost:" + port); options.setCapability(CapabilityType.PROXY, proxy); - //options.setHeadless(isHeadless); + if (headless) options.addArguments("--headless"); System.setProperty("webdriver.gecko.driver", driver_path); try { driver = new FirefoxDriver(options); } catch (SessionNotCreatedException e) { e.printStackTrace(); - return; } } + return driver; + } + + /** + * Registers the execute track listener, used to communicate with the ExecuteTrack thread + * + * @param listener the listener + */ + public void registerExecuteTrackListener(ExecuteTrackListener listener) { + this.listener = listener; + } + + /** + * Runs the session track + */ + @Override + public void run() { + WebDriver driver; + int TIMEOUT = 10; + + driver = init_driver(chrome_selected, port, driver_path, false); + WebElement currentElement = null; int act_window_index = 0; diff --git a/tool/src/main/java/migt/Main.java b/tool/src/main/java/migt/Main.java index 82a8d88..1db7f4d 100644 --- a/tool/src/main/java/migt/Main.java +++ b/tool/src/main/java/migt/Main.java @@ -8,6 +8,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.openqa.selenium.WebDriver; import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; @@ -215,6 +216,24 @@ public void init() { txt_err_debug_tab = new JTextArea(); } + public boolean is_driver_path_valid() { + WebDriver driver = ExecuteTrack.init_driver( + !btnselectChrome.isEnabled(), + Integer.toString(DEFAULT_PORT), + DRIVER_PATH, + true); + + boolean res = driver != null; + if (!res) { + System.out.println("The selenium driver executable is not working"); + } else { + driver.quit(); + } + + driver = null; // Free up + return res; + } + /** * Set a redirect of the stdout and stderr to the txtboxes in the debug tab of the GUI */ @@ -869,10 +888,14 @@ private void setup_tab_butons() { if (returnVal == JFileChooser.APPROVE_OPTION) { File file = driverSelector.getSelectedFile(); DRIVER_PATH = file.getPath(); - editConfigFile("last_driver_path", DRIVER_PATH); - lbldriver.setText("Driver Selected"); - btndriverSelector.setBackground(Color.GREEN); - btnTestTrack.setEnabled(true); + if (is_driver_path_valid()) { + editConfigFile("last_driver_path", DRIVER_PATH); + lbldriver.setText("Driver Selected"); + btndriverSelector.setBackground(Color.GREEN); + btnTestTrack.setEnabled(true); + } else { + lbldriver.setText("Driver:selected executable seems to not work, check logs"); + } } else if ((returnVal == JFileChooser.ERROR) || (returnVal == JFileChooser.ERROR_OPTION)) { lbldriver.setText("Driver:error during file selection"); System.out.println("error during file selection");