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

com.github.siwenyan.common.ScenarioLogger Maven / Gradle / Ivy

There is a newer version: 1.25.a
Show newest version
package com.github.siwenyan.common;

import com.github.siwenyan.pickle.GlueManager;
import com.github.siwenyan.web.WebUtils;
import cucumber.api.Scenario;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;

public class ScenarioLogger {

    private final Scenario scenario;
    private final Logger logger;
    private WebDriver webDriver;

    public ScenarioLogger(Scenario scenario, Logger logger, WebDriver webDriver) {
        this.scenario = scenario;
        this.logger = logger;
        this.webDriver = webDriver;
    }

    public void setDriver(WebDriver webDriver) {
        GlueManager.immediateAbortCheck(scenario);
        this.webDriver = webDriver;
    }

    public void warn(String message) {
        try {
            logger.warn(message);
            scenario.write("\r\nWARN: " + message);
        } catch (Exception e) {
            System.out.println(message);
        }
    }

    public void error(String message) {
        try {
            logger.error(message);
            scenario.write("\r\nERROR: " + message);
            tryScreenshot(makeName(message, 20, scenario));
            WebUtils.demoBreakPoint(scenario, webDriver, makeName(message, 20, scenario));
        } catch (Exception e) {
            System.out.println(message);
        }
    }

    public String getOwner() {
        try {
            return this.scenario.getId();
        } catch (Exception e) {
            return null;
        }
    }

    public Scenario getScenario() {
        return this.scenario;
    }

    public void write(String s) {
        logger.warn(s);
        scenario.write(s);
    }

    public void highlight(String message) {
        try {
            logger.warn(message);
            scenario.write("\r\nHIGHLIGHT: " + message);
            tryScreenshot(makeName(message, 20, scenario));
        } catch (Exception e) {
            System.out.println(message);
        }
    }

    private String makeName(String s, int limit, Scenario scenario) {
        s = s.replaceAll("[^A-Za-z0-9]+", " ").trim().replace(' ', '_');
        s = s.length() > limit ? s.substring(0, limit) : s;
        return null == scenario ? "" : (makeName(scenario.getName(), 20, null) + "_") + DateTimeTools.timestamp() + "_" + s;
    }

    private void tryScreenshot(String name) {
        if ("true".equalsIgnoreCase(Conf.getInstance().getProperty("ScenarioLogger.takeScreenshot"))) {
            WebUtils.demoBreakPoint(this.scenario, this.webDriver, name);
        } else {
            String message = "[Placeholder for screenshot: " + name + "]";
            logger.warn(message);
            scenario.write(message);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy