Locator.isChecked() method return true when the checkbox or radio box is checked, else false.
Signature
boolean isChecked()
isChecked(IsCheckedOptions options)
Example
page.getByLabel("java").isChecked()
Find the below working application.
checkbox.html
<!DOCTYPE html>
<html>
<body>
<h1>User favorite languages form</h1>
<p>Select your favorite languages and click on submit button</p>
<form onsubmit="return false;">
<input type="checkbox" id="java" name="my_fav_language" value="Java">
<label for="java">Java</label><br>
<input type="checkbox" id="go" name="my_fav_language" value="Go">
<label for="go">Go</label><br>
<input type="checkbox" id="python" name="my_fav_language" value="Python">
<label for="python">Python</label><br><br>
<input type="submit" value="submit" onclick="display()">
</form>
<p id="result"></p>
<script>
function display() {
let result = document.getElementById("result");
let favoriteLanguages = "<br />";
var markedCheckbox = document.getElementsByName('my_fav_language');
for (var checkbox of markedCheckbox) {
if (checkbox.checked)
favoriteLanguages = favoriteLanguages + "<br />" + checkbox.value;
}
result.innerHTML = `<b>Your favourite language are : ${favoriteLanguages}</b>`
}
</script>
</body>
</html>
FileUtil.java
package com.sample.app.util;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class FileUtil {
public static String resourceAsString(String resourceName) throws IOException {
ClassLoader classLoader = FileUtil.class.getClassLoader();
URL url = classLoader.getResource(resourceName);
if (url == null) {
return null;
}
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
try (InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream())) {
char[] buffer = new char[1048];
StringBuilder builder = new StringBuilder();
int count = -1;
while ((count = inputStreamReader.read(buffer, 0, buffer.length)) != -1) {
builder.append(buffer, 0, count);
}
return builder.toString();
}
}
}
SelectCheckBox.java
package com.sample.app.actions;
import java.io.File;
import java.io.IOException;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;
public class SelectCheckBox {
public static void main(String[] args) throws IOException, InterruptedException {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium()
.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
final String content = FileUtil.resourceAsString("actions" + File.separator + "checkbox.html");
Page page = browser.newPage();
page.setContent(content);
System.out.println("Selecting Java langauge");
page.getByLabel("java").click();
page.getByLabel("go").check();
System.out.println("Is Java langauge checked : " + page.getByLabel("java").isChecked());
System.out.println("Is Go langauge checked : " + page.getByLabel("go").isChecked());
System.out.println("Is Python language checked : " + page.getByLabel("python").isChecked());
}
}
}
Output
Selecting Java langauge Is Java langauge checked : true Is Go langauge checked : true Is Python language checked : false
No comments:
Post a Comment