add playwright api test demo from playwright docs

This commit is contained in:
Greg Gauthier 2023-01-26 16:02:12 +00:00
parent 9a98767b67
commit 43e81c921c
7 changed files with 370 additions and 135 deletions

280
pom.xml
View File

@ -1,139 +1,165 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId> <groupId>org.example</groupId>
<artifactId>PlaywrightJava1</artifactId> <artifactId>PlaywrightJava1</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<distributionManagement> <distributionManagement>
<site> <site>
<id>website</id> <id>website</id>
<url>scp://webhost.company.com/www/website</url> <url>scp://webhost.company.com/www/website</url>
</site> </site>
</distributionManagement> </distributionManagement>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>7.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>io.cucumber</groupId> <!-- *********************************** JUNIT5 **************************** -->
<artifactId>cucumber-bom</artifactId> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<version>7.10.1</version> <dependency>
<type>pom</type> <groupId>org.junit.jupiter</groupId>
<scope>import</scope> <artifactId>junit-jupiter-api</artifactId>
</dependency> <version>5.9.2</version>
<dependency> <scope>test</scope>
<groupId>org.junit</groupId> </dependency>
<artifactId>junit-bom</artifactId>
<version>5.9.2</version> <!-- *************************** PLAYWRIGHT **************************** -->
<type>pom</type> <!-- https://mvnrepository.com/artifact/com.microsoft.playwright/playwright -->
<scope>import</scope> <dependency>
</dependency> <groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.29.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/assertions -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>assertions</artifactId>
<version>1.17.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/driver -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>driver</artifactId>
<version>1.29.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/driver-bundle -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>driver-bundle</artifactId>
<version>1.29.0</version>
</dependency>
<!-- *********************** CUCUMBER ******************************* -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.opentest4j/opentest4j -->
<dependency>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement>
<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>
<!-- *********************************** JUNIT5 **************************** --> <build>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <plugins>
<dependency> <plugin>
<groupId>org.junit.jupiter</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>junit-jupiter-api</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>5.9.2</version> <version>3.10.1</version>
<scope>test</scope> <configuration>
</dependency> <source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</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>
<!-- *************************** PLAYWRIGHT **************************** --> </build>
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/playwright -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.29.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/assertions -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>assertions</artifactId>
<version>1.17.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/driver -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>driver</artifactId>
<version>1.29.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/driver-bundle -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>driver-bundle</artifactId>
<version>1.29.0</version>
</dependency>
<!-- *********************** CUCUMBER ******************************* -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.opentest4j/opentest4j -->
<dependency>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View 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());
}
}

View 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();
}
}

View File

@ -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();

View File

@ -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
) )

View File

@ -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());