All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.epam.jdi.light.driver.WebDriverUtils Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
package com.epam.jdi.light.driver;

/**
 * Created by Roman Iovlev on 14.02.2018 Email: [email protected]; Skype: roman.iovlev
 */

import java.io.IOException;

import static com.epam.jdi.light.common.UnixProcessUtils.killProcessesTree;
import static com.epam.jdi.light.settings.WebSettings.logger;
import static java.lang.Runtime.getRuntime;
import static java.lang.String.format;

public final class WebDriverUtils {


    private WebDriverUtils() {
    }

    /**
     * @throws IOException
     */
    public static void killAllSeleniumDrivers() {
        String os = System.getProperty("os.name");
        try {
            if (os.contains("Mac")) {
                killAllMacOSDriverProcesses();
            } else {
                killAllWindowsDriverProcesses();
            }
        }
        catch (Exception ignore){
            logger.info("Can't kill driver processes");
        }
    }

    private static void killAllMacOSDriverProcesses() {
        killMacOSDriverProcesses("firefox");
        killMacOSDriverProcesses("chrome");
    }

    /**
     *
     */
    private static void killAllWindowsDriverProcesses() throws IOException {
        killByName("chromedriver");
        killByName("geckodriver");
        killByName("IEDriverServer");
        killByName("MicrosoftWebDriver");
    }
	
    private static void killByName(String name) throws IOException {
        getRuntime().exec(format("taskkill /F /IM %s.exe /T", name));
    }

    private static void killMacOSDriverProcesses(String browserName) {

        String name = null;
        switch (browserName.toLowerCase()) {
            case "firefox":
                name = "geckodriver";
                break;
            case "chrome":
                name = "chromedriver";
                break;

        }
        if (name != null) {
            killAllMacOSDriverProcessesByName(name);
        }
    }

    /**
     * @param driverName
     */
    private static void killAllMacOSDriverProcessesByName(String driverName) {
            killProcessesTree(driverName);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy