Sunday, 12 February 2023

Playwright: fill the input using type() method

'type' method is used to enter some text to the input fields. 'type' method is available in below overloaded forms.

void type(String text)
void type(String text, TypeOptions options)

 

Example

page.getByLabel("email").type("demo@demo.com");
page.locator("input#password").type("test1234", new Locator.TypeOptions().setDelay(100));

 

Find the below working application.

 

registerMe.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();
    }

  }
}

 

FillTheInputUsingTypeMethod.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.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;

public class FillTheInputUsingTypeMethod {
  private static void printValueBySelector(Page page, String selector, String label) {
    String value1 = page.inputValue(selector);

    System.out.println(label + " : " + value1);
  }

  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("actions" + File.separator + "registerMe.html");

      Page page = browser.newPage();
      page.setContent(content);

      page.getByLabel("email").type("demo@demo.com");
      page.locator("input#password").type("test1234", new Locator.TypeOptions().setDelay(100));
      page.locator("input#confirm-password").type("test1234", new Locator.TypeOptions().setDelay(100));

      printValueBySelector(page, "input#email", "email");
      printValueBySelector(page, "input#password", "password");
      printValueBySelector(page, "input#confirm-password", "confirm password");

    }
  }
}

 

Output

email : demo@demo.com
password : test1234
confirm password : test1234

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment