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

io.github.selcukes.extent.report.Reporter Maven / Gradle / Ivy

There is a newer version: 2.3.12
Show newest version
/*
 *
 *  Copyright (c) Ramesh Babu Prudhvi.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package io.github.selcukes.extent.report;

import io.github.selcukes.commons.logging.LogRecordListener;
import io.github.selcukes.commons.logging.LoggerFactory;
import io.github.selcukes.snapshot.Snapshot;
import io.github.selcukes.snapshot.SnapshotImpl;
import org.openqa.selenium.WebDriver;

import java.util.logging.Level;
import java.util.stream.Collectors;

import static io.github.selcukes.databind.utils.StringHelper.isNullOrEmpty;
import static io.github.selcukes.databind.utils.StringHelper.nullOrEmpty;

public class Reporter {
    private static final ThreadLocal reporterThreadLocal = new InheritableThreadLocal<>();
    private Snapshot snapshot;
    private LogRecordListener logRecordListener;

    public static void log(String message) {
        if (!isNullOrEmpty(message)) {
            SelcukesExtentAdapter.addTestStepLog(message);
        }
    }

    public static Reporter getReporter() {
        if (reporterThreadLocal.get() == null) {
            reporterThreadLocal.set(new Reporter());
        }
        return reporterThreadLocal.get();
    }

    Reporter start() {
        logRecordListener = new LogRecordListener();
        LoggerFactory.addListener(logRecordListener);
        return this;
    }

    public void initSnapshot(WebDriver driver) {
        snapshot = new SnapshotImpl(driver);
    }

    public String getLogRecords() {
        if (logRecordListener != null) {
            return logRecordListener.getLogRecords()
                    .filter(logRecord -> logRecord.getLevel() == Level.INFO || logRecord.getLevel() == Level.SEVERE)
                    .map(logRecord -> {
                        if (logRecord.getLevel() == Level.SEVERE) {
                            return "" + logRecord.getMessage().replace("\n", "
") + "
"; } else { return logRecord.getMessage(); } }) .filter(nullOrEmpty.negate()) .collect(Collectors.joining("
  • ", "
    • ", "

    ")); } return ""; } private Reporter attachLog() { String infoLogs = getLogRecords(); if (!infoLogs.equalsIgnoreCase("

    ")) { Reporter.log(infoLogs); } return this; } private Reporter stop() { if (logRecordListener != null) { LoggerFactory.removeListener(logRecordListener); } return this; } void removeReporter() { reporterThreadLocal.remove(); } void attachAndClear() { attachLog().stop(); } void attachAndRestart() { attachLog().stop().start(); } public void attachScreenshot() { SelcukesExtentAdapter.attachScreenshot(snapshot.shootPageAsBytes()); } }




  • © 2015 - 2024 Weber Informatics LLC | Privacy Policy