-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added listener before stop test case (#67)
Co-authored-by: Dmitry.Gridnev <[email protected]>
- Loading branch information
Showing
18 changed files
with
244 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version=1.1.5 | ||
version=1.1.6 | ||
|
||
org.gradle.daemon=true | ||
org.gradle.parallel=true | ||
|
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
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
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
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 |
---|---|---|
@@ -1,7 +1,41 @@ | ||
# How to enable debug logging? | ||
Create **simplelogger.properties** file in the resource directory of the project: | ||
``` | ||
```text | ||
org.slf4j.simpleLogger.defaultLogLevel=debug | ||
org.slf4j.simpleLogger.showDateTime=true | ||
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd'T'HH:mm:ss.SSSZ | ||
``` | ||
|
||
# How to add an attachment for a failed test? | ||
You need to implement the AdapterListener interface and override the beforeTestStop method. | ||
For example: | ||
|
||
```java | ||
import ru.testit.services.Adapter; | ||
import ru.testit.listener.AdapterListener; | ||
import ru.testit.models.ItemStatus; | ||
import ru.testit.models.TestResult; | ||
import ru.testit.services.Adapter; | ||
|
||
public class AttachmentManager implements AdapterListener { | ||
|
||
@Override | ||
public void beforeTestStop(final TestResult result) { | ||
if (result.getItemStatus().equals(ItemStatus.FAILED)) { | ||
// Add a screenshot | ||
Adapter.addAttachments("Screenshot.jpg", new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES))); | ||
|
||
// Add log file | ||
Adapter.addAttachments("/logs/failed.log"); | ||
|
||
// Add any text | ||
Adapter.addAttachments("any text", "file.txt"); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
After that, you need to add file **ru.testit.listener.AdapterListener** to **resources/META-INT/services** folder: | ||
```text | ||
<your-package>.AttachmentManager | ||
``` |
9 changes: 9 additions & 0 deletions
9
testit-java-commons/src/main/java/ru/testit/listener/AdapterListener.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,9 @@ | ||
package ru.testit.listener; | ||
|
||
import ru.testit.models.TestResult; | ||
|
||
public interface AdapterListener extends DefaultListener { | ||
default void beforeTestStop(final TestResult result) { | ||
//do nothing | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
testit-java-commons/src/main/java/ru/testit/listener/DefaultListener.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,4 @@ | ||
package ru.testit.listener; | ||
|
||
public interface DefaultListener { | ||
} |
34 changes: 34 additions & 0 deletions
34
testit-java-commons/src/main/java/ru/testit/listener/ListenerManager.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,34 @@ | ||
package ru.testit.listener; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import ru.testit.models.TestResult; | ||
|
||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
|
||
public class ListenerManager { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ListenerManager.class); | ||
private List<AdapterListener> listeners; | ||
|
||
public ListenerManager(final List<AdapterListener> listeners) { | ||
|
||
this.listeners = listeners; | ||
} | ||
|
||
public void beforeTestStop(final TestResult result) { | ||
runSafelyMethod(listeners, AdapterListener::beforeTestStop, result); | ||
} | ||
|
||
protected <T extends DefaultListener, S> void runSafelyMethod(final List<T> listeners, | ||
final BiConsumer<T, S> method, | ||
final S object) { | ||
listeners.forEach(listener -> { | ||
try { | ||
method.accept(listener, object); | ||
} catch (Exception e) { | ||
LOGGER.error("Could not invoke listener method", e); | ||
} | ||
}); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
testit-java-commons/src/main/java/ru/testit/listener/ServiceLoaderListener.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,41 @@ | ||
package ru.testit.listener; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
|
||
public final class ServiceLoaderListener { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceLoaderListener.class); | ||
|
||
private ServiceLoaderListener() { | ||
throw new IllegalStateException("Do not have instance"); | ||
} | ||
|
||
public static <T> List<T> load(final Class<T> type, final ClassLoader classLoader) { | ||
final List<T> loaded = new ArrayList<>(); | ||
final Iterator<T> iterator = ServiceLoader.load(type, classLoader).iterator(); | ||
while (nextSafely(iterator)) { | ||
try { | ||
final T next = iterator.next(); | ||
loaded.add(next); | ||
LOGGER.debug("Found type {}", type); | ||
} catch (Exception e) { | ||
LOGGER.error("Could not load listener {}: {}", type, e); | ||
} | ||
} | ||
return loaded; | ||
} | ||
|
||
private static boolean nextSafely(final Iterator iterator) { | ||
try { | ||
return iterator.hasNext(); | ||
} catch (Exception e) { | ||
LOGGER.error("nextSafely failed", e); | ||
return false; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.