generating logs) code example

Example: how to generate logs

1. Insert 'log4j' dependency into pom.xml file.

2. Create 'log4j.properties' file under src/main/resources. 

3. Add required codes in it as below:
	log4j.rootLogger=DEBUG, Appender1,Appender2
	log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
	log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
	log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
	
	log4j.appender.Appender2=org.apache.log4j.FileAppender
	log4j.appender.Appender2.File=execution_log.txt
	log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
	log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n 

4. It will generate execution_log.txt file which
shows each single step second by second with
message what we put into logger method. 
It is not a report, it only logs
all the steps of our test execution.

5. Create a Logger instance in Driver class.
    private static Logger logger = Logger.getLogger(Driver.class);

6. Create a getter method to use it in all classes.
	public static Logger getLogger(){
		return logger;
	}

7. Use this static method in any class to log any action. 
    Driver.getLogger().info("Comparing usernames");

Exp-1: You can use under Driver.
It generates a log after each browser creation
	
	public static WebDriver get() {
        if (driverPool.get() == null) {
            Driver.getLogger().info("TRYING TO CREATE DRIVER"); ==> generates log after each browser creation
            String browserParamFromEnv = System.getProperty("browser");
            String browser = browserParamFromEnv == null ? ConfigurationReader.getProperty("browser") : browserParamFromEnv;
            switch (browser) {
                case "chrome":
                    WebDriverManager.chromedriver().setup();
                    driverPool.set(new ChromeDriver());
                    break;


Exp-2: It logs the error message which comes
from exception in Driver class
	
	case "remote_chrome":
        try {
            DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
            desiredCapabilities.setBrowserName(BrowserType.CHROME);
            desiredCapabilities.setCapability("platform", Platform.ANY);
            driverPool.set(new RemoteWebDriver(new URL("hub url here"), desiredCapabilities));
        } catch (Exception e) {
            Driver.getLogger().error(e.getMessage()); ==> logs error message which comes from exception
            e.printStackTrace();
        }
    	break;

Exp-3: It can also log each step of the test execution.
Just put a simple logger.info("your message is here")
    @Given("user is on the login page")
    public void user_is_on_the_login_page() {
        Driver.getLogger.info("I am on the login page");
        Driver.get().get(ConfigurationReader.getProperty("url"));
    }

    @Then("user logs in as store manager")
    public void user_logs_in_as_store_manager() {
        Driver.getLogger.info("Login as store manager");
        String userName = ConfigurationReader.getProperty("user_name");
        String password = ConfigurationReader.getProperty("password");
        loginPage.login(userName, password);
    }

Tags:

Misc Example