Skip to content

Commit

Permalink
Merge pull request #287 from giulong/feature/next-release
Browse files Browse the repository at this point in the history
Feature/next release
  • Loading branch information
giulong authored Nov 11, 2024
2 parents 4b36fa7 + f473651 commit ee61944
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ All you need to do is take the three steps listed below, as shown in this video:
https://github.com/giulong/spectrum/assets/27963644/fecee8f5-f6dc-4b47-81a3-514e66b3907d

> ⚠️ JDK<br/>
> Since Spectrum is compiled with a jdk 21, you need a [jdk 21+](https://jdk.java.net/21/) to be able to run your tests.
> Since Spectrum is compiled with a jdk 21, you need a [jdk 21+](https://jdk.java.net/archive/) to be able to run your tests.
1. Generate a new project leveraging the [Spectrum Archetype](https://mvnrepository.com/artifact/io.github.giulong/spectrum-archetype) via your IDE or by running this in a terminal
and providing values when prompted:
Expand Down
14 changes: 7 additions & 7 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ and **mobile and desktop applications** via [Appium](http://appium.io/docs/en/la
## Setup

> ⚠️ **JDK**<br/>
> Since Spectrum is compiled with a jdk 21, you need a [jdk 21+](https://jdk.java.net/21/){:target="_blank"} to be able to run your tests.
> Since Spectrum is compiled with a jdk 21, you need a [jdk 21+](https://jdk.java.net/archive/){:target="_blank"} to be able to run your tests.
> If you get an `Unsupported major.minor version` exception, the reason is that you're using an incompatible java version.
### Spectrum Archetype
Expand Down Expand Up @@ -1232,7 +1232,7 @@ Given [Spectrum intercepts web driver's events](#webdriver-events-listener), it
such as `beforeSendKeys` and `afterSendKeys`, send sensitive data to logs and html report in plain text.

To avoid this, you just need to decorate the sensitive web elements with `@Secured`, and the sensitive data will be redacted
with `[***]`. The replacement will only affect events' consumers such as logs and html report,
with `[***]`. The replacement will only affect events' consumers such as logs and html report,
of course the actual value will still be sent to or read from the web element.

{% include copyCode.html %}
Expand Down Expand Up @@ -1313,11 +1313,11 @@ Spectrum
with an events listener used to automatically take actions such as logging and generating reports.
You can tweak each event in your `configuration.yaml`, by providing these:

| Key | Type | Default | Description |
|---------|---------------------------------------------------------------------------------------------|---------|-----------------------------------------------------|
| level | [Level](https://logback.qos.ch/apidocs/ch/qos/logback/classic/Level.html){:target="_blank"} | `OFF` | Level at which this event will be logged |
| message | String | `null` | Message to be logged upon receiving this event |
| wait | long | `0` | Milliseconds to wait before listening to this event |
| Key | Type | Default | Description |
|---------|---------------------------------------------------------------------------------|---------|-----------------------------------------------------|
| level | [Level](https://www.slf4j.org/api/org/slf4j/event/Level.html){:target="_blank"} | `null` | Level at which this event will be logged |
| message | String | `null` | Message to be logged upon receiving this event |
| wait | long | `0` | Milliseconds to wait before listening to this event |

For example, you can set these:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.model.Media;
import io.github.giulong.spectrum.interfaces.Shared;
import io.github.giulong.spectrum.utils.*;
import io.github.giulong.spectrum.types.TestData;
import io.github.giulong.spectrum.utils.*;
import io.github.giulong.spectrum.utils.events.EventsDispatcher;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -17,12 +17,10 @@
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.List;

import static com.aventstack.extentreports.MediaEntityBuilder.createScreenCaptureFromPath;
import static com.aventstack.extentreports.Status.*;
Expand Down Expand Up @@ -85,13 +83,6 @@ public abstract class SpectrumEntity<T extends SpectrumEntity<T, Data>, Data> {
@Shared
JsWebElementProxyBuilder jsWebElementProxyBuilder;

List<Field> getSharedFields() {
return Arrays
.stream(SpectrumEntity.class.getDeclaredFields())
.filter(f -> f.isAnnotationPresent(Shared.class))
.toList();
}

/**
* Hovers on the provided WebElement, leveraging the {@code actions} field
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public T waitForPageLoading() {
*/
public boolean isLoaded() {
final String currentUrl = driver.getCurrentUrl();
final String pageUrl = configuration.getApplication().getBaseUrl() + endpoint;
final String pageUrl = String.format("%s/%s", configuration.getApplication().getBaseUrl(), endpoint.replaceFirst("/", ""));
log.debug("Current url: {}", currentUrl);
log.debug("Page url: {}", pageUrl);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.giulong.spectrum.extensions.interceptors.SpectrumInterceptor;
import io.github.giulong.spectrum.extensions.resolvers.*;
import io.github.giulong.spectrum.extensions.watchers.EventsWatcher;
import io.github.giulong.spectrum.interfaces.Shared;
import io.github.giulong.spectrum.types.*;
import io.github.giulong.spectrum.utils.*;
import io.github.giulong.spectrum.utils.events.EventsDispatcher;
Expand Down Expand Up @@ -106,7 +107,7 @@ void beforeEach(final TestContext testContext, final Configuration configuration
final Class<?> clazz = this.getClass();
log.debug("Initializing pages of test '{}'", clazz.getSimpleName());

final List<Field> sharedFields = getSharedFields();
final List<Field> sharedFields = Reflections.getAnnotatedFields(SpectrumEntity.class, Shared.class);

return Reflections
.getFieldsOf(clazz, SpectrumTest.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@

import static io.github.giulong.spectrum.enums.Frame.AUTO_AFTER;
import static io.github.giulong.spectrum.enums.Frame.AUTO_BEFORE;
import static org.slf4j.event.Level.*;

@Slf4j
@Builder
public class SpectrumWebDriverListener implements WebDriverListener {

private static final Map<String, Level> LEVELS = Map.of("TRACE", TRACE, "INFO", INFO, "WARN", WARN);

private Pattern locatorPattern;
private Events events;
private List<Consumer<WebDriverEvent>> consumers;
Expand All @@ -59,9 +56,9 @@ protected List<String> parse(final Object[] args) {

@SneakyThrows
protected void listenTo(final Frame frame, final Configuration.Drivers.Event event, final Object... args) {
final Level level = LEVELS.getOrDefault(event.getLevel().levelStr, DEBUG);
final Level level = event.getLevel();

if (!log.isEnabledForLevel(level)) {
if (level == null || !log.isEnabledForLevel(level)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;
import java.util.logging.Level;

import static ch.qos.logback.classic.Level.OFF;
import static lombok.AccessLevel.PRIVATE;

@SuppressWarnings("unused")
Expand Down Expand Up @@ -499,8 +498,8 @@ public static class Events {
public static class Event {

@JsonPropertyDescription("Level at which this event will be logged")
@JsonSchemaTypes(value = String.class, valueList = {"OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", "ALL"})
private final ch.qos.logback.classic.Level level = OFF;
@JsonSchemaTypes(value = String.class, valueList = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"})
private org.slf4j.event.Level level;

@JsonPropertyDescription("Message to be logged upon receiving this event")
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,22 @@ public static void copyField(final Field field, final Object source, final Objec
field.set(dest, field.get(source));
}

public static List<Field> getAnnotatedFields(final Object object, final Class<? extends Annotation> annotation) {
final String className = object.getClass().getTypeName();
public static List<Field> getAnnotatedFields(final Class<?> clazz, final Class<? extends Annotation> annotation) {
final String className = clazz.getTypeName();
final String annotationName = annotation.getTypeName();

return Arrays
.stream(object.getClass().getDeclaredFields())
.stream(clazz.getDeclaredFields())
.filter(f -> f.isAnnotationPresent(annotation))
.peek(f -> log.debug("Field {}.{} is annotated with {}", className, f.getName(), annotationName))
.peek(f -> f.setAccessible(true))
.toList();
}

public static List<Field> getAnnotatedFields(final Object object, final Class<? extends Annotation> annotation) {
return getAnnotatedFields(object.getClass(), annotation);
}

public static <T> List<T> getAnnotatedFieldsValues(final Object object, final Class<? extends Annotation> annotation, final Class<T> clazz) {
return getAnnotatedFields(object, annotation)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ drivers:
beforeGetSize:
message: Getting size of <code>%1$s</code>
afterGetSize:
level: OFF
message: Size of <code>%1$s</code> is <code>%2$s</code>
beforeGetCssValue:
message: Getting css property <code>%2$s</code> of element <code>%1$s</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.model.Media;
import io.github.giulong.spectrum.interfaces.Shared;
import io.github.giulong.spectrum.types.TestData;
import io.github.giulong.spectrum.utils.Configuration;
import io.github.giulong.spectrum.utils.Reflections;
import io.github.giulong.spectrum.utils.StatefulExtentTest;
import lombok.SneakyThrows;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -115,9 +117,9 @@ private void addScreenshotToReportStubs() {
}

@Test
@DisplayName("getSharedFields should return the list of fields of SpectrumEntity.class that are annotated with @Shared")
@DisplayName("checking shared fields")
void getSharedFields() {
final List<Field> actual = spectrumEntity.getSharedFields();
final List<Field> actual = Reflections.getAnnotatedFields(SpectrumEntity.class, Shared.class);
final List<String> sharedFieldsNames = actual
.stream()
.map(Field::getName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

class SpectrumPageTest {

private final String endpoint = "/endpoint";

private MockedStatic<JsWebElementListInvocationHandler> jsWebElementListInvocationHandlerMockedStatic;

@Mock
Expand Down Expand Up @@ -85,6 +83,7 @@ void afterEach() {
@DisplayName("open should get the configured base url and wait for the page to be loaded")
void open() {
final String url = "url";
final String endpoint = "/endpoint";
Reflections.setField("endpoint", spectrumPage, endpoint);

when(configuration.getApplication()).thenReturn(application);
Expand All @@ -101,23 +100,24 @@ void waitForPageLoading() {
}

@DisplayName("isLoaded should check if the current page url matches the endpoint")
@ParameterizedTest(name = "with page url {0} and current url {1} we expect {2}")
@ParameterizedTest(name = "with base url {0}, endpoint {1}, and current url {2} we expect {3}")
@MethodSource("valuesProvider")
void isLoaded(final String pageUrl, final String currentUrl, final boolean expected) {
void isLoaded(final String baseUrl, final String endpoint, final String currentUrl, final boolean expected) {
Reflections.setField("endpoint", spectrumPage, endpoint);

//noinspection DataFlowIssue
when(webDriver.getCurrentUrl()).thenReturn(currentUrl + endpoint);
when(webDriver.getCurrentUrl()).thenReturn(currentUrl);
when(configuration.getApplication()).thenReturn(application);
when(application.getBaseUrl()).thenReturn(pageUrl);
when(application.getBaseUrl()).thenReturn(baseUrl);

assertEquals(expected, spectrumPage.isLoaded());
}

static Stream<Arguments> valuesProvider() {
return Stream.of(
arguments("current", "current", true),
arguments("page", "current", false)
arguments("baseUrl", "/endpoint", "baseUrl/endpoint", true),
arguments("baseUrl", "endpoint", "baseUrl/endpoint", true),
arguments("baseUrl", "/endpoint", "nope", false)
);
}

Expand Down
Loading

0 comments on commit ee61944

Please sign in to comment.