Skip to content

Commit

Permalink
Merge pull request #37 from Goodie01/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Goodie01 authored Jun 29, 2024
2 parents 3229dce + ae2132a commit 5f59eaa
Show file tree
Hide file tree
Showing 20 changed files with 300 additions and 256 deletions.
4 changes: 2 additions & 2 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>nz.geek.goodwin.melinoe</groupId>
<artifactId>examples</artifactId>
<version>0.3.4-SNAPSHOT</version>
<version>0.4.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
Expand Down Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>nz.geek.goodwin.melinoe</groupId>
<artifactId>framework</artifactId>
<version>0.3.4-SNAPSHOT</version>
<version>0.4.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import nz.geek.goodwin.melinoe.framework.api.Session;
import nz.geek.goodwin.melinoe.framework.api.web.BasePage;
import nz.geek.goodwin.melinoe.framework.api.web.By;
import nz.geek.goodwin.melinoe.framework.api.web.FindElement;
import nz.geek.goodwin.melinoe.framework.api.web.WebElement;
import nz.geek.goodwin.melinoe.framework.api.web.validation.TitleValidator;
import nz.geek.goodwin.melinoe.framework.api.web.validation.WebElementText;
import nz.geek.goodwin.melinoe.framework.api.web.validation.WebValidator;

import java.util.List;
import java.util.stream.Collectors;

public class GithubRepoPage extends BasePage {
@FindElement(linkText = "Pull requests")
private WebElement pullRequestLink;
Expand All @@ -19,7 +18,7 @@ public class GithubRepoPage extends BasePage {

public GithubRepoPage(final Session session) {
super(session,
TitleValidator.equals("GitHub - Goodie01/Melinoe: Melinoe is named for the greek mythological figure who is known as a \"bringer of nightmares and madness\", this seems apt for a automated testing framework"));
WebValidator.titleEquals("GitHub - Goodie01/Melinoe: Melinoe is named for the greek mythological figure who is known as a \"bringer of nightmares and madness\", this seems apt for a automated testing framework"));
//
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.goodiemania.melinoe.samples.github;

import nz.geek.goodwin.melinoe.framework.api.web.BasePage;
import nz.geek.goodwin.melinoe.framework.api.Session;
import nz.geek.goodwin.melinoe.framework.api.web.validation.TitleValidator;
import nz.geek.goodwin.melinoe.framework.api.web.BasePage;
import nz.geek.goodwin.melinoe.framework.api.web.validation.WebValidator;

public class GithubRepoPullRequestPage extends BasePage {
public GithubRepoPullRequestPage(final Session session) {
super(session, TitleValidator.equals("Pull requests · Goodie01/Melinoe · GitHub"));
super(session, WebValidator.titleEquals("Pull requests · Goodie01/Melinoe · GitHub"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import nz.geek.goodwin.melinoe.framework.api.web.BasePage;
import nz.geek.goodwin.melinoe.framework.api.web.FindElement;
import nz.geek.goodwin.melinoe.framework.api.web.WebElement;
import nz.geek.goodwin.melinoe.framework.api.web.validation.TitleValidator;
import nz.geek.goodwin.melinoe.framework.api.web.validation.WebValidator;

/**
* @author Goodie
Expand All @@ -14,7 +14,7 @@ public class MozillaHomepage extends BasePage {
private WebElement manifestoLink;

public MozillaHomepage(Session session) {
super(session, TitleValidator.equals("Internet for people, not profit — Mozilla (US)"));
super(session, WebValidator.titleEquals("Internet for people, not profit — Mozilla (US)"));
}

public void clickManifestoLink() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import nz.geek.goodwin.melinoe.framework.api.web.By;
import nz.geek.goodwin.melinoe.framework.api.web.FindElement;
import nz.geek.goodwin.melinoe.framework.api.web.WebElement;
import nz.geek.goodwin.melinoe.framework.api.web.validation.TitleValidator;
import nz.geek.goodwin.melinoe.framework.api.web.validation.WebElementText;
import nz.geek.goodwin.melinoe.framework.api.web.validation.WebValidator;

/**
* @author Goodie
Expand All @@ -16,7 +16,7 @@ public class MozillaManifestopage extends BasePage {
private WebElement manifestoSubTitle;

public MozillaManifestopage(Session session) {
super(session, TitleValidator.equals("The Mozilla Manifesto"),
super(session, WebValidator.titleEquals("The Mozilla Manifesto"),
WebElementText.equals(By.cssSelector(".addendum-subtitle"), "Pledge for a Healthy Internet"));

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.goodiemania.melinoe.samples.standard;

import com.fasterxml.jackson.annotation.JsonProperty;
import nz.geek.goodwin.melinoe.framework.api.Session;
import nz.geek.goodwin.melinoe.framework.api.rest.validation.RestValidator;
import nz.geek.goodwin.melinoe.framework.internal.MelinoeExtension;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -20,7 +20,44 @@ public void run() {
session = Session.create();
session.rest().get("https://1.1.1.1/dns-query?name=cloudflare.com")
.header("accept", "application/dns-json")
.result()
.verify(List.of(RestValidator.statusCodeEquals(200)));
.verifyAsString(List.of(RestValidator.statusCodeEquals(200)));

session.rest().get("https://1.1.1.1/dns-query?name=cloudflare.com")
.header("accept", "application/dns-json")
.verify(Root.class, List.of(RestValidator.statusCodeEquals(200)));

}

public static class Answer {
public String name;
public int type;
@JsonProperty("TTL")
public int tTL;
public String data;
}

public static class Question {
public String name;
public int type;
}

public static class Root {
@JsonProperty("Status")
public int status;
@JsonProperty("TC")
public boolean tC;
@JsonProperty("RD")
public boolean rD;
@JsonProperty("RA")
public boolean rA;
@JsonProperty("AD")
public boolean aD;
@JsonProperty("CD")
public boolean cD;
@JsonProperty("Question")
public List<Question> question;
@JsonProperty("Answer")
public List<Answer> answer;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public class FailingTests {
private Session session;
private MozillaHomepage mozillaHomepage;

@Test
@DisplayName("Run, checking for a page and being on the wrong page")
public void runWithBadTitle() {
Expand All @@ -26,6 +27,7 @@ public void runWithBadTitle() {

mozillaHomepage.checkPage();
}

@Test
@DisplayName("Run, dont't validate page")
public void runWithOutChecking() {
Expand All @@ -49,7 +51,6 @@ public void run() {
session = Session.create();
session.rest().get("https://1.1.1.1/dns-query?name=cloudflare.com")
.header("accept", "application/dns-json")
.result()
.verify(List.of(RestValidator.statusCodeEquals(201)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import nz.geek.goodwin.melinoe.framework.internal.MelinoeExtension;
import org.goodiemania.melinoe.samples.mozilla.MozillaHomepage;
import org.goodiemania.melinoe.samples.mozilla.MozillaManifestopage;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -18,6 +14,7 @@ public class MozillaTest {
private MozillaHomepage mozillaHomepage;

private MozillaManifestopage mozillaManifestopage;

@Test
@DisplayName("Run")
public void run() {
Expand Down
2 changes: 1 addition & 1 deletion framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>nz.geek.goodwin.melinoe</groupId>
<artifactId>framework</artifactId>
<version>0.3.4-SNAPSHOT</version>
<version>0.4.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nz.geek.goodwin.melinoe.framework.api;

import nz.geek.goodwin.melinoe.framework.api.rest.HttpResult;
import nz.geek.goodwin.melinoe.framework.api.web.validation.ValidationResult;

/**
* @author Goodie
*/
public interface Validator<T> {
ValidationResult validate(final T result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import nz.geek.goodwin.melinoe.framework.api.rest.validation.RestValidator;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

/**
* @author Goodie
Expand All @@ -14,12 +19,21 @@ public interface HttpRequest {
HttpRequest body(Object objectBody);

HttpRequest body(String body);

HttpResult<String> resultAsString();

HttpResult<JsonNode> result();

<T> HttpResult<T> result(Class<T> tClass);

<T> HttpResult<T> result(TypeReference<T> typeReference);
HttpResult<String> verifyAsString(List<RestValidator<String>> validators);
HttpResult<JsonNode> verify(List<RestValidator<JsonNode>> validators);
<T> HttpResult<T> verify(TypeReference<T> tClass, List<RestValidator<T>> validators);
<T> HttpResult<T> verify(Class<T> tClass, List<RestValidator<T>> validators);

default HttpResult<String> verifyAsString(RestValidator<String>... validators) {
return verifyAsString(List.of(validators));
}
default HttpResult<JsonNode> verify(RestValidator<JsonNode>... validators) {
return verify(List.of(validators));
}
default <T> HttpResult<T> verify(TypeReference<T> tClass, RestValidator<T>... validators) {
return verify(tClass, List.of(validators));
}
default <T> HttpResult<T> verify(Class<T> tClass, RestValidator<T>... validators) {
return verify(tClass, List.of(validators));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,4 @@ public interface HttpResult<T> {
Map<String, List<String>> respHeaders();

T respBody();

default HttpResult<T> verify(final RestValidator<T>... validators) {
return verify(Arrays.asList(validators));
}

HttpResult<T> verify(List<RestValidator<T>> validators);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package nz.geek.goodwin.melinoe.framework.api.rest.validation;

import nz.geek.goodwin.melinoe.framework.api.Validator;
import nz.geek.goodwin.melinoe.framework.api.rest.HttpResult;
import nz.geek.goodwin.melinoe.framework.api.web.validation.ValidationResult;

/**
* @author Goodie
*/
public interface RestValidator<T> {
public interface RestValidator<T> extends Validator<HttpResult<T>> {
ValidationResult validate(final HttpResult<T> result);

static <T> RestValidator<T> statusCodeEquals(int statusCode) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
package nz.geek.goodwin.melinoe.framework.api.web.validation;

import nz.geek.goodwin.melinoe.framework.api.Validator;
import nz.geek.goodwin.melinoe.framework.api.web.WebDriver;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.function.ToBooleanBiFunction;

/**
* @author Goodie
*/
public interface WebValidator {
public interface WebValidator extends Validator<WebDriver> {
ValidationResult validate(final WebDriver webDriver);

static WebValidator titleEquals(String title) {
return titleImpl(StringUtils::equals, title);
}

static WebValidator titleContains(String title) {
return titleImpl(StringUtils::contains, title);
}

private static WebValidator titleImpl(ToBooleanBiFunction<String, String> comparisonType, String expectedTitle) {
return webDriver -> {
String actualTitle = webDriver.getTitle();
if (comparisonType.applyAsBoolean(actualTitle, expectedTitle)) {
return ValidationResult.passed("Found expected title: " + expectedTitle);
} else {
return ValidationResult.failed("Title is not as expected",
String.format("Expected title: \"%s\"", expectedTitle),
String.format("Actual title: \"%s\"", actualTitle));
}
};
}
}
Loading

0 comments on commit 5f59eaa

Please sign in to comment.