Skip to content

Commit

Permalink
Capture the logs from the Firefox console.
Browse files Browse the repository at this point in the history
  • Loading branch information
k-wall committed Jul 31, 2019
1 parent 393f076 commit 52270ec
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -100,7 +98,7 @@ public void saveScreenShots(String className, String methodName) {
} catch (Exception ex) {
log.warn("Cannot save screenshots: " + ex.getMessage());
} finally {
tearDownDrivers();
//tearDownDrivers();
}
}

Expand Down Expand Up @@ -286,6 +284,14 @@ public void waitUntilItemNotPresent(int timeInSeconds, Supplier<WebItem> item) t
waitUntilItem(timeInSeconds, item, false);
}

private Path getWebConsolePath(String target, String className, String methodName) {
return Paths.get(
target,
webconsoleFolder,
className,
methodName);
}

private <T extends WebItem> T waitUntilItem(int timeInSeconds, Supplier<T> item, boolean present) throws Exception {
log.info("Waiting for element be present");
int attempts = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 52270ec

Please sign in to comment.