From e9f3c1ed0d39e7909cfadf0f44eb5b32ec930edb Mon Sep 17 00:00:00 2001 From: k-wall Date: Thu, 1 Aug 2019 08:21:55 +0100 Subject: [PATCH] Capture the logs from the Firefox console. (#3058) https://github.com/mozilla/geckodriver/issues/284#issuecomment-458305621 --- .../systemtest/GlobalLogCollector.java | 6 +++- .../selenium/SeleniumManagement.java | 17 ++++++--- .../systemtest/selenium/SeleniumProvider.java | 36 +++++++++++-------- .../enmasse/systemtest/utils/TestUtils.java | 11 +++++- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/systemtests/src/main/java/io/enmasse/systemtest/GlobalLogCollector.java b/systemtests/src/main/java/io/enmasse/systemtest/GlobalLogCollector.java index a85bc65367c..118ea92de27 100644 --- a/systemtests/src/main/java/io/enmasse/systemtest/GlobalLogCollector.java +++ b/systemtests/src/main/java/io/enmasse/systemtest/GlobalLogCollector.java @@ -31,9 +31,13 @@ public class GlobalLogCollector { private final String namespace; public GlobalLogCollector(Kubernetes kubernetes, File logDir) { + this(kubernetes, logDir, kubernetes.getInfraNamespace()); + } + + public GlobalLogCollector(Kubernetes kubernetes, File logDir, String namespace) { this.kubernetes = kubernetes; this.logDir = logDir; - this.namespace = kubernetes.getInfraNamespace(); + this.namespace = namespace; } diff --git a/systemtests/src/main/java/io/enmasse/systemtest/selenium/SeleniumManagement.java b/systemtests/src/main/java/io/enmasse/systemtest/selenium/SeleniumManagement.java index dc2cda7ebe7..24db436ccb6 100644 --- a/systemtests/src/main/java/io/enmasse/systemtest/selenium/SeleniumManagement.java +++ b/systemtests/src/main/java/io/enmasse/systemtest/selenium/SeleniumManagement.java @@ -5,15 +5,14 @@ package io.enmasse.systemtest.selenium; -import io.enmasse.systemtest.CustomLogger; -import io.enmasse.systemtest.Kubernetes; -import io.enmasse.systemtest.SystemtestsKubernetesApps; -import io.enmasse.systemtest.TimeoutBudget; +import io.enmasse.systemtest.*; import io.enmasse.systemtest.timemeasuring.SystemtestsOperation; import io.enmasse.systemtest.timemeasuring.TimeMeasuringSystem; import io.enmasse.systemtest.utils.TestUtils; import org.slf4j.Logger; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -53,6 +52,16 @@ public static void removeFirefoxApp() throws Exception { TimeMeasuringSystem.stopOperation(operationID); } + public static void collectAppLogs(Path path) { + try { + Files.createDirectories(path); + GlobalLogCollector collector = new GlobalLogCollector(Kubernetes.getInstance(), path.toFile(), SystemtestsKubernetesApps.SELENIUM_PROJECT); + collector.collectLogsOfPodsInNamespace(SystemtestsKubernetesApps.SELENIUM_PROJECT); + } catch (Exception e) { + log.error("Failed to collect pod logs from namespace : {}", SystemtestsKubernetesApps.SELENIUM_PROJECT); + } + } + public static void removeChromeApp() throws Exception { String operationID = TimeMeasuringSystem.startOperation(SystemtestsOperation.DELETE_SELENIUM_CONTAINER); SystemtestsKubernetesApps.deleteChromeSeleniumApp(SystemtestsKubernetesApps.SELENIUM_PROJECT, Kubernetes.getInstance()); diff --git a/systemtests/src/main/java/io/enmasse/systemtest/selenium/SeleniumProvider.java b/systemtests/src/main/java/io/enmasse/systemtest/selenium/SeleniumProvider.java index 26f50eac288..a8e31ade846 100644 --- a/systemtests/src/main/java/io/enmasse/systemtest/selenium/SeleniumProvider.java +++ b/systemtests/src/main/java/io/enmasse/systemtest/selenium/SeleniumProvider.java @@ -61,36 +61,34 @@ public static synchronized SeleniumProvider getInstance() { public void onFailed(ExtensionContext extensionContext) { String getTestClassName = extensionContext.getTestClass().get().getName(); String getTestMethodName = extensionContext.getTestMethod().get().getName(); - saveBrowserLog(getTestClassName, getTestMethodName); - saveScreenShots(getTestClassName, getTestMethodName); + Path webConsolePath = getWebConsolePath(Environment.getInstance().testLogDir(), getTestClassName, getTestMethodName); + saveBrowserLog(webConsolePath); + SeleniumManagement.collectAppLogs(webConsolePath); + saveScreenShots(webConsolePath, getTestClassName, getTestMethodName); + } - public void saveBrowserLog(String className, String methodName) { + private void saveBrowserLog(Path path) { try { log.info("Saving browser console log..."); - Path path = Paths.get( - Environment.getInstance().testLogDir(), - webconsoleFolder, - className, - methodName); Files.createDirectories(path); File consoleLog = new File(path.toString(), "browser_console.log"); StringBuilder logEntries = formatedBrowserLogs(); Files.write(Paths.get(consoleLog.getPath()), logEntries.toString().getBytes()); - log.info("Browser console log saved successfully"); + log.info("Browser console log saved successfully : {}", consoleLog); } catch (Exception ex) { log.warn("Cannot save browser log: " + ex.getMessage()); } } public void saveScreenShots(String className, String methodName) { + Path webConsolePath = getWebConsolePath(Environment.getInstance().testLogDir(), className, methodName); + saveScreenShots(webConsolePath, className, methodName); + } + + private void saveScreenShots(Path path, String className, String methodName) { try { takeScreenShot(); - Path path = Paths.get( - Environment.getInstance().testLogDir(), - webconsoleFolder, - className, - methodName); Files.createDirectories(path); for (Date key : browserScreenshots.keySet()) { FileUtils.copyFile(browserScreenshots.get(key), new File(Paths.get(path.toString(), @@ -100,7 +98,7 @@ public void saveScreenShots(String className, String methodName) { } catch (Exception ex) { log.warn("Cannot save screenshots: " + ex.getMessage()); } finally { - tearDownDrivers(); + //tearDownDrivers(); } } @@ -286,6 +284,14 @@ public void waitUntilItemNotPresent(int timeInSeconds, Supplier item) t waitUntilItem(timeInSeconds, item, false); } + private Path getWebConsolePath(String target, String className, String methodName) { + return Paths.get( + target, + webconsoleFolder, + className, + methodName); + } + private T waitUntilItem(int timeInSeconds, Supplier item, boolean present) throws Exception { log.info("Waiting for element be present"); int attempts = 0; diff --git a/systemtests/src/main/java/io/enmasse/systemtest/utils/TestUtils.java b/systemtests/src/main/java/io/enmasse/systemtest/utils/TestUtils.java index 3f960d84d1e..16381f6c60f 100644 --- a/systemtests/src/main/java/io/enmasse/systemtest/utils/TestUtils.java +++ b/systemtests/src/main/java/io/enmasse/systemtest/utils/TestUtils.java @@ -22,7 +22,9 @@ import org.junit.jupiter.api.function.ThrowingSupplier; import org.openqa.selenium.Capabilities; import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.firefox.FirefoxDriverLogLevel; import org.openqa.selenium.firefox.FirefoxOptions; +import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.remote.RemoteWebDriver; import org.slf4j.Logger; @@ -492,7 +494,14 @@ public static void deleteAddressSpaceCreatedBySC(Kubernetes kubernetes, AddressS public static RemoteWebDriver getFirefoxDriver() throws Exception { Endpoint endpoint = SystemtestsKubernetesApps.getFirefoxSeleniumAppEndpoint(Kubernetes.getInstance()); - return getRemoteDriver(endpoint.getHost(), endpoint.getPort(), new FirefoxOptions()); + FirefoxOptions options = new FirefoxOptions(); + FirefoxProfile myProfile = new FirefoxProfile(); + // https://github.com/mozilla/geckodriver/issues/330 enable the emission of console.info(), warn() etc + // to stdout of the browser process. Works around the fact that Firefox logs do are not available through + // WebDriver.manage().logs() + myProfile.setPreference("devtools.console.stdout.content", true); + options.setProfile(myProfile); + return getRemoteDriver(endpoint.getHost(), endpoint.getPort(), options); } public static RemoteWebDriver getChromeDriver() throws Exception {