From dd0a7d27d3d5dccdc2a76689a7df1a8132f85c18 Mon Sep 17 00:00:00 2001 From: Darshan Date: Thu, 22 Jun 2023 18:54:02 +0530 Subject: [PATCH 01/10] Added logic to get browser version of chrome in local --- .../teswiz/runner/BrowserDriverManager.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java index 15f56f62b..0cf044c37 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java @@ -29,7 +29,11 @@ import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.safari.SafariOptions; + +import java.io.BufferedReader; import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.*; @@ -201,6 +205,29 @@ private static DriverManagerType setupBrowserDriver(TestExecutionContext testExe webDriverManagerProxyUrl, browserType)); // TODO - get browser version from local or container. What about cloud? + + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.setBinary(getChromeBinaryPath()); + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command(getChromeBinaryPath(), "--version"); + + try { + Process process = processBuilder.start(); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + if (line.startsWith("Google Chrome")) { + String[] split = line.split(" "); + String version = split[split.length - 1]; + LOGGER.info("Browser Version : " + version); + break; + } + } + process.destroy(); + } catch (IOException e) { + e.printStackTrace(); + } + WebDriverManager webDriverManager = WebDriverManager.getInstance(driverManagerType) .proxy(webDriverManagerProxyUrl); webDriverManager.setup(); @@ -213,6 +240,17 @@ private static DriverManagerType setupBrowserDriver(TestExecutionContext testExe return driverManagerType; } + private static String getChromeBinaryPath () { + String os = System.getProperty("os.name").toLowerCase(); + if (os.contains("win")) { + return "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"; + } else if (os.contains("mac")) { + return "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; + } else if (os.contains("nix") || os.contains("nux") || os.contains("linux")) { + return "/usr/bin/google-chrome"; + } + throw new IllegalStateException("Unsupported operating system: " + os); + } @NotNull private static WebDriver createChromeDriver(String forUserPersona, TestExecutionContext testExecutionContext, From 76c3b038d96fc86c2c2cf70be12da66493f8ffc2 Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 23 Jun 2023 14:11:10 +0530 Subject: [PATCH 02/10] Added get browser version logic for chrome and safari --- .../teswiz/runner/BrowserDriverManager.java | 77 ++++++++++++------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java index 0cf044c37..d348c5699 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java @@ -24,8 +24,6 @@ import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.logging.LogType; import org.openqa.selenium.logging.LoggingPreferences; -import org.openqa.selenium.remote.CapabilityType; -import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.safari.SafariOptions; @@ -198,49 +196,60 @@ private static DriverManagerType setupBrowserDriver(TestExecutionContext testExe String browserType) { DriverManagerType driverManagerType = DriverManagerType.valueOf(browserType.toUpperCase()); String webDriverManagerProxyUrl = (null == Runner.getWebDriverManagerProxyURL()) ? "" - : - Runner.getWebDriverManagerProxyURL(); + : + Runner.getWebDriverManagerProxyURL(); LOGGER.info(String.format( "Using webDriverManagerProxyUrl: '%s' for getting the WebDriver for browser: '%s'", webDriverManagerProxyUrl, browserType)); // TODO - get browser version from local or container. What about cloud? - ChromeOptions chromeOptions = new ChromeOptions(); - chromeOptions.setBinary(getChromeBinaryPath()); - ProcessBuilder processBuilder = new ProcessBuilder(); - processBuilder.command(getChromeBinaryPath(), "--version"); - - try { - Process process = processBuilder.start(); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - String line; - while ((line = reader.readLine()) != null) { - if (line.startsWith("Google Chrome")) { - String[] split = line.split(" "); - String version = split[split.length - 1]; - LOGGER.info("Browser Version : " + version); - break; - } - } - process.destroy(); - } catch (IOException e) { - e.printStackTrace(); - } + String browserVersion = getBrowserVersionFor(browserType); WebDriverManager webDriverManager = WebDriverManager.getInstance(driverManagerType) - .proxy(webDriverManagerProxyUrl); + .proxy(webDriverManagerProxyUrl); webDriverManager.setup(); String downloadedDriverVersion = webDriverManager.getDownloadedDriverVersion(); String message = String.format("Using %s browser version: %s", driverManagerType, - downloadedDriverVersion); + downloadedDriverVersion); LOGGER.info(message); ReportPortalLogger.logInfoMessage(message); return driverManagerType; } - private static String getChromeBinaryPath () { + private static String getBrowserVersionFor(String browserType) { + String binaryPath = ""; + switch (browserType) { + case "chrome": + binaryPath = getChromeBinaryPath(); + break; + case "firefox": + binaryPath = getFirefoxBinaryPath(); + break; + default: + throw new InvalidTestDataException("Invalid browser : " + browserType); + } + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.setBinary(binaryPath); + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command(binaryPath, "--version"); + String version = ""; + try { + Process process = processBuilder.start(); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = reader.readLine(); + String[] split = line.split(" "); + version = split[split.length - 1]; + LOGGER.info("Browser Version : " + version); + process.destroy(); + } catch (IOException e) { + e.printStackTrace(); + } + return version; + } + + private static String getChromeBinaryPath() { String os = System.getProperty("os.name").toLowerCase(); if (os.contains("win")) { return "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"; @@ -251,6 +260,18 @@ private static String getChromeBinaryPath () { } throw new IllegalStateException("Unsupported operating system: " + os); } + private static String getFirefoxBinaryPath() { + String os = System.getProperty("os.name").toLowerCase(); + if (os.contains("win")) { + return "C:\\Program Files\\Firefox\\Application\\firefox.exe"; + } else if (os.contains("mac")) { + return "/Applications/Firefox.app/Contents/MacOS/Firefox"; + } else if (os.contains("nix") || os.contains("nux") || os.contains("linux")) { + return "/usr/bin/firefox"; + } + throw new IllegalStateException("Unsupported operating system: " + os); + } + @NotNull private static WebDriver createChromeDriver(String forUserPersona, TestExecutionContext testExecutionContext, From 4d17a242d05dc3190a5dee21d1b45ed21a1f126a Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 28 Jun 2023 17:18:36 +0530 Subject: [PATCH 03/10] Added implementation to get browser version in container --- build.gradle | 2 + configs/indigo_local_config.properties | 3 +- .../teswiz/runner/BrowserDriverManager.java | 140 +++++++++++++++--- .../com/znsio/teswiz/features/indigo.feature | 2 +- 4 files changed, 124 insertions(+), 23 deletions(-) diff --git a/build.gradle b/build.gradle index d1c276101..e562ade81 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ buildscript { masterThoughtVersion = '5.7.5' browserStackLocalVersion = '1.1.0' jetbrainsAnnotationsVersion = '24.0.1' + } } @@ -80,6 +81,7 @@ dependencies { implementation "com.applitools:eyes-appium-java5:$project.applitoolsAppiumVersion" implementation "com.applitools:eyes-selenium-java5:$project.applitoolsSeleniumVersion" implementation "net.masterthought:cucumber-reporting:$masterThoughtVersion" + implementation 'com.github.docker-java:docker-java:3.2.9' } shadowJar { diff --git a/configs/indigo_local_config.properties b/configs/indigo_local_config.properties index 53e08e40f..a28df0eab 100644 --- a/configs/indigo_local_config.properties +++ b/configs/indigo_local_config.properties @@ -15,9 +15,10 @@ PARALLEL=1 PLATFORM=android PROXY_KEY=HTTP_PROXY REPORT_PORTAL_FILE=src/test/resources/reportportal.properties -RUN_IN_CI=false +RUN_IN_CI=true TARGET_ENVIRONMENT=prod LAUNCH_NAME_SUFFIX= on 'prod' Environment TEST_DATA_FILE=./src/test/resources/testData.json CLEANUP_DEVICE_BEFORE_STARTING_EXECUTION=false BROWSER_CONFIG_FILE=./configs/browser_config.json +REMOTE_WEBDRIVER_GRID_PORT=GRID_PORT diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java index 3fdf96c77..76f1508b4 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java @@ -1,6 +1,13 @@ package com.znsio.teswiz.runner; import com.context.TestExecutionContext; +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.ExecCreateCmdResponse; +import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.api.model.Frame; +import com.github.dockerjava.core.DefaultDockerClientConfig; +import com.github.dockerjava.core.DockerClientBuilder; +import com.github.dockerjava.core.command.ExecStartResultCallback; import com.znsio.teswiz.entities.Platform; import com.znsio.teswiz.entities.TEST_CONTEXT; import com.znsio.teswiz.exceptions.EnvironmentSetupException; @@ -28,15 +35,16 @@ import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.safari.SafariOptions; +import org.yaml.snakeyaml.Yaml; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.util.*; import java.util.logging.Level; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import static com.znsio.teswiz.runner.Runner.*; import static com.znsio.teswiz.runner.Setup.CAPS; @@ -49,6 +57,7 @@ class BrowserDriverManager { private static final String MAXIMIZE = "maximize"; private static final String EXCLUDE_SWITCHES = "excludeSwitches"; private static final String SETTING_PROXY_FOR_BROWSER = "Setting Proxy for browser: "; + private static final String FETCH_CONTAINER_BROWSER_VERSION_COMMAND = "google-chrome-stable --version"; private static int numberOfWebDriversUsed = 0; private static boolean shouldBrowserBeMaximized = false; private static boolean isRunInHeadlessMode = false; @@ -205,7 +214,8 @@ private static DriverManagerType setupBrowserDriver(TestExecutionContext testExe // TODO - get browser version from local or container. What about cloud? - String browserVersion = getBrowserVersionFor(browserType); + String localBrowserVersion = getLocalBrowserVersionFor(browserType); + String containerBrowserVersion = getContainerBrowserVersion(); WebDriverManager webDriverManager = WebDriverManager.getInstance(driverManagerType) .proxy(webDriverManagerProxyUrl); @@ -219,7 +229,69 @@ private static DriverManagerType setupBrowserDriver(TestExecutionContext testExe return driverManagerType; } - private static String getBrowserVersionFor(String browserType) { + private static String getContainerBrowserVersion() { + DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().build(); + DockerClient dockerClient = DockerClientBuilder.getInstance(config).build(); + + String imageName = ""; + String containerId = ""; + + try { + Yaml yaml = new Yaml(); + FileInputStream fis = new FileInputStream("docker-compose.yml"); + Map data = yaml.load(fis); + + Map services = (Map) data.get("services"); + Map selenium = (Map) services.get("chrome"); + imageName = (String) selenium.get("image"); + LOGGER.info("Selenium image: " + imageName); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + List containers = dockerClient.listContainersCmd() + .withShowAll(true) + .exec(); + for (Container container : containers) { + if (container.getImage().equals(imageName)) { + containerId = container.getId(); + } + } + + assert containerId != null; + ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId) + .withCmd("sh", "-c", FETCH_CONTAINER_BROWSER_VERSION_COMMAND) + .withAttachStdout(true) + .withAttachStderr(true) + .exec(); + + String browserVersion = executeAndGetBrowserVersion(dockerClient, execCreateCmdResponse.getId()); + LOGGER.info("Browser Version in container: " + browserVersion); + return browserVersion; + } + + private static String executeAndGetBrowserVersion(DockerClient dockerClient, String execId) { + StringBuilder logOutput = new StringBuilder(); + Pattern versionPattern = Pattern.compile("Google Chrome (\\d+\\.\\d+\\.\\d+\\.\\d+)"); + try { + dockerClient.execStartCmd(execId) + .exec(new ExecStartResultCallback() { + @Override + public void onNext(Frame frame) { + logOutput.append(frame.toString()); + } + }).awaitCompletion(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + Matcher matcher = versionPattern.matcher(logOutput.toString()); + if (matcher.find()) { + return matcher.group(1); + } + return null; + } + + private static String getLocalBrowserVersionFor(String browserType) { String binaryPath = ""; switch (browserType) { case "chrome": @@ -228,6 +300,8 @@ private static String getBrowserVersionFor(String browserType) { case "firefox": binaryPath = getFirefoxBinaryPath(); break; + case "safari": + return getSafariVersion(); default: throw new InvalidTestDataException("Invalid browser : " + browserType); } @@ -242,7 +316,7 @@ private static String getBrowserVersionFor(String browserType) { String line = reader.readLine(); String[] split = line.split(" "); version = split[split.length - 1]; - LOGGER.info("Browser Version : " + version); + LOGGER.info("Browser Version in system : " + version); process.destroy(); } catch (IOException e) { e.printStackTrace(); @@ -252,25 +326,49 @@ private static String getBrowserVersionFor(String browserType) { private static String getChromeBinaryPath() { String os = System.getProperty("os.name").toLowerCase(); - if (os.contains("win")) { - return "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"; - } else if (os.contains("mac")) { - return "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; - } else if (os.contains("nix") || os.contains("nux") || os.contains("linux")) { - return "/usr/bin/google-chrome"; + switch (os) { + case "win": + return System.getenv("ProgramFiles") + "\\Google\\Chrome\\Application\\chrome.exe"; + case "mac os x": + return "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; + case "nix": + case "nux": + case "linux": + return "/usr/bin/google-chrome"; + default: + throw new IllegalStateException("Unsupported operating system: " + os); } - throw new IllegalStateException("Unsupported operating system: " + os); } + private static String getFirefoxBinaryPath() { String os = System.getProperty("os.name").toLowerCase(); - if (os.contains("win")) { - return "C:\\Program Files\\Firefox\\Application\\firefox.exe"; - } else if (os.contains("mac")) { - return "/Applications/Firefox.app/Contents/MacOS/Firefox"; - } else if (os.contains("nix") || os.contains("nux") || os.contains("linux")) { - return "/usr/bin/firefox"; + switch (os) { + case "win": + return System.getenv("ProgramFiles") + "\\Firefox\\Application\\firefox.exe"; + case "mac": + return "/Applications/Firefox.app/Contents/MacOS/firefox"; + case "nix": + case "nux": + case "linux": + return "/usr/bin/firefox"; + default: + throw new IllegalStateException("Unsupported operating system: " + os); + } + } + + private static String getSafariVersion() { + String safariVersion = ""; + try { + Process process = Runtime.getRuntime() + .exec(new String[]{"defaults", "read", "/Applications/Safari.app/Contents/Info.plist", "CFBundleShortVersionString"}); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + safariVersion = reader.readLine(); + reader.close(); + } catch (IOException e) { + e.printStackTrace(); } - throw new IllegalStateException("Unsupported operating system: " + os); + LOGGER.info("Safari version in system = " + safariVersion); + return safariVersion; } @NotNull diff --git a/src/test/resources/com/znsio/teswiz/features/indigo.feature b/src/test/resources/com/znsio/teswiz/features/indigo.feature index eec74aa7f..3114efb2a 100644 --- a/src/test/resources/com/znsio/teswiz/features/indigo.feature +++ b/src/test/resources/com/znsio/teswiz/features/indigo.feature @@ -2,7 +2,7 @@ Feature: Search for flight options # CONFIG=./configs/indigo_local_config.properties PLATFORM=web TAG=searchFlights ./gradlew run - @web @searchFlights + @web @searchFlights @browserVersionTest Scenario: Search for one way ticket from Pune to Delhi for single passenger Given I search for a "one-way" ticket from "Pune" to "Delhi" for "1" adult passenger From a8b2b5af4028ea2b30a9f3439ff030ef0bdbd44a Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 28 Jun 2023 17:30:31 +0530 Subject: [PATCH 04/10] Added version variable in build.gradle --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index e562ade81..b49789801 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ buildscript { masterThoughtVersion = '5.7.5' browserStackLocalVersion = '1.1.0' jetbrainsAnnotationsVersion = '24.0.1' - + dockerJavaVersion = '3.2.9' } } @@ -81,7 +81,7 @@ dependencies { implementation "com.applitools:eyes-appium-java5:$project.applitoolsAppiumVersion" implementation "com.applitools:eyes-selenium-java5:$project.applitoolsSeleniumVersion" implementation "net.masterthought:cucumber-reporting:$masterThoughtVersion" - implementation 'com.github.docker-java:docker-java:3.2.9' + implementation "com.github.docker-java:docker-java:$dockerJavaVersion" } shadowJar { From 6d39b8a0861d5f7c5bae270252f31cb5a1cd75de Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 28 Jun 2023 17:32:21 +0530 Subject: [PATCH 05/10] disabled run in CI --- configs/indigo_local_config.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configs/indigo_local_config.properties b/configs/indigo_local_config.properties index a28df0eab..a0d972f2a 100644 --- a/configs/indigo_local_config.properties +++ b/configs/indigo_local_config.properties @@ -15,7 +15,7 @@ PARALLEL=1 PLATFORM=android PROXY_KEY=HTTP_PROXY REPORT_PORTAL_FILE=src/test/resources/reportportal.properties -RUN_IN_CI=true +RUN_IN_CI=false TARGET_ENVIRONMENT=prod LAUNCH_NAME_SUFFIX= on 'prod' Environment TEST_DATA_FILE=./src/test/resources/testData.json From b41840ba371f9ab96e16f7870eb88850444e01d9 Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 28 Jun 2023 18:34:08 +0530 Subject: [PATCH 06/10] added constants for path and indentation --- .../teswiz/runner/BrowserDriverManager.java | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java index 76f1508b4..0b2cbe414 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java @@ -36,7 +36,6 @@ import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.safari.SafariOptions; import org.yaml.snakeyaml.Yaml; - import java.io.*; import java.net.MalformedURLException; import java.net.URL; @@ -58,6 +57,10 @@ class BrowserDriverManager { private static final String EXCLUDE_SWITCHES = "excludeSwitches"; private static final String SETTING_PROXY_FOR_BROWSER = "Setting Proxy for browser: "; private static final String FETCH_CONTAINER_BROWSER_VERSION_COMMAND = "google-chrome-stable --version"; + private static final String CHROME_PATH_FOR_MAC = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; + private static final String CHROME_PATH_FOR_WINDOWS = "\\Google\\Chrome\\Application\\chrome.exe"; + public static final String FIREFOX_PATH_FOR_MAC = "/Applications/Firefox.app/Contents/MacOS/firefox"; + public static final String FIREFOX_PATH_FOR_WINDOWS = "\\Firefox\\Application\\firefox.exe"; private static int numberOfWebDriversUsed = 0; private static boolean shouldBrowserBeMaximized = false; private static boolean isRunInHeadlessMode = false; @@ -211,9 +214,6 @@ private static DriverManagerType setupBrowserDriver(TestExecutionContext testExe LOGGER.info(String.format( "Using webDriverManagerProxyUrl: '%s' for getting the WebDriver for browser: '%s'", webDriverManagerProxyUrl, browserType)); - - // TODO - get browser version from local or container. What about cloud? - String localBrowserVersion = getLocalBrowserVersionFor(browserType); String containerBrowserVersion = getContainerBrowserVersion(); @@ -240,7 +240,6 @@ private static String getContainerBrowserVersion() { Yaml yaml = new Yaml(); FileInputStream fis = new FileInputStream("docker-compose.yml"); Map data = yaml.load(fis); - Map services = (Map) data.get("services"); Map selenium = (Map) services.get("chrome"); imageName = (String) selenium.get("image"); @@ -274,13 +273,12 @@ private static String executeAndGetBrowserVersion(DockerClient dockerClient, Str StringBuilder logOutput = new StringBuilder(); Pattern versionPattern = Pattern.compile("Google Chrome (\\d+\\.\\d+\\.\\d+\\.\\d+)"); try { - dockerClient.execStartCmd(execId) - .exec(new ExecStartResultCallback() { - @Override - public void onNext(Frame frame) { - logOutput.append(frame.toString()); - } - }).awaitCompletion(); + dockerClient.execStartCmd(execId).exec(new ExecStartResultCallback() { + @Override + public void onNext(Frame frame) { + logOutput.append(frame.toString()); + } + }).awaitCompletion(); } catch (InterruptedException e) { e.printStackTrace(); } @@ -293,12 +291,13 @@ public void onNext(Frame frame) { private static String getLocalBrowserVersionFor(String browserType) { String binaryPath = ""; + String os = System.getProperty("os.name").toLowerCase(); switch (browserType) { case "chrome": - binaryPath = getChromeBinaryPath(); + binaryPath = getChromeBinaryPath(os); break; case "firefox": - binaryPath = getFirefoxBinaryPath(); + binaryPath = getFirefoxBinaryPath(os); break; case "safari": return getSafariVersion(); @@ -324,35 +323,33 @@ private static String getLocalBrowserVersionFor(String browserType) { return version; } - private static String getChromeBinaryPath() { - String os = System.getProperty("os.name").toLowerCase(); + private static String getChromeBinaryPath(String os) { switch (os) { case "win": - return System.getenv("ProgramFiles") + "\\Google\\Chrome\\Application\\chrome.exe"; + return System.getenv("ProgramFiles") + CHROME_PATH_FOR_WINDOWS; case "mac os x": - return "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; + return CHROME_PATH_FOR_MAC; case "nix": case "nux": case "linux": return "/usr/bin/google-chrome"; default: - throw new IllegalStateException("Unsupported operating system: " + os); + throw new IllegalStateException("Unsupported operating system for chrome: " + os); } } - private static String getFirefoxBinaryPath() { - String os = System.getProperty("os.name").toLowerCase(); + private static String getFirefoxBinaryPath(String os) { switch (os) { case "win": - return System.getenv("ProgramFiles") + "\\Firefox\\Application\\firefox.exe"; + return System.getenv("ProgramFiles") + FIREFOX_PATH_FOR_WINDOWS; case "mac": - return "/Applications/Firefox.app/Contents/MacOS/firefox"; + return FIREFOX_PATH_FOR_MAC; case "nix": case "nux": case "linux": return "/usr/bin/firefox"; default: - throw new IllegalStateException("Unsupported operating system: " + os); + throw new IllegalStateException("Unsupported operating system for firefox: " + os); } } From e903d909c0e350a11d38fafae359975a02b1a21c Mon Sep 17 00:00:00 2001 From: Darshan Date: Thu, 29 Jun 2023 13:08:48 +0530 Subject: [PATCH 07/10] Removed chrome options set binary path --- src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java index 0b2cbe414..26e95f73b 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java @@ -304,8 +304,6 @@ private static String getLocalBrowserVersionFor(String browserType) { default: throw new InvalidTestDataException("Invalid browser : " + browserType); } - ChromeOptions chromeOptions = new ChromeOptions(); - chromeOptions.setBinary(binaryPath); ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command(binaryPath, "--version"); String version = ""; From 7121b10dd60de620ad6cef7613485b170ee18f17 Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 30 Jun 2023 14:40:15 +0530 Subject: [PATCH 08/10] Removed local browser version fetch methods --- .../teswiz/runner/BrowserDriverManager.java | 78 ------------------- 1 file changed, 78 deletions(-) diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java index 26e95f73b..b11d88199 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java @@ -214,7 +214,6 @@ private static DriverManagerType setupBrowserDriver(TestExecutionContext testExe LOGGER.info(String.format( "Using webDriverManagerProxyUrl: '%s' for getting the WebDriver for browser: '%s'", webDriverManagerProxyUrl, browserType)); - String localBrowserVersion = getLocalBrowserVersionFor(browserType); String containerBrowserVersion = getContainerBrowserVersion(); WebDriverManager webDriverManager = WebDriverManager.getInstance(driverManagerType) @@ -289,83 +288,6 @@ public void onNext(Frame frame) { return null; } - private static String getLocalBrowserVersionFor(String browserType) { - String binaryPath = ""; - String os = System.getProperty("os.name").toLowerCase(); - switch (browserType) { - case "chrome": - binaryPath = getChromeBinaryPath(os); - break; - case "firefox": - binaryPath = getFirefoxBinaryPath(os); - break; - case "safari": - return getSafariVersion(); - default: - throw new InvalidTestDataException("Invalid browser : " + browserType); - } - ProcessBuilder processBuilder = new ProcessBuilder(); - processBuilder.command(binaryPath, "--version"); - String version = ""; - try { - Process process = processBuilder.start(); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - String line = reader.readLine(); - String[] split = line.split(" "); - version = split[split.length - 1]; - LOGGER.info("Browser Version in system : " + version); - process.destroy(); - } catch (IOException e) { - e.printStackTrace(); - } - return version; - } - - private static String getChromeBinaryPath(String os) { - switch (os) { - case "win": - return System.getenv("ProgramFiles") + CHROME_PATH_FOR_WINDOWS; - case "mac os x": - return CHROME_PATH_FOR_MAC; - case "nix": - case "nux": - case "linux": - return "/usr/bin/google-chrome"; - default: - throw new IllegalStateException("Unsupported operating system for chrome: " + os); - } - } - - private static String getFirefoxBinaryPath(String os) { - switch (os) { - case "win": - return System.getenv("ProgramFiles") + FIREFOX_PATH_FOR_WINDOWS; - case "mac": - return FIREFOX_PATH_FOR_MAC; - case "nix": - case "nux": - case "linux": - return "/usr/bin/firefox"; - default: - throw new IllegalStateException("Unsupported operating system for firefox: " + os); - } - } - - private static String getSafariVersion() { - String safariVersion = ""; - try { - Process process = Runtime.getRuntime() - .exec(new String[]{"defaults", "read", "/Applications/Safari.app/Contents/Info.plist", "CFBundleShortVersionString"}); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - safariVersion = reader.readLine(); - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - LOGGER.info("Safari version in system = " + safariVersion); - return safariVersion; - } - @NotNull private static WebDriver createChromeDriver(String forUserPersona, TestExecutionContext testExecutionContext, From d4cfb11a78893cfa46458b141b791cf722126aca Mon Sep 17 00:00:00 2001 From: Darshan Date: Sun, 2 Jul 2023 21:56:34 +0530 Subject: [PATCH 09/10] Removed docker terminal command approach for browser version fetching --- .../teswiz/runner/BrowserDriverManager.java | 60 ------------------- 1 file changed, 60 deletions(-) diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java index b11d88199..f789a259d 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java @@ -214,7 +214,6 @@ private static DriverManagerType setupBrowserDriver(TestExecutionContext testExe LOGGER.info(String.format( "Using webDriverManagerProxyUrl: '%s' for getting the WebDriver for browser: '%s'", webDriverManagerProxyUrl, browserType)); - String containerBrowserVersion = getContainerBrowserVersion(); WebDriverManager webDriverManager = WebDriverManager.getInstance(driverManagerType) .proxy(webDriverManagerProxyUrl); @@ -228,65 +227,6 @@ private static DriverManagerType setupBrowserDriver(TestExecutionContext testExe return driverManagerType; } - private static String getContainerBrowserVersion() { - DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().build(); - DockerClient dockerClient = DockerClientBuilder.getInstance(config).build(); - - String imageName = ""; - String containerId = ""; - - try { - Yaml yaml = new Yaml(); - FileInputStream fis = new FileInputStream("docker-compose.yml"); - Map data = yaml.load(fis); - Map services = (Map) data.get("services"); - Map selenium = (Map) services.get("chrome"); - imageName = (String) selenium.get("image"); - LOGGER.info("Selenium image: " + imageName); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - - List containers = dockerClient.listContainersCmd() - .withShowAll(true) - .exec(); - for (Container container : containers) { - if (container.getImage().equals(imageName)) { - containerId = container.getId(); - } - } - - assert containerId != null; - ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId) - .withCmd("sh", "-c", FETCH_CONTAINER_BROWSER_VERSION_COMMAND) - .withAttachStdout(true) - .withAttachStderr(true) - .exec(); - - String browserVersion = executeAndGetBrowserVersion(dockerClient, execCreateCmdResponse.getId()); - LOGGER.info("Browser Version in container: " + browserVersion); - return browserVersion; - } - - private static String executeAndGetBrowserVersion(DockerClient dockerClient, String execId) { - StringBuilder logOutput = new StringBuilder(); - Pattern versionPattern = Pattern.compile("Google Chrome (\\d+\\.\\d+\\.\\d+\\.\\d+)"); - try { - dockerClient.execStartCmd(execId).exec(new ExecStartResultCallback() { - @Override - public void onNext(Frame frame) { - logOutput.append(frame.toString()); - } - }).awaitCompletion(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - Matcher matcher = versionPattern.matcher(logOutput.toString()); - if (matcher.find()) { - return matcher.group(1); - } - return null; - } @NotNull private static WebDriver createChromeDriver(String forUserPersona, From 514dec261ab1fcbf6afd76835b46765a7996af96 Mon Sep 17 00:00:00 2001 From: Darshan Date: Sun, 2 Jul 2023 22:00:11 +0530 Subject: [PATCH 10/10] Removed imports not used --- build.gradle | 2 -- .../znsio/teswiz/runner/BrowserDriverManager.java | 15 --------------- 2 files changed, 17 deletions(-) diff --git a/build.gradle b/build.gradle index c0d37647b..ed7bb0b38 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,6 @@ buildscript { masterThoughtVersion = '5.7.6' browserStackLocalVersion = '1.1.0' jetbrainsAnnotationsVersion = '24.0.1' - dockerJavaVersion = '3.2.9' } } @@ -81,7 +80,6 @@ dependencies { implementation "com.applitools:eyes-appium-java5:$project.applitoolsAppiumVersion" implementation "com.applitools:eyes-selenium-java5:$project.applitoolsSeleniumVersion" implementation "net.masterthought:cucumber-reporting:$masterThoughtVersion" - implementation "com.github.docker-java:docker-java:$dockerJavaVersion" } shadowJar { diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java index f789a259d..c80b62fee 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java @@ -1,13 +1,6 @@ package com.znsio.teswiz.runner; import com.context.TestExecutionContext; -import com.github.dockerjava.api.DockerClient; -import com.github.dockerjava.api.command.ExecCreateCmdResponse; -import com.github.dockerjava.api.model.Container; -import com.github.dockerjava.api.model.Frame; -import com.github.dockerjava.core.DefaultDockerClientConfig; -import com.github.dockerjava.core.DockerClientBuilder; -import com.github.dockerjava.core.command.ExecStartResultCallback; import com.znsio.teswiz.entities.Platform; import com.znsio.teswiz.entities.TEST_CONTEXT; import com.znsio.teswiz.exceptions.EnvironmentSetupException; @@ -35,14 +28,11 @@ import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.safari.SafariOptions; -import org.yaml.snakeyaml.Yaml; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.util.*; import java.util.logging.Level; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import static com.znsio.teswiz.runner.Runner.*; import static com.znsio.teswiz.runner.Setup.CAPS; @@ -56,11 +46,6 @@ class BrowserDriverManager { private static final String MAXIMIZE = "maximize"; private static final String EXCLUDE_SWITCHES = "excludeSwitches"; private static final String SETTING_PROXY_FOR_BROWSER = "Setting Proxy for browser: "; - private static final String FETCH_CONTAINER_BROWSER_VERSION_COMMAND = "google-chrome-stable --version"; - private static final String CHROME_PATH_FOR_MAC = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; - private static final String CHROME_PATH_FOR_WINDOWS = "\\Google\\Chrome\\Application\\chrome.exe"; - public static final String FIREFOX_PATH_FOR_MAC = "/Applications/Firefox.app/Contents/MacOS/firefox"; - public static final String FIREFOX_PATH_FOR_WINDOWS = "\\Firefox\\Application\\firefox.exe"; private static int numberOfWebDriversUsed = 0; private static boolean shouldBrowserBeMaximized = false; private static boolean isRunInHeadlessMode = false;