From ce1c4f3282e6da3b4165e1e44670a22a44018051 Mon Sep 17 00:00:00 2001 From: Andrei Kamarouski Date: Mon, 26 Aug 2024 08:59:09 +0200 Subject: [PATCH 1/2] refactor(IDriverPool): replace executor pool for driver close by fluent wait --- .../carina/webdriver/IDriverPool.java | 59 ++++++------------- .../config/WebDriverConfiguration.java | 5 ++ src/main/resources/config.properties | 1 + 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/zebrunner/carina/webdriver/IDriverPool.java b/src/main/java/com/zebrunner/carina/webdriver/IDriverPool.java index b9518ee..a501ceb 100644 --- a/src/main/java/com/zebrunner/carina/webdriver/IDriverPool.java +++ b/src/main/java/com/zebrunner/carina/webdriver/IDriverPool.java @@ -16,16 +16,12 @@ package com.zebrunner.carina.webdriver; import java.lang.invoke.MethodHandles; +import java.time.Duration; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; import io.appium.java_client.remote.MobileCapabilityType; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -36,10 +32,10 @@ import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.SessionId; import org.openqa.selenium.support.decorators.Decorated; +import org.openqa.selenium.support.ui.FluentWait; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.zebrunner.agent.core.registrar.Label; import com.zebrunner.carina.utils.R; import com.zebrunner.carina.utils.common.CommonUtils; import com.zebrunner.carina.utils.commons.SpecialKeywords; @@ -286,38 +282,27 @@ default void removeCapabilities() { private void quitDriver(CarinaDriver carinaDriver, @Deprecated boolean keepProxyDuring) { try { carinaDriver.getDevice().disconnectRemote(); - // castDriver to disable DriverListener operations on quit - WebDriver drv = castDriver(carinaDriver.getDriver()); POOL_LOGGER.debug("start driver quit: {}", carinaDriver.getName()); - - Future future = Executors.newSingleThreadExecutor().submit((Callable) () -> { - if (Configuration.get(WebDriverConfiguration.Parameter.CHROME_CLOSURE, Boolean.class).orElse(false)) { - // workaround to not cleaned chrome profiles on hard drive - POOL_LOGGER.debug("Starting drv.close()"); - drv.close(); - POOL_LOGGER.debug("Finished drv.close()"); - } - POOL_LOGGER.debug("Starting drv.quit()"); - drv.quit(); - POOL_LOGGER.debug("Finished drv.quit()"); - return null; - }); - // default timeout for driver quit 1/2 of explicit - long timeout = Configuration.getRequired(WebDriverConfiguration.Parameter.EXPLICIT_TIMEOUT, Integer.class) / 2; + Duration timeout = Duration.ofSeconds(Configuration.getRequired(Parameter.DRIVER_CLOSE_TIMEOUT, Integer.class)); try { - future.get(timeout, TimeUnit.SECONDS); - } catch (InterruptedException e) { - POOL_LOGGER.error("InterruptedException: Unable to quit driver!", e); - Thread.currentThread().interrupt(); - } catch (ExecutionException e) { - if (e.getMessage() != null && e.getMessage().contains("not found in active sessions")) { - POOL_LOGGER.warn("Skip driver quit for already disconnected session!"); - } else { - POOL_LOGGER.error("ExecutionException: Unable to quit driver!", e); - } - } catch (java.util.concurrent.TimeoutException e) { + new FluentWait<>(castDriver(carinaDriver.getDriver())) + .pollingEvery(timeout.plus(Duration.ofSeconds(5))) + .withTimeout(timeout) + .until(driver -> { + if (Configuration.get(WebDriverConfiguration.Parameter.CHROME_CLOSURE, Boolean.class).orElse(false)) { + // workaround to not cleaned chrome profiles on hard drive + POOL_LOGGER.debug("Starting drv.close()"); + driver.close(); + POOL_LOGGER.debug("Finished drv.close()"); + } + POOL_LOGGER.debug("Starting drv.quit()"); + driver.quit(); + POOL_LOGGER.debug("Finished drv.quit()"); + return true; + }); + } catch (org.openqa.selenium.TimeoutException e) { POOL_LOGGER.error("Unable to quit driver for {} sec!", timeout, e); } } catch (WebDriverException e) { @@ -327,12 +312,6 @@ private void quitDriver(CarinaDriver carinaDriver, @Deprecated boolean keepProxy POOL_LOGGER.error("Error discovered during driver quit!", e); } finally { POOL_LOGGER.debug("finished driver quit: {}", carinaDriver.getName()); - if (!keepProxyDuring) { -// ProxyPool.stopProxy(); -// if (com.zebrunner.carina.proxy.ProxyPool.isProxyRegistered()) { -// com.zebrunner.carina.proxy.ProxyPool.stopProxy(); -// } - } } } diff --git a/src/main/java/com/zebrunner/carina/webdriver/config/WebDriverConfiguration.java b/src/main/java/com/zebrunner/carina/webdriver/config/WebDriverConfiguration.java index b83008d..a312334 100644 --- a/src/main/java/com/zebrunner/carina/webdriver/config/WebDriverConfiguration.java +++ b/src/main/java/com/zebrunner/carina/webdriver/config/WebDriverConfiguration.java @@ -209,6 +209,11 @@ public enum Parameter implements IParameter { */ ALLOW_FULLSIZE_SCREENSHOT("allow_fullsize_screenshot"), + /** + * Driver close timeout (in seconds). Default: 20 + */ + DRIVER_CLOSE_TIMEOUT("driver_close_timeout"), + /** * Timeout is seconds to wait for a certain condition to occur before proceeding further in the code. * Default: {@code 20} diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties index e2c0b95..cc40248 100644 --- a/src/main/resources/config.properties +++ b/src/main/resources/config.properties @@ -23,6 +23,7 @@ proxy_pac_local=false proxy_zebrunner_args=NULL auto_screenshot=false allow_fullsize_screenshot=false +driver_close_timeout=20 explicit_timeout=20 read_timeout=660 auto_download=false From cfdb3337fe96bb0dd898b233dd22046b487cd86b Mon Sep 17 00:00:00 2001 From: Andrei Kamarouski Date: Mon, 26 Aug 2024 09:03:10 +0200 Subject: [PATCH 2/2] fix[pom.xml]: replace nexus --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index c58c41f..9535b4c 100644 --- a/pom.xml +++ b/pom.xml @@ -83,7 +83,7 @@ zebrunner_snapshots zebrunner Snapshots - https://nexus.zebrunner.dev/repository/ce-snapshots/ + https://public-nexus.zebrunner.com/repository/ce-snapshots/ false @@ -567,7 +567,7 @@ ZBR_Nexus Zebrunner Snapshots - https://nexus.zebrunner.dev/repository/ce-snapshots/ + https://public-nexus.zebrunner.com/repository/ce-snapshots/