-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from ThePalaceProject/refactoring
Refactoring alerts and logger
- Loading branch information
Showing
7 changed files
with
160 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package framework.utilities; | ||
|
||
import aquality.appium.mobile.application.AqualityServices; | ||
import org.apache.log4j.PatternLayout; | ||
import org.apache.log4j.RollingFileAppender; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
public class Logger { | ||
|
||
private static ThreadLocal<Logger> instance = ThreadLocal.withInitial(Logger::new); | ||
private RollingFileAppender scenarioAppender; | ||
|
||
private Logger() { | ||
} | ||
|
||
public static Logger getInstance() { | ||
return instance.get(); | ||
} | ||
|
||
public void upload() { | ||
instance.remove(); | ||
} | ||
|
||
|
||
public void createAppender(String name) { | ||
PatternLayout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n"); | ||
try { | ||
scenarioAppender = new RollingFileAppender( | ||
layout, String.format("%s%s.log", "target/log/", name.replace(",", "").replace(" ", "_")), false); | ||
AqualityServices.getLogger().addAppender(scenarioAppender); | ||
} catch (IOException exception) { | ||
AqualityServices.getLogger().error("Failed to add appender !! " + exception.getMessage()); | ||
} | ||
} | ||
|
||
public byte[] getLoggerInfoBytes() { | ||
byte[] data = new byte[0]; | ||
try { | ||
data = Files.readAllBytes(Paths.get(scenarioAppender.getFile())); | ||
} catch (IOException exception) { | ||
AqualityServices.getLogger().error(exception.toString()); | ||
} | ||
return data; | ||
} | ||
|
||
public void removeAppender() { | ||
if (scenarioAppender != null) { | ||
AqualityServices.getLogger().removeAppender(scenarioAppender); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package hooks; | ||
|
||
import aquality.appium.mobile.application.AqualityServices; | ||
import framework.utilities.Logger; | ||
import io.cucumber.core.backend.TestCaseState; | ||
import io.cucumber.java.*; | ||
import io.cucumber.plugin.event.PickleStepTestStep; | ||
import io.cucumber.plugin.event.TestCase; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class LoggerHooks { | ||
|
||
private static ThreadLocal<Integer> counter = ThreadLocal.withInitial(() -> 0); | ||
|
||
@Before(order = 0) | ||
public void startScenarioLogger(Scenario scenario) { | ||
Logger logger = Logger.getInstance(); | ||
AqualityServices.getLogger().info("isLoggerNullBeforeAnnotation-"+logger); | ||
logger.createAppender(scenario.getName()); | ||
AqualityServices.getLogger().info(format("Scenario '%s' start", scenario.getName())); | ||
AqualityServices.getLogger().info("Session id - " + AqualityServices.getApplication().getDriver().getSessionId().toString()); | ||
} | ||
|
||
@After(order = 2) | ||
public void addLogToScenario(Scenario scenario) { | ||
Logger logger = Logger.getInstance(); | ||
AqualityServices.getLogger().info("isLoggerNullAfterAnnotation-"+logger); | ||
byte[] data = logger.getLoggerInfoBytes(); | ||
AqualityServices.getLogger().info(format("Scenario '%s' end", scenario.getName())); | ||
scenario.attach(data, "text/plain", "log.txt"); | ||
Logger.getInstance().removeAppender(); | ||
} | ||
|
||
@BeforeStep | ||
public void getStepName(Scenario scenario) throws Exception { | ||
Field delegateField = scenario.getClass().getDeclaredField("delegate"); | ||
delegateField.setAccessible(true); | ||
TestCaseState testCaseState = (TestCaseState) delegateField.get(scenario); | ||
|
||
Field testCaseField = testCaseState.getClass().getDeclaredField("testCase"); | ||
testCaseField.setAccessible(true); | ||
TestCase r1 = (TestCase) testCaseField.get(testCaseState); | ||
|
||
List<PickleStepTestStep> stepDefinitions = r1.getTestSteps() | ||
.stream() | ||
.filter(x -> x instanceof PickleStepTestStep) | ||
.map(x -> (PickleStepTestStep) x) | ||
.collect(Collectors.toList()); | ||
|
||
PickleStepTestStep currentStep = stepDefinitions.get(counter.get()); | ||
|
||
AqualityServices.getLogger().info(String.format("Step %d - %s", counter.get(), currentStep.getStep().getText())); | ||
} | ||
|
||
@AfterStep | ||
public void afterStep(Scenario scenario) { | ||
counter.set(counter.get() + 1); | ||
} | ||
|
||
@After | ||
public void afterTest(Scenario scenario) { | ||
counter.set(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.