This commit is contained in:
Athou
2024-08-07 08:10:14 +02:00
parent 2f6ddf0e70
commit cc32f8ad16
164 changed files with 2011 additions and 3288 deletions

View File

@@ -1,20 +1,35 @@
package com.commafeed.e2e;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import com.commafeed.CommaFeedDropwizardAppExtension;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.assertions.PlaywrightAssertions;
import com.microsoft.playwright.options.AriaRole;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import io.quarkus.test.junit.QuarkusTest;
@ExtendWith(DropwizardExtensionsSupport.class)
class AuthentificationIT extends PlaywrightTestBase {
@QuarkusTest
class AuthentificationIT {
private static final CommaFeedDropwizardAppExtension EXT = new CommaFeedDropwizardAppExtension();
private final Playwright playwright = Playwright.create();
private final Browser browser = playwright.chromium().launch();
private Page page;
@BeforeEach
void init() {
page = browser.newContext().newPage();
}
@AfterEach
void cleanup() {
playwright.close();
}
@Test
void loginFail() {
@@ -29,7 +44,7 @@ class AuthentificationIT extends PlaywrightTestBase {
void loginSuccess() {
page.navigate(getLoginPageUrl());
PlaywrightTestUtils.login(page);
PlaywrightAssertions.assertThat(page).hasURL("http://localhost:" + EXT.getLocalPort() + "/#/app/category/all");
PlaywrightAssertions.assertThat(page).hasURL("http://localhost:8085/#/app/category/all");
}
@Test
@@ -56,10 +71,10 @@ class AuthentificationIT extends PlaywrightTestBase {
page.getByPlaceholder("E-mail address").fill("user@domain.com");
page.getByPlaceholder("Password").fill("MyPassword1!");
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Sign up")).click();
PlaywrightAssertions.assertThat(page).hasURL("http://localhost:" + EXT.getLocalPort() + "/#/app/category/all");
PlaywrightAssertions.assertThat(page).hasURL("http://localhost:8085/#/app/category/all");
}
private String getLoginPageUrl() {
return "http://localhost:" + EXT.getLocalPort() + "/#/login";
return "http://localhost:8085/#/login";
}
}

View File

@@ -1,140 +0,0 @@
package com.commafeed.e2e;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Browser.NewContextOptions;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.Tracing;
/**
* Base class for all Playwright tests.
*
* <ul>
* <li>Takes a screenshot on failure</li>
* <li>Keeps the video on failure</li>
* <li>Saves a trace file on failure</li>
* </ul>
*
* inspired by https://github.com/microsoft/playwright-java/issues/503#issuecomment-872636373
*
*/
@ExtendWith(PlaywrightTestBase.SaveArtifactsOnTestFailed.class)
public abstract class PlaywrightTestBase {
private static Playwright playwright;
private static Browser browser;
protected Page page;
private BrowserContext context;
@BeforeAll
static void initBrowser() {
playwright = Playwright.create();
browser = playwright.chromium().launch();
}
@AfterAll
static void closeBrowser() {
playwright.close();
}
protected void customizeNewContextOptions(NewContextOptions options) {
// override in subclasses to customize the browser context
}
protected static class SaveArtifactsOnTestFailed implements TestWatcher, BeforeEachCallback {
// defined in the config of maven-failsafe-plugin in pom.xml
private final String buildDirectory = System.getProperty("buildDirectory", "target");
private final String directory = buildDirectory + "/playwright-artifacts";
@Override
public void beforeEach(ExtensionContext context) {
PlaywrightTestBase testInstance = getTestInstance(context);
NewContextOptions newContextOptions = new Browser.NewContextOptions().setRecordVideoDir(Paths.get(directory));
testInstance.customizeNewContextOptions(newContextOptions);
testInstance.context = PlaywrightTestBase.browser.newContext(newContextOptions);
testInstance.context.tracing().start(new Tracing.StartOptions().setScreenshots(true).setSnapshots(true));
testInstance.page = testInstance.context.newPage();
}
@Override
public void testFailed(ExtensionContext context, Throwable cause) {
PlaywrightTestBase testInstance = getTestInstance(context);
String fileName = getFileName(context);
saveScreenshot(testInstance, fileName);
saveTrace(testInstance, fileName);
testInstance.context.close();
saveVideo(testInstance, fileName);
}
@Override
public void testAborted(ExtensionContext context, Throwable cause) {
PlaywrightTestBase testInstance = getTestInstance(context);
testInstance.context.close();
testInstance.page.video().delete();
}
@Override
public void testDisabled(ExtensionContext context, Optional<String> reason) {
PlaywrightTestBase testInstance = getTestInstance(context);
testInstance.context.close();
testInstance.page.video().delete();
}
@Override
public void testSuccessful(ExtensionContext context) {
PlaywrightTestBase testInstance = getTestInstance(context);
testInstance.context.close();
testInstance.page.video().delete();
}
private PlaywrightTestBase getTestInstance(ExtensionContext context) {
return (PlaywrightTestBase) context.getRequiredTestInstance();
}
private String getFileName(ExtensionContext context) {
return String.format("%s.%s-%s", context.getRequiredTestClass().getSimpleName(), context.getRequiredTestMethod().getName(),
DateTimeFormatter.ofPattern("yyyy-MM-dd--HH-mm-ss").format(ZonedDateTime.now()));
}
private void saveScreenshot(PlaywrightTestBase testInstance, String fileName) {
byte[] screenshot = testInstance.page.screenshot();
try {
Files.write(Paths.get(directory, fileName + ".png"), screenshot);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void saveTrace(PlaywrightTestBase testInstance, String fileName) {
testInstance.context.tracing().stop(new Tracing.StopOptions().setPath(Paths.get(directory, fileName + ".zip")));
}
private void saveVideo(PlaywrightTestBase testInstance, String fileName) {
testInstance.page.video().saveAs(Paths.get(directory, fileName + ".webm"));
testInstance.page.video().delete();
}
}
}

View File

@@ -6,43 +6,51 @@ import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockserver.client.MockServerClient;
import org.mockserver.junit.jupiter.MockServerExtension;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import com.commafeed.CommaFeedDropwizardAppExtension;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.assertions.PlaywrightAssertions;
import com.microsoft.playwright.options.AriaRole;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import io.quarkus.test.junit.QuarkusTest;
@ExtendWith(DropwizardExtensionsSupport.class)
@ExtendWith(MockServerExtension.class)
class ReadingIT extends PlaywrightTestBase {
@QuarkusTest
class ReadingIT {
private static final CommaFeedDropwizardAppExtension EXT = new CommaFeedDropwizardAppExtension();
private final Playwright playwright = Playwright.create();
private final Browser browser = playwright.chromium().launch();
private Page page;
private MockServerClient mockServerClient;
@BeforeEach
void init(MockServerClient mockServerClient) throws IOException {
this.mockServerClient = mockServerClient;
void init() throws IOException {
this.page = browser.newContext().newPage();
this.mockServerClient = ClientAndServer.startClientAndServer(0);
this.mockServerClient.when(HttpRequest.request().withMethod("GET"))
.respond(HttpResponse.response()
.withBody(IOUtils.toString(getClass().getResource("/feed/rss.xml"), StandardCharsets.UTF_8))
.withDelay(TimeUnit.MILLISECONDS, 100));
}
@AfterEach
void cleanup() {
playwright.close();
}
@Test
void scenario() {
// login
page.navigate("http://localhost:" + EXT.getLocalPort());
page.navigate("http://localhost:8085");
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Log in")).click();
PlaywrightTestUtils.login(page);