In this blog we will be utilizing Playwright Java to assert checkboxes and we will also see the usage of blur() and clear() methods.

Topics that we will cover:

  • Assert checkboxes (using assertThat() method)
  • Assert if checkbox is visible
  • Usage of blur() method
  • Usage of clear() method
  • Source code (assert checkboxes)
  • Source code (blur, clear)

Assert checkboxes (using assertThat() method)

We will now see how to assert using assertThat() method. To utilize assertThat(), we have to first import the static package: 

 Below we can see that ‘Soccer’ checkbox is checked by default

 Execute. 

All assertions pass

 Execute.

As expected, we get assertion error

Execute, the assertion passes

As expected, the letters were inputted by typing in the search field. Playwright focussed on the search field by default

 The focus on the search field can be eliminated by utilizing the 'blur' method (see line#34)

 Execute. 

The script failed to input the letter ‘e’ due to the search field losing focus

 Execute. 

The text gets entered and than gets cleared

 

Source code (assert checkboxes)

package com.rsa.playwrightjava;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
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.Page;
import com.microsoft.playwright.Playwright;
public class Blog40_AssertCheckboxes {
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("http://www.tizag.com/htmlT/htmlcheckboxes.php");
//assert url
assertThat(page).hasURL("http://www.tizag.com/htmlT/htmlcheckboxes.php");
//assert title
assertThat(page).hasTitle("HTML Tutorial - Checkboxes");
//assert link text
assertThat(page.locator("#menu > a:nth-child(38)")).hasText("HTML - Images");
//assert whether checkbox is checked
assertThat(page.locator("//html/body/table[3]/tbody/tr[1]/td[2]/table/tbody/tr/td/div[6]/input[1]")).isChecked();
assertThat(page.locator("body > table:nth-child(3) > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr > td > div:nth-child(15) > input[type=checkbox]:nth-child(6)")).isChecked();

//assert whether checkbox is visible
assertThat(page.locator("body > table:nth-child(3) > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr > td > div:nth-child(15) > input[type=checkbox]:nth-child(6)")).isVisible();
}
@AfterMethod
public void tearDown() {
page.pause();
browser.close();
page.close();
}
}

Source code (blur, clear)
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.Page;
import com.microsoft.playwright.Playwright;
public class Blog40_blur_clear {
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://www.google.com/");
page.keyboard().press("r");
page.keyboard().press("a");
page.keyboard().press("h");
page.keyboard().press("u");
page.keyboard().press("l");
page.keyboard().press("s");
page.keyboard().press("h");
page.locator("#APjFqb").blur();
page.keyboard().press("e");
page.getByTitle("Search").fill("https://rahulshettyacademy.com/");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
page.getByTitle("Search").clear();

}
@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"}