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

Topics that we will cover:

  • check()” method to select a checkbox
  • locator() method clubbed with check()” method to select a checkbox
  • Use the "click()" function to pick a checkbox
  • uncheck()” method to uncheck a checkbox
  • locator() method clubbed with click()” method to select a checkbox
  • Source code

check()” method to select a checkbox 

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

Observe below that none of the checkboxes are selected by default

 Let us see how to select a checkbox, for example ‘Option1’.

Let us inspect ‘Option1’

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

locator() method clubbed with check()” method to select a checkbox

We can also use the “locator()” method clubbed with “check()” method to select a checkbox (line#32) 

 Use the "click()" function to pick a checkbox

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

 uncheck()” method to uncheck a checkbox

We can uncheck a checkbox using “uncheck()” method

 locator() method clubbed with click()” method to select a checkbox

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

 

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

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 Blog33_Checkboxes_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("[id='checkBoxOption1']");

page.uncheck("[id='checkBoxOption1']");

//Method 2

page.locator("[id='checkBoxOption2']").check();

//Method 3

page.click("[id='checkBoxOption3']");

//Method 4

page.locator("[id='checkBoxOption1']").click();

}

@AfterMethod

public void tearDown() {

page.pause();

browser.close();

page.close();

}

}


Thanks.


Tags


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}