All Downloads are FREE. Search and download functionalities are using the official Maven repository.

qa.justtestlah.log.WebDriverLogEnricher Maven / Gradle / Ivy

Go to download

JustTestLah! is a JAVA test framework targeting projects that support multiple platforms, in particular Web, Android and iOS. It follows a BDD approach and allows testing against all platforms using the same feature files. JustTestLah's main aim is to make the configuration and the actual test code as easy as possible.

There is a newer version: 1.9-RC4
Show newest version
package qa.justtestlah.log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.support.events.WebDriverEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qa.justtestlah.configuration.PropertiesHolder;

/**
 * Fetches logs from the WebDriver (server) and attaches them to the current (client) log.
 *
 * 

logTypes can be configured in justtestlah.properties: * *

e.g. additionalLogTypes=server,logcat * *

For Appium, this requires the server to be started with `appium --relaxed-security`. */ public class WebDriverLogEnricher implements WebDriverEventListener { // filter to match log lines related to fetching the log private static final String REGEXP_FILTER = ".*Retrieving .* log.*|.*Calling AppiumDriver.getLog.*|Responding to client with driver.getLog.*|.*POST.*log.*|.*\\{\"type\":\"server\"\\}.*"; private static final Logger LOG = LoggerFactory.getLogger("webdriver"); private List logTypes = new ArrayList<>(); public WebDriverLogEnricher() { String property = new PropertiesHolder().getOptionalProperty("additionalLogTypes"); if (property != null && !property.isEmpty()) { logTypes.addAll(Arrays.asList(property.split(","))); } } @SuppressWarnings("squid:S4784") private void appendWebDriverLog(WebDriver driver) { for (String logType : logTypes) { for (LogEntry log : driver.manage().logs().get(logType)) { DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS"); String message = log.getMessage(); // filter out messages related to fetching the log if (!message.matches(REGEXP_FILTER)) { LOG.atInfo() .addArgument(() -> logType) .addArgument(() -> formatter.format(log.getTimestamp())) .addArgument(() -> log.getMessage()) .log("{} {} {}"); } } } } @Override public void beforeAlertAccept(WebDriver driver) { // do nothing } @Override public void afterAlertAccept(WebDriver driver) { appendWebDriverLog(driver); } @Override public void afterAlertDismiss(WebDriver driver) { appendWebDriverLog(driver); } @Override public void beforeAlertDismiss(WebDriver driver) { // do nothing } @Override public void beforeNavigateTo(String url, WebDriver driver) { // do nothing } @Override public void afterNavigateTo(String url, WebDriver driver) { appendWebDriverLog(driver); } @Override public void beforeNavigateBack(WebDriver driver) { // do nothing } @Override public void afterNavigateBack(WebDriver driver) { appendWebDriverLog(driver); } @Override public void beforeNavigateForward(WebDriver driver) { // do nothing } @Override public void afterNavigateForward(WebDriver driver) { appendWebDriverLog(driver); } @Override public void beforeNavigateRefresh(WebDriver driver) { // do nothing } @Override public void afterNavigateRefresh(WebDriver driver) { appendWebDriverLog(driver); } @Override public void beforeFindBy(By by, WebElement element, WebDriver driver) { // do nothing } @Override public void afterFindBy(By by, WebElement element, WebDriver driver) { appendWebDriverLog(driver); } @Override public void beforeClickOn(WebElement element, WebDriver driver) { // do nothing } @Override public void afterClickOn(WebElement element, WebDriver driver) { appendWebDriverLog(driver); } @Override public void beforeChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) { // do nothing } @Override public void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) { appendWebDriverLog(driver); } @Override public void beforeScript(String script, WebDriver driver) { // do nothing } @Override public void afterScript(String script, WebDriver driver) { appendWebDriverLog(driver); } @Override public void beforeSwitchToWindow(String windowName, WebDriver driver) { // do nothing } @Override public void afterSwitchToWindow(String windowName, WebDriver driver) { appendWebDriverLog(driver); } @Override public void onException(Throwable throwable, WebDriver driver) { appendWebDriverLog(driver); } @Override public void beforeGetScreenshotAs(OutputType target) { // do nothing } @Override public void afterGetScreenshotAs(OutputType target, X screenshot) { // do nothing } @Override public void beforeGetText(WebElement element, WebDriver driver) { // do nothing } @Override public void afterGetText(WebElement element, WebDriver driver, String text) { appendWebDriverLog(driver); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy