diff --git a/pom.xml b/pom.xml index cd8db72..9c29090 100644 --- a/pom.xml +++ b/pom.xml @@ -1,139 +1,165 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.example - PlaywrightJava1 - 1.0-SNAPSHOT - pom + org.example + PlaywrightJava1 + 1.0-SNAPSHOT + pom - - - website - scp://webhost.company.com/www/website - - + + + website + scp://webhost.company.com/www/website + + - - UTF-8 - + + UTF-8 + + + + + + io.cucumber + cucumber-bom + 7.10.1 + pom + import + + + org.junit + junit-bom + 5.9.2 + pom + import + + + - - - io.cucumber - cucumber-bom - 7.10.1 - pom - import - - - org.junit - junit-bom - 5.9.2 - pom - import - + + + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 + test + + + + + + com.microsoft.playwright + playwright + 1.29.0 + + + + + com.microsoft.playwright + assertions + 1.17.2 + + + + + com.microsoft.playwright + driver + 1.29.0 + + + + + com.microsoft.playwright + driver-bundle + 1.29.0 + + + + + io.cucumber + cucumber-java + test + + + + + io.cucumber + cucumber-junit + 7.11.0 + test + + + + io.cucumber + cucumber-junit-platform-engine + test + + + + io.cucumber + cucumber-picocontainer + test + + + + + com.google.code.gson + gson + 2.8.9 + + + + + org.opentest4j + opentest4j + 1.2.0 + + - - + + + dev + + @dev + + + + qa + + @qa + + + - - - - org.junit.jupiter - junit-jupiter-api - 5.9.2 - test - + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + 1.8 + 1.8 + UTF-8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M7 + + + + ${cucumber.filter.tags} + + + + - - - - com.microsoft.playwright - playwright - 1.29.0 - - - - - com.microsoft.playwright - assertions - 1.17.2 - - - - - com.microsoft.playwright - driver - 1.29.0 - - - - - com.microsoft.playwright - driver-bundle - 1.29.0 - - - - - io.cucumber - cucumber-java - test - - - - - io.cucumber - cucumber-junit - 7.11.0 - test - - - - - io.cucumber - cucumber-junit-platform-engine - test - - - - io.cucumber - cucumber-picocontainer - test - - - - - com.google.code.gson - gson - 2.8.9 - - - - - org.opentest4j - opentest4j - 1.2.0 - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.10.1 - - 1.8 - 1.8 - UTF-8 - - - - + diff --git a/src/test/java/org/example/apitests/GithubExampleApiTests.java b/src/test/java/org/example/apitests/GithubExampleApiTests.java new file mode 100644 index 0000000..bd589f3 --- /dev/null +++ b/src/test/java/org/example/apitests/GithubExampleApiTests.java @@ -0,0 +1,146 @@ +package org.example.apitests; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import com.microsoft.playwright.APIRequest; +import com.microsoft.playwright.APIRequestContext; +import com.microsoft.playwright.APIResponse; +import com.microsoft.playwright.Playwright; +import com.microsoft.playwright.options.RequestOptions; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class GithubExampleApiTests { + private static final String REPO = "test-repo-2"; + private static final String USER = System.getenv("GITHUB_USER"); + private static final String API_TOKEN = System.getenv("GITHUB_API_TOKEN"); + + private Playwright playwright; + private APIRequestContext request; + + void createPlaywright() { + playwright = Playwright.create(); + } + + void createAPIRequestContext() { + Map headers = new HashMap<>(); + // We set this header per GitHub guidelines. + headers.put("Accept", "application/vnd.github.v3+json"); + // Add authorization token to all requests. + // Assuming personal access token available in the environment. + headers.put("Authorization", "token " + API_TOKEN); + + request = playwright.request().newContext(new APIRequest.NewContextOptions() + // All requests we send go to this API endpoint. + .setBaseURL("https://api.github.com") + .setExtraHTTPHeaders(headers)); + } + + void createTestRepository() { + APIResponse newRepo = request.post("/user/repos", + RequestOptions.create().setData(Collections.singletonMap("name", REPO))); + assertTrue(newRepo.ok(), newRepo.text()); + } + + @BeforeAll + void beforeAll() { + createPlaywright(); + createAPIRequestContext(); + createTestRepository(); + } + + void deleteTestRepository() { + if (request != null) { + APIResponse deletedRepo = request.delete("/repos/" + USER + "/" + REPO); + assertTrue(deletedRepo.ok()); + } + } + + void disposeAPIRequestContext() { + if (request != null) { + request.dispose(); + request = null; + } + } + + void closePlaywright() { + if (playwright != null) { + playwright.close(); + playwright = null; + } + } + + @AfterAll + void afterAll() { + deleteTestRepository(); + disposeAPIRequestContext(); + closePlaywright(); + } + + @Test + void shouldCreateBugReport() { + Map data = new HashMap<>(); + data.put("title", "[Bug] report 1"); + data.put("body", "Bug description"); + APIResponse newIssue = request.post("/repos/" + USER + "/" + REPO + "/issues", + RequestOptions.create().setData(data)); + assertTrue(newIssue.ok()); + + APIResponse issues = request.get("/repos/" + USER + "/" + REPO + "/issues"); + assertTrue(issues.ok()); + JsonArray json = new Gson().fromJson(issues.text(), JsonArray.class); + JsonObject issue = null; + for (JsonElement item : json) { + JsonObject itemObj = item.getAsJsonObject(); + if (!itemObj.has("title")) { + continue; + } + if ("[Bug] report 1".equals(itemObj.get("title").getAsString())) { + issue = itemObj; + break; + } + } + assertNotNull(issue); + assertEquals("Bug description", issue.get("body").getAsString(), issue.toString()); + } + + @Test + void shouldCreateFeatureRequest() { + Map data = new HashMap<>(); + data.put("title", "[Feature] request 1"); + data.put("body", "Feature description"); + APIResponse newIssue = request.post("/repos/" + USER + "/" + REPO + "/issues", + RequestOptions.create().setData(data)); + assertTrue(newIssue.ok()); + + APIResponse issues = request.get("/repos/" + USER + "/" + REPO + "/issues"); + assertTrue(issues.ok()); + JsonArray json = new Gson().fromJson(issues.text(), JsonArray.class); + JsonObject issue = null; + for (JsonElement item : json) { + JsonObject itemObj = item.getAsJsonObject(); + if (!itemObj.has("title")) { + continue; + } + if ("[Feature] request 1".equals(itemObj.get("title").getAsString())) { + issue = itemObj; + break; + } + } + assertNotNull(issue); + assertEquals("Feature description", issue.get("body").getAsString(), issue.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/org/example/utils/ApiContext.java b/src/test/java/org/example/utils/ApiContext.java new file mode 100644 index 0000000..bbb274d --- /dev/null +++ b/src/test/java/org/example/utils/ApiContext.java @@ -0,0 +1,63 @@ +package org.example.utils; + +import com.microsoft.playwright.APIRequest; +import com.microsoft.playwright.APIRequestContext; +import com.microsoft.playwright.Playwright; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.TestInstance; + +import java.util.HashMap; +import java.util.Map; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class ApiContext { + private static final String API_TOKEN = System.getenv("GITHUB_API_TOKEN"); + + private Playwright playwright; + private APIRequestContext request; + + void createPlaywright() { + playwright = Playwright.create(); + } + + void createAPIRequestContext() { + Map headers = new HashMap<>(); + // We set this header per GitHub guidelines. + headers.put("Accept", "application/vnd.github.v3+json"); + // Add authorization token to all requests. + // Assuming personal access token available in the environment. + headers.put("Authorization", "token " + API_TOKEN); + + request = playwright.request().newContext(new APIRequest.NewContextOptions() + // All requests we send go to this API endpoint. + .setBaseURL("https://api.github.com") + .setExtraHTTPHeaders(headers)); + } + + @BeforeAll + void beforeAll() { + createPlaywright(); + createAPIRequestContext(); + } + + void disposeAPIRequestContext() { + if (request != null) { + request.dispose(); + request = null; + } + } + + void closePlaywright() { + if (playwright != null) { + playwright.close(); + playwright = null; + } + } + + @AfterAll + void afterAll() { + disposeAPIRequestContext(); + closePlaywright(); + } +} \ No newline at end of file diff --git a/src/test/java/org/example/steps/TestContext.java b/src/test/java/org/example/utils/TestContext.java similarity index 87% rename from src/test/java/org/example/steps/TestContext.java rename to src/test/java/org/example/utils/TestContext.java index cb8bac9..952a4a0 100644 --- a/src/test/java/org/example/steps/TestContext.java +++ b/src/test/java/org/example/utils/TestContext.java @@ -1,4 +1,4 @@ -package org.example.steps; +package org.example.utils; import com.microsoft.playwright.Browser; import com.microsoft.playwright.BrowserType; @@ -22,7 +22,8 @@ public class TestContext { playwright.webkit().launch(new BrowserType.LaunchOptions().setHeadless(headless)); case "chromium": browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(headless)); - case "firefox": browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(headless)); + case "firefox": browser = + playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(headless)); default: browser = playwright.webkit().launch(); } page = browser.newPage(); diff --git a/src/test/java/org/example/features/login.feature b/src/test/java/org/example/webtests/features/login.feature similarity index 100% rename from src/test/java/org/example/features/login.feature rename to src/test/java/org/example/webtests/features/login.feature diff --git a/src/test/java/org/example/runner/DemoRunner.java b/src/test/java/org/example/webtests/runner/DemoRunner.java similarity index 61% rename from src/test/java/org/example/runner/DemoRunner.java rename to src/test/java/org/example/webtests/runner/DemoRunner.java index 8b81d47..08b4dd2 100644 --- a/src/test/java/org/example/runner/DemoRunner.java +++ b/src/test/java/org/example/webtests/runner/DemoRunner.java @@ -1,4 +1,4 @@ -package org.example.runner; +package org.example.webtests.runner; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; @@ -6,8 +6,8 @@ import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( - features = "src/test/java/org/example/features/", - glue = {"org.example.steps"}, + features = "src/test/java/org/example/webtests/features/", + glue = {"org.example.webtests.steps"}, plugin = {"pretty"}, publish = true ) diff --git a/src/test/java/org/example/steps/LoginSteps.java b/src/test/java/org/example/webtests/steps/LoginSteps.java similarity index 96% rename from src/test/java/org/example/steps/LoginSteps.java rename to src/test/java/org/example/webtests/steps/LoginSteps.java index 8eb561a..6f7d8bc 100644 --- a/src/test/java/org/example/steps/LoginSteps.java +++ b/src/test/java/org/example/webtests/steps/LoginSteps.java @@ -1,7 +1,6 @@ -package org.example.steps; +package org.example.webtests.steps; import com.microsoft.playwright.Page; -import io.cucumber.java.AfterAll; import io.cucumber.java.Before; import io.cucumber.java.After; import io.cucumber.java.en.Given; @@ -9,6 +8,7 @@ import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import org.example.pages.Home; import org.example.pages.Login; +import org.example.utils.TestContext; import org.junit.Assert; public class LoginSteps { @@ -23,7 +23,6 @@ public class LoginSteps { login = new Login(page); } - @Given("User has a functional browser") public void userHasAFunctionalBrowser() { Assert.assertEquals("webkit", testContext.browser.browserType().name());