'type' method is used to type some text on the selected element.
Signature
void type(String selector, String text)
void type(String selector, String text, TypeOptions options)
Using TypeOptions, we can customizes the behaviors like "time to wait between key presses in milliseconds".
Example
page.type("#email", "demo@demo.com", new Page.TypeOptions().setDelay(100));
page.type("#password", "demo123");
Find the below working application.
loginPage.html
<!DOCTYPE html>
<html>
<head> </head>
<body>
<form onsubmit="return false;">
<table>
<tr>
<td> <label for="email"><b>Email</b></label> </td>
<td><input type="text" placeholder="Enter your email id" name="email" id="email" required> </td>
</tr>
<tr>
<td> <label for="password"><b>Password</b></label></td>
<td><input type="password" placeholder="Enter Password" name="password" id="password" required> </td>
</tr>
<tr>
<td> <label for="confirm-password"><b>Repeat Password</b></label> </td>
<td><input type="password" placeholder="Repeat Password" name=confirm-password" id="confirm-password" required></td>
</tr>
<tr>
<td> <input type="submit" value="submit" onclick="display()"></td>
</tr>
</table>
</form>
<p id="result"></p>
<script>
function display() {
let result = document.getElementById("result");
let userEmail = document.querySelector('input[id="email"]').value;
result.innerHTML = `<b>Your email : ${userEmail} is registered</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();
}
}
}
PageTypeDemo.java
package com.sample.app.page;
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 PageTypeDemo {
public static void main(String[] args) throws IOException {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium()
.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
final String content = FileUtil.resourceAsString("locators" + File.separator + "loginPage.html");
Page page = browser.newPage();
page.setContent(content);
page.type("#email", "demo@demo.com", new Page.TypeOptions().setDelay(100));
page.type("#password", "demo123");
page.type("#confirm-password", "demo123");
page.click("input[type=\"submit\"]");
String result = page.locator("#result").textContent();
System.out.println(result);
}
}
}
Output
Your email : demo@demo.com is registered
Previous Next Home
No comments:
Post a Comment