Mastering Query Parameters in Playwright Java API Tests

HOME

https://jsonplaceholder.typicode.com/comments?postId=1

<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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>Playwright_API_Demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Playwright_API_Demo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <playwright.version>1.57.0</playwright.version>
    <testng.version>7.11.0</testng.version>
    <maven.compiler.plugin.version>3.15.0</maven.compiler.plugin.version>
    <maven.compiler.source.version>17</maven.compiler.source.version>
    <maven.compiler.target.version>17</maven.compiler.target.version>
    <maven.surefire.plugin.version>3.5.4</maven.surefire.plugin.version>
  </properties>

  <dependencies>

    <!-- Playwright -->
    <dependency>
      <groupId>com.microsoft.playwright</groupId>
      <artifactId>playwright</artifactId>
      <version>${playwright.version}</version>
    </dependency>

    <!-- TestNG -->
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>${testng.version}</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven.compiler.plugin.version}</version>
        <configuration>
          <source>${maven.compiler.source.version}</source>
          <target>${maven.compiler.target.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven.surefire.plugin.version}</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
        <dependencies>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

 @BeforeMethod
    void setUp() {
        createPlaywright();
        createAPIRequestContext();
    }

 void createPlaywright() {
        playwright = Playwright.create();
    }

void createAPIRequestContext() {
        Map<String, String> headers = new HashMap<>();
        headers.put("Accept", "application/json");

        request = playwright.request().newContext(new APIRequest.NewContextOptions()
                .setBaseURL("https://jsonplaceholder.typicode.com")
                .setExtraHTTPHeaders(headers));
    }

  @AfterMethod
    void tearDown() {
        if (request != null) {
            request.dispose();
        }
        if (playwright != null) {
            playwright.close();
        }
    }

 @Test
    public void getCommentsByPostId() {

        // GET /comments?postId=1
        APIResponse response = request.get("/comments",
                RequestOptions.create().setQueryParam("postId", "1"));

        Assert.assertEquals(response.status(), 200, "Expected 200 for GET /comments?postId=1");

        String body = response.text();
        System.out.println("GET Response: " + body);

        Gson gson = new Gson();
        JsonObject[] posts = gson.fromJson(body, JsonObject[].class);

        Assert.assertTrue(posts.length > 0, "Expected at least one post for postId=1");
        Assert.assertEquals(posts[0].get("postId").getAsInt(), 1, "Expected postId=1 in first result");
        Assert.assertEquals(posts[0].get("id").getAsInt(), 1, "Expected id=1 in first result");
        Assert.assertEquals(posts[0].get("email").getAsString(), "Eliseo@gardner.biz", "Expected email is incorrect for first result" + posts[0].get("email").getAsString());
    }

APIResponse response = request.get("/comments",
                RequestOptions.create().setQueryParam("postId", "1"));
Assert.assertEquals(response.status(), 200, "Expected 200 for GET /comments?postId=1");
String body = response.text();
System.out.println("GET Response: " + body);
Gson gson = new Gson();
JsonObject[] posts = gson.fromJson(body, JsonObject[].class);
Assert.assertTrue(posts.length > 0, "Expected at least one post for postId=1");

Verifies that the `postId` in the first comment object is `1`.

Assert.assertEquals(posts[0].get("postId").getAsInt(), 1, "Expected postId=1 in first result");

mvn clean test