Handle Radio buttons using Playwright Java

In this blog we will be utilizing Playwright Java to handle radio buttons.


Topics that we will cover:

  • “check()” method to select a radio button
  • “locator() method clubbed with check()” method to select a radio button
  • Use the “click()” function to pick a radio button
  • “locator() method clubbed with click()” method to select a radio button
  • Source code

“check()” method to select a radio button 

Go to https://rahulshettyacademy.com/AutomationPractice/ 

Observe below that none of the radio buttons are selected by default

 Let us see how to select a radio button, for example ‘Radio1’.

Let us inspect ‘Radio1’

 Let us use “check()” method to select the radio button and pass the ‘value’ attribute (line#29)

 Run the script. 

Notice below that “Radio1” gets selected

 “locator() method clubbed with check()” method to select a radio button

We can also use the “locator()” method clubbed with “check()” method to select a radio button 

 Use the “click()” function to pick a radio button

We can also use the “click()” method to select a radio button (see line#35)

Execute.
Notice below that ‘Radio3’ is selected

 “locator() method clubbed with click()” method to select a radio button

We can also use the “locator()” method clubbed with “click()” method to select a radio button

 Execute. 

Notice below that ‘Radio1’ is selected

 So this is how we can use the various methods to select a radio button.

Source code

package com.rsa.playwrightjava;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Locator;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;

public
class Blog32_RadioButtons_PWJava {

private Browser browser;

private Page page;

@BeforeMethod

public
void setUp() {

Playwright playwright = Playwright.create(); 

browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

page = browser.newPage();

}

@Test

public
void test_1() {

page.navigate(“https://rahulshettyacademy.com/AutomationPractice/”);

//Method 1

page.check(“[value=’radio1′]”);

//Method 2

page.locator(“[value=’radio2′]”).check();

//Method 3

page.click(“[value=’radio3′]”);

//Method 4

page.locator(“[value=’radio1′]”).click();

}

@AfterMethod

public
void tearDown() {

page.pause();

browser.close();

page.close();

}

}

Thanks.