In this blog we will be discovering the Keypress Functionality and will get started with API Testing in Playwright Java.
Topics that we will cover:
- Keypress feature
- Automate GET Request for status code 200
- Automate GET Request for status code 404
- Source code (keypress)
- Source code (GET Request status code 200)
- Source code (GET Request status code 404)
Keypress feature
Go to
https://the-internet.herokuapp.com/key_presses
When we press any key, the name of the same key would become visible. Press ‘Shift’ key, notice that we get the message “You entered: SHIFT”

Similarly, let us press ‘Caps Lock’

We will now explore the possibilities of automating this scenario.
Inspect the text area, it is located using “id”

We can make use of ‘press’ method to simulate pressing a key

By using the press method, we can simulate a variety of keys

n the same way, you can try other keys.
Automate GET Request for status code 200
Go to https://reqres.in/
Click GET request button displayed next to LIST USERS to retrieve the user list

Notice above response code 200 (200 meanins that the request was successfully processed by server).
We will now automate to validate the response code 200.
The url will be summation of “base url” and “uri”.
base url = https://reqres.in
request uri = /api/users?/page=2
The navigate statement than becomes:

Execute.
200 response code is printed

Automate GET Request for status code 404
We will proceed to test the API below, which has a response code of 404

Also notice that the response body is blank

Testing Get APIs can be done using this approach.
Source code (keyress)
package com.rsa.playwrightjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog13_keypress {
@Test
public void PlaywrightJTest() {
Playwright pt = Playwright.create();
LaunchOptions lo = new LaunchOptions();
lo.setChannel("msedge");
lo.setHeadless(false);
BrowserType btype = pt.chromium();
Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));
Page pg = b.newPage();
pg.navigate("https://the-internet.herokuapp.com/key_presses?");
pg.press("#target","F6");
pg.pause();
}
}
Source code (GET Request status code 200)
package com.rsa.playwrightjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.Response;
public class Blog13_StatusCode200 {
@Test
public void PlaywrightJTest() {
Playwright pt = Playwright.create();
LaunchOptions lo = new LaunchOptions();
lo.setChannel("msedge");
lo.setHeadless(false);
BrowserType btype = pt.chromium();
Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));
Page pg = b.newPage();
Response resp = pg.navigate("https://reqres.in/api/users?page=2");
System.out.println(resp.status());
pg.pause();
}
}
Source code (GET Request status code 404)
package com.rsa.playwrightjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.Response;
public class Blog13_StatusCode404 {
@Test
public void PlaywrightJTest() {
Playwright pt = Playwright.create();
LaunchOptions lo = new LaunchOptions();
lo.setChannel("msedge");
lo.setHeadless(false);
BrowserType btype = pt.chromium();
Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));
Page pg = b.newPage();
Response resp = pg.navigate("https://reqres.in/api/unknown/23");
System.out.println(resp.status());
pg.pause();
}
}
Thanks.