mirror of
https://github.com/burakkaygusuz/selenium-4-grid-k8s-docker.git
synced 2021-06-27 00:03:24 +03:00
Fix the drivers instantiation using thread safe
This commit is contained in:
@@ -1,33 +1,35 @@
|
||||
package com.burakkaygusuz;
|
||||
package com.burakkaygusuz.config;
|
||||
|
||||
import com.burakkaygusuz.config.DriverFactory;
|
||||
import com.burakkaygusuz.enums.Browsers;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class DriverBase {
|
||||
|
||||
public static final List<DriverFactory> webDriverThreadPool = Collections.synchronizedList(new ArrayList<>());
|
||||
public static ThreadLocal<DriverFactory> driverFactoryThread;
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(DriverBase.class.getName());
|
||||
|
||||
protected RemoteWebDriver driver;
|
||||
protected WebDriverWait wait;
|
||||
private static final List<DriverFactory> threadPool = Collections.synchronizedList(new ArrayList<>());
|
||||
private static ThreadLocal<DriverFactory> driverThread;
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
driverFactoryThread = ThreadLocal.withInitial(() -> {
|
||||
DriverFactory driverFactory = new DriverFactory();
|
||||
webDriverThreadPool.add(driverFactory);
|
||||
return driverFactory;
|
||||
public void instantiateDriverObject() {
|
||||
|
||||
logger.info(String.format("Operating System : %s", System.getProperty("os.name").toUpperCase()));
|
||||
logger.info(String.format("Version : %s", System.getProperty("os.version")));
|
||||
logger.info(String.format("Arch : %s", System.getProperty("os.arch")));
|
||||
logger.info(String.format("Grid URL : %s", DriverFactory.HUB_URL));
|
||||
logger.info(String.format("Tests running on %d cores...)", Runtime.getRuntime().availableProcessors()));
|
||||
|
||||
driverThread = ThreadLocal.withInitial(() -> {
|
||||
DriverFactory factory = new DriverFactory();
|
||||
threadPool.add(factory);
|
||||
return factory;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -41,18 +43,13 @@ public class DriverBase {
|
||||
logger.info(String.format("Test: %s finished", testInfo.getDisplayName()));
|
||||
}
|
||||
|
||||
public static RemoteWebDriver getWebDriver(Browsers browser) {
|
||||
return driverFactoryThread.get().getWebDriver(browser);
|
||||
}
|
||||
|
||||
public static WebDriverWait getDriverWait(RemoteWebDriver driver) {
|
||||
return driverFactoryThread.get().getWebDriverWait(driver, 10, 0);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void tearDown() {
|
||||
for (DriverFactory driverFactory : webDriverThreadPool) {
|
||||
driverFactory.quitWebDriver();
|
||||
}
|
||||
public void closeDriverObject() {
|
||||
threadPool.forEach(DriverFactory::quitWebDriver);
|
||||
driverThread.remove();
|
||||
}
|
||||
|
||||
public static RemoteWebDriver getDriver(Browsers browser) {
|
||||
return driverThread.get().getDriver(browser);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,9 @@ import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.time.Duration;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static com.burakkaygusuz.config.DriverType.*;
|
||||
@@ -17,12 +15,11 @@ import static com.burakkaygusuz.config.DriverType.*;
|
||||
public class DriverFactory {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(DriverFactory.class.getName());
|
||||
protected static WebDriverWait wait;
|
||||
protected RemoteWebDriver driver;
|
||||
private RemoteWebDriver driver;
|
||||
|
||||
private static final String HUB_URL = getHubUrl();
|
||||
protected static final String HUB_URL = getHubUrl();
|
||||
|
||||
public RemoteWebDriver getWebDriver(Browsers browser) {
|
||||
protected RemoteWebDriver getDriver(Browsers browser) {
|
||||
if (driver == null) {
|
||||
try {
|
||||
switch (browser) {
|
||||
@@ -48,13 +45,7 @@ public class DriverFactory {
|
||||
return driver;
|
||||
}
|
||||
|
||||
public WebDriverWait getWebDriverWait(RemoteWebDriver driver, int seconds, int milliSeconds) {
|
||||
if (wait == null)
|
||||
wait = new WebDriverWait(driver, Duration.ofSeconds(seconds), Duration.ofMillis(milliSeconds));
|
||||
return wait;
|
||||
}
|
||||
|
||||
public void quitWebDriver() {
|
||||
protected void quitWebDriver() {
|
||||
if (driver != null) {
|
||||
driver.quit();
|
||||
driver = null;
|
||||
|
||||
Reference in New Issue
Block a user