close all window firefox using selenium in java?

Viewed 415

I have a problem when I want to close all Firefox that is a running,I can close only one window. for example like this : I have a program where when I run firefox it will open and then end it will close firefox, but the problem is when my program is not running yet but there is already firefox running first and therefore I want to make before my program runs firefox I want to check whether there is a Firefox that is already open first, if there is then I have to close it, how do I do it?

and this is a little sample code

private static WebDriver driver;
private static WebDriverWait wait;

public static void main(String[] args) throws Exception {
    
    
    System.setProperty("webdriver.gecko.driver", DRIVER_LOCATION);
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    wait = new WebDriverWait(driver, 30, 100);
    

    runJourneys();
}

and this is the last process to close firefox

driver.quit();

my question is :

  1. How do I check is there firefox that has been running before ?
  2. How can I close all firefox window ?
3 Answers

Being that you haven't specified the working OS below you can find a method that will help you to achieve the desired task without third-party apps.

executeCommand - in order to kill a process from Java, as you may guess, you need to open a process in the command line - below that was achieved using ProcessBuilder. In the current form, the method is using varargs - so you can pass multiple parameters.

killBrowserProcess - is getting the current OS and executes the specific cli command to kill that process - the method can be used in various situations not only for killing the browser.

    public void killBrowserProcess(String browserName){
    String osName = System.getProperty("os.name").toLowerCase();
    if (osName.contains("windows")) {
        String windowsCmdLine = "taskkill /f /IM " + browserName + ".exe";
        executeCommand(windowsCmdLine);
    } else {
        if (osName.contains("mac") || osName.contains("linux")) {
            executeCommand("/usr/bin/killall -KILL " + browserName);
        }
    }
}

private void executeCommand(String... args) {
    ProcessBuilder pb;
    Process pc;
    try {
        for (String arg : args) {
            pb = new ProcessBuilder(arg.split(" "));
            pc = pb.start();
            pc.waitFor();
        }
        
    } catch (InterruptedException | IOException e) {
        e.printStackTrace();
        //handle the exception
    }
}

To check whether Firefox is running or not write your own function as follow :

public boolean isFirefoxRunning(){
    boolean flag = false;
    try {
        Process proc = Runtime.getRuntime().exec("wmic.exe");
        BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
        oStream.write("process where name='firefox.exe'");
        oStream.flush();
        oStream.close();
        while ( input.readLine() != null) {
            flag= true;
            break;
        }
        input.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return flag;
}

You can write your own utility function to close all Firefox instances/windows as follow :

public void closeAllFirefoxInstances(){
    try {
        Runtime.getRuntime().exec("TASKKILL /F /IM firefox.exe");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

driver.close();-This is for close only open tab which are processing

driver,quit(); -This is exit from browser(end the session, tabs, pop-ups)

Related