Integration of selenium grid with automation framework

121 Views Asked by At

Please Note:Kindly request the people not to mark this as duplicate or etc. Request everyone to read the question.

I have come across the task where I need to integrate the selenium grid in my automation framework: I have the following classes:

DriverFactory
DriverManager

Apart from it, I have the drivers folder where I have defined the following things.

selenium standalone server jar
hub.json
node1.json
chromedriver.exe

DriverManager Class:

public class DriverManager {
    public static ThreadLocal<WebDriver> driver  = new ThreadLocal<>();

    public static WebDriver getDriver() {
        return driver.get();
    }

    public static void setDriver(WebDriver drive) {
        driver.set(drive);
    }

    public static void removeDriver() {
        if (driver.get() != null) {
            driver.get().quit();
            driver.remove();
        }
    }
}

Here I need to replace the WebDriver with RemoteWebDriver but when I do it I am facing an error.

DriverFactory Class:

public class DriverFactory {

    public static WebDriver createInstance(String browser) {
        if (browser.equals("chrome")) {

            ChromeDriver driver = new ChromeDriver(getChromeOptions());

            try {
                driver.manage().deleteAllCookies();
                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
            }
            catch (Exception e) {

            }
            return driver;
        }
        return null;
    }

    public static void killBrowserInstance(String browser) throws InterruptedException {
        try {


            if (browser.equals("chrome")) {

                if (OsUtils.isWindows()) {
                    Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
                }
                else {
                    Runtime.getRuntime().exec("killall \"chromedriver\"");
                    Runtime.getRuntime().exec("killall \"Google Chrome\"");
                }
            }

            Thread.sleep(1000);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static ChromeOptions getChromeOptions() {
        if (OsUtils.isWindows()) {
            System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe");
        }
        else {
            System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver");
        }

        ChromeOptions options = new ChromeOptions();
        return options;
    }
}

Kindly ignore the imports statement. Please let me know if you required any more information, even able for a remote solution to it will be available for screen share.

Sample Test-Case:

 @Test
    public void LoginValid() throws Throwable {
        try {
            loginpage.login(prop.getProperty("username"), prop.getProperty("password"));
            // Logout
        } catch (Exception e) {
           Logz.step(LogStatus.FAIL, "Unable to Login");
            e.printStackTrace();
            throw e;
        }

    }

When I trigger the testng,internally calls are happenning where the driver initialiation and instance is created.

 public synchronized void onTestStart(ITestResult iTestResult)
        DriverManager.setDriver(DriverFactory.createInstance(iTestResult.getTestContext().getCurrentXmlTest().getLocalParameters().get("browser")));
    }
0

There are 0 best solutions below