-
Notifications
You must be signed in to change notification settings - Fork 19
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 #21 from Invictum/20_Ability_to_push_selenium_logs
Ability to push selenium logs
- Loading branch information
Showing
10 changed files
with
266 additions
and
16 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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/github/invictum/reportportal/EnhancedLogEntry.java
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,30 @@ | ||
package com.github.invictum.reportportal; | ||
|
||
import org.openqa.selenium.logging.LogEntry; | ||
|
||
import java.util.Map; | ||
import java.util.logging.Level; | ||
|
||
/** | ||
* Enhanced version of {@link LogEntry} with added type field | ||
*/ | ||
public class EnhancedLogEntry extends LogEntry { | ||
|
||
private String type; | ||
|
||
public EnhancedLogEntry(String type, Level level, long timestamp, String message) { | ||
super(level, timestamp, message); | ||
this.type = type; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> toJson() { | ||
Map<String, Object> json = super.toJson(); | ||
json.put("type", type); | ||
return json; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/github/invictum/reportportal/LogStorage.java
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,60 @@ | ||
package com.github.invictum.reportportal; | ||
|
||
import org.openqa.selenium.logging.Logs; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Central storage for collected logs provided by Selenium | ||
*/ | ||
public class LogStorage { | ||
|
||
private ThreadLocal<List<EnhancedLogEntry>> logs = ThreadLocal.withInitial(ArrayList::new); | ||
private ThreadLocal<Optional<Set<String>>> types = ThreadLocal.withInitial(Optional::empty); | ||
|
||
/** | ||
* Collects {@link org.openqa.selenium.WebDriver} logs if available and stores its to internal storage | ||
* | ||
* @param seleniumLogs to collect from | ||
*/ | ||
public void collect(Logs seleniumLogs) { | ||
// Discover available log types | ||
if (!types.get().isPresent()) { | ||
Set<String> discoveredTypes = seleniumLogs.getAvailableLogTypes(); | ||
types.set(Optional.of(discoveredTypes)); | ||
} | ||
// Collect all available logs | ||
types.get().ifPresent(types -> types.forEach(type -> { | ||
List<EnhancedLogEntry> typedLogs = seleniumLogs.get(type).getAll().stream() | ||
.map(log -> new EnhancedLogEntry(type, log.getLevel(), log.getTimestamp(), log.getMessage())) | ||
.collect(Collectors.toList()); | ||
logs.get().addAll(typedLogs); | ||
})); | ||
} | ||
|
||
/** | ||
* Clears all logs related to current thread | ||
*/ | ||
public void clean() { | ||
logs.remove(); | ||
types.remove(); | ||
} | ||
|
||
/** | ||
* Returns logs that meets defined condition and removes them from storage. | ||
* Logs could be queried only once | ||
* | ||
* @param predicate condition to use to find logs | ||
* @return a list of queried logs | ||
*/ | ||
public List<EnhancedLogEntry> query(Predicate<EnhancedLogEntry> predicate) { | ||
List<EnhancedLogEntry> result = logs.get().stream().filter(predicate).collect(Collectors.toList()); | ||
logs.get().removeAll(result); | ||
return result; | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/main/java/com/github/invictum/reportportal/processor/SeleniumLogsAttacher.java
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,45 @@ | ||
package com.github.invictum.reportportal.processor; | ||
|
||
import com.epam.reportportal.service.ReportPortal; | ||
import com.github.invictum.reportportal.EnhancedLogEntry; | ||
import com.github.invictum.reportportal.LogLevel; | ||
import com.github.invictum.reportportal.LogStorage; | ||
import com.github.invictum.reportportal.Utils; | ||
import com.github.invictum.reportportal.injector.IntegrationInjector; | ||
import net.thucydides.core.model.TestStep; | ||
|
||
import java.util.Date; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Attaches logs provided by Selenium | ||
*/ | ||
public class SeleniumLogsAttacher implements StepProcessor { | ||
|
||
private Predicate<EnhancedLogEntry> filter; | ||
|
||
public SeleniumLogsAttacher(Predicate<EnhancedLogEntry> filter) { | ||
this.filter = filter; | ||
} | ||
|
||
public SeleniumLogsAttacher() { | ||
this(log -> true); | ||
} | ||
|
||
@Override | ||
public void proceed(final TestStep step) { | ||
long start = Utils.stepEndDate(step).getTime(); | ||
long end = Utils.stepEndDate(step).getTime(); | ||
LogStorage storage = IntegrationInjector.getInjector().getInstance(LogStorage.class); | ||
storage.query(filter.and(entry -> start <= entry.getTimestamp() && entry.getTimestamp() >= end)) | ||
.forEach(log -> { | ||
String message = String.format("[Selenium-%s] [%s] %s", log.getType(), log.getLevel(), log.getMessage()); | ||
ReportPortal.emitLog(message, LogLevel.DEBUG.toString(), new Date(log.getTimestamp())); | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return obj instanceof SeleniumLogsAttacher; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/com/github/invictum/reportportal/EnhancedLogEntityTest.java
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,18 @@ | ||
package com.github.invictum.reportportal; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.util.logging.Level; | ||
|
||
@RunWith(JUnit4.class) | ||
public class EnhancedLogEntityTest { | ||
|
||
@Test | ||
public void toJsonTest() { | ||
EnhancedLogEntry entry = new EnhancedLogEntry("type", Level.INFO, 0, "Message"); | ||
Assert.assertTrue(entry.toJson().containsKey("type")); | ||
} | ||
} |
Oops, something went wrong.