How to enter text in a text box using appium?

Whether you’re building a login screen test or filling out forms, one of the most common interactions you’ll deal with is entering text into input fields.

In this article, we’ll walk through different ways to enter text in a text box using Appium, with code examples.

Throughout our example, we will try to fill in the highlighted element below with the value “codekru“. This element is present at our playground website – https://testkru.com/Elements/TextFields.

first name with placeholder

Here are a couple of ways to fill in the text box using Appium:

Let’s take a look at each option, one by one.

WebElement provides a sendKeys() method, which we can use to enter the data in an input field.

  • First, we need to locate the element, which we can do using the findElement() method. Since the element’s ID is ‘firstName‘, we can use that to find it.
WebElement element = driver.findElement(By.id("firstName"));
  • Next, we can enter the data into the text box using the sendKeys() method.
element.sendKeys("codekru");

Here is the whole code –

public class CodekruTest {

    public static void main(String[] args) throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("appium:deviceName", "iPhone 16");  // Check simulator name in Xcode
        caps.setCapability("appium:automationName", "XCUITest");
        caps.setCapability("appium:platformVersion", "18.3");  // Match your simulator's iOS version
        caps.setCapability("appium:browserName", "Safari");  // If testing a web app
        IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/"), caps);

        driver.get("https://testkru.com/Elements/TextFields");

        WebElement element = driver.findElement(By.id("firstName"));
        element.sendKeys("codekru");

    }

}

Below is the output of the above program –

Program result

JavascriptExecutor helps in executing the JavaScript code, and we can use it to enter the text in the textbox.

JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("arguments[0].value='codekru'",element);

Here is the whole code –

import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;

public class CodekruTest {

    public static void main(String[] args) throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("appium:deviceName", "iPhone 16");  // Check simulator name in Xcode
        caps.setCapability("appium:automationName", "XCUITest");
        caps.setCapability("appium:platformVersion", "18.3");  // Match your simulator's iOS version
        caps.setCapability("appium:browserName", "Safari");  // If testing a web app
        IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/"), caps);

        driver.get("https://testkru.com/Elements/TextFields");

        WebElement element = driver.findElement(By.id("firstName"));

        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
        javascriptExecutor.executeScript("arguments[0].value='codekru'",element);
    }

}

We hope that you have liked the article. If you have any doubts or concerns, please email us at admin@codekru.com or write us in the comments.

Liked the article? Share this on

Leave a Comment

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