Tutorial 16 – Keypress and GET Request API testing in Playwright Java
What you will Learn in this blog:
Keypress in playwright-java
Automate API GET Request (status code 200)
Automate API GET Request (status code 404)
Code snippets
Keypress in Playwright-Java
Launch https://the-internet.herokuapp.com/key_presses
Bring cursor in the highlighted text area and press ‘Tab’ key. We notice the text ‘You Entered: TAB’ written below the text field
Let us see how we can automate this use case.
Let us inspect the tet area, notice that it can be located using the ‘id #target’
Playwright has ‘press’ method that we can use to simulate pressing a key
These are some of the keys that we can in with the method
We will identify ‘key Presses’ using its text (line#18)
We then select the text area using its css selector #target and call the ‘F1’ key (line#19)
Save and execute.
Notice below that F1 key was pressed
Similarly, we can try with other keys.
Automate API GET Request (status code 200)
Launch https://reqres.in/
Click GET request shown against SINGLE USER.
Notice that the response is 200 (viz the GET request was successfully processed by the server)
Also notice the ‘Request’ uri that is shown above viz /api/users/2.
Let us see how to validate the response status using PW.
Our url is composed of ‘base url’ plus ‘uri’
So our script looks like below
Save and execute.
Notice the console showing status code 200
Automate API GET Request (status code 404)
Let us now test below api having response code of 404
Below would be our script
Save and execute.
Notice the console showing status code 404
This is how we test Get APIs.
Refer https://playwright.dev/java/docs/api/class-response further for official documentation on API
Code snippet (keyress)
package com.w2a.pwjava;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class KeypressPW {
public static void main(String[] args) {
Playwright pw = Playwright.create();
Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
BrowserContext browserContext = browser.newContext();
Page page = browserContext.newPage();
page.navigate("https://the-internet.herokuapp.com/");
page.locator("text=Key Presses").click();
page.press("#target","F1");
}
}
Code snippet (api get request)
package com.w2a.pwjava;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.Response;
public class APIGetReqPW {
public static void main(String[] args) {
Playwright pw = Playwright.create();
Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
BrowserContext browserContext = browser.newContext();
Page page = browserContext.newPage();
/*
//for status 200
Response response = page.navigate("https://reqres.in/api/users/2");
System.out.println(response.status());
*/
//for status 404
Response response = page.navigate("https://reqres.in/api/users/23");
System.out.println(response.status());
}
}
Thank you for reading!