add playwright api test demo from playwright docs
This commit is contained in:
parent
9a98767b67
commit
43e81c921c
28
pom.xml
28
pom.xml
@ -92,7 +92,6 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.cucumber</groupId>
|
<groupId>io.cucumber</groupId>
|
||||||
<artifactId>cucumber-junit-platform-engine</artifactId>
|
<artifactId>cucumber-junit-platform-engine</artifactId>
|
||||||
@ -121,6 +120,21 @@
|
|||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>dev</id>
|
||||||
|
<properties>
|
||||||
|
<cucumber.filter.tags>@dev</cucumber.filter.tags>
|
||||||
|
</properties>
|
||||||
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>qa</id>
|
||||||
|
<properties>
|
||||||
|
<cucumber.filter.tags>@qa</cucumber.filter.tags>
|
||||||
|
</properties>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@ -133,7 +147,19 @@
|
|||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.0.0-M7</version>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<!--suppress UnresolvedMavenProperty -->
|
||||||
|
<cucumber.filter.tags>${cucumber.filter.tags}</cucumber.filter.tags>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
146
src/test/java/org/example/apitests/GithubExampleApiTests.java
Normal file
146
src/test/java/org/example/apitests/GithubExampleApiTests.java
Normal file
@ -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<String, String> 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<String, String> 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<String, String> 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());
|
||||||
|
}
|
||||||
|
}
|
63
src/test/java/org/example/utils/ApiContext.java
Normal file
63
src/test/java/org/example/utils/ApiContext.java
Normal file
@ -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<String, String> 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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.example.steps;
|
package org.example.utils;
|
||||||
|
|
||||||
import com.microsoft.playwright.Browser;
|
import com.microsoft.playwright.Browser;
|
||||||
import com.microsoft.playwright.BrowserType;
|
import com.microsoft.playwright.BrowserType;
|
||||||
@ -22,7 +22,8 @@ public class TestContext {
|
|||||||
playwright.webkit().launch(new BrowserType.LaunchOptions().setHeadless(headless));
|
playwright.webkit().launch(new BrowserType.LaunchOptions().setHeadless(headless));
|
||||||
case "chromium": browser =
|
case "chromium": browser =
|
||||||
playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(headless));
|
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();
|
default: browser = playwright.webkit().launch();
|
||||||
}
|
}
|
||||||
page = browser.newPage();
|
page = browser.newPage();
|
@ -1,4 +1,4 @@
|
|||||||
package org.example.runner;
|
package org.example.webtests.runner;
|
||||||
|
|
||||||
import io.cucumber.junit.Cucumber;
|
import io.cucumber.junit.Cucumber;
|
||||||
import io.cucumber.junit.CucumberOptions;
|
import io.cucumber.junit.CucumberOptions;
|
||||||
@ -6,8 +6,8 @@ import org.junit.runner.RunWith;
|
|||||||
|
|
||||||
@RunWith(Cucumber.class)
|
@RunWith(Cucumber.class)
|
||||||
@CucumberOptions(
|
@CucumberOptions(
|
||||||
features = "src/test/java/org/example/features/",
|
features = "src/test/java/org/example/webtests/features/",
|
||||||
glue = {"org.example.steps"},
|
glue = {"org.example.webtests.steps"},
|
||||||
plugin = {"pretty"},
|
plugin = {"pretty"},
|
||||||
publish = true
|
publish = true
|
||||||
)
|
)
|
@ -1,7 +1,6 @@
|
|||||||
package org.example.steps;
|
package org.example.webtests.steps;
|
||||||
|
|
||||||
import com.microsoft.playwright.Page;
|
import com.microsoft.playwright.Page;
|
||||||
import io.cucumber.java.AfterAll;
|
|
||||||
import io.cucumber.java.Before;
|
import io.cucumber.java.Before;
|
||||||
import io.cucumber.java.After;
|
import io.cucumber.java.After;
|
||||||
import io.cucumber.java.en.Given;
|
import io.cucumber.java.en.Given;
|
||||||
@ -9,6 +8,7 @@ import io.cucumber.java.en.Then;
|
|||||||
import io.cucumber.java.en.When;
|
import io.cucumber.java.en.When;
|
||||||
import org.example.pages.Home;
|
import org.example.pages.Home;
|
||||||
import org.example.pages.Login;
|
import org.example.pages.Login;
|
||||||
|
import org.example.utils.TestContext;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
|
||||||
public class LoginSteps {
|
public class LoginSteps {
|
||||||
@ -23,7 +23,6 @@ public class LoginSteps {
|
|||||||
login = new Login(page);
|
login = new Login(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Given("User has a functional browser")
|
@Given("User has a functional browser")
|
||||||
public void userHasAFunctionalBrowser() {
|
public void userHasAFunctionalBrowser() {
|
||||||
Assert.assertEquals("webkit", testContext.browser.browserType().name());
|
Assert.assertEquals("webkit", testContext.browser.browserType().name());
|
Loading…
Reference in New Issue
Block a user