Or copy link
Copy link
Testing mobile applications can be a complex task, especially when it comes to handling text input via the virtual keyboard. In this blog post, we will walk you through the steps to use the emulator virtual keyboard in a Flutter app for Appium testing. We’ll cover the necessary changes in your Flutter code and provide examples of how to handle text input in your Java-based Appium tests.
To ensure that the Flutter app is correctly set up for Appium testing, you need to disable text entry emulation. This allows the virtual keyboard to interact naturally with the app. Add the following line in your main.dart file:
main.dart
import 'package:flutter_driver/driver_extension.dart'; import 'package:flutter/material.dart'; import 'my_app.dart'; // Replace with your actual import void main() { // setting enableTextEntryEmulation: false allows keyboard interations enableFlutterDriverExtension(enableTextEntryEmulation: false); runApp(MyApp()); }
When configuring your Appium capabilities for automated testing, it’s essential to understand the role of unicodeKeyboard and resetKeyboard capabilities. These settings play a crucial role in managing the virtual keyboard’s behavior during your test execution. Below is a brief explanation of what these capabilities do and why you might want to set them to false and true respectively:
unicodeKeyboard
resetKeyboard
false
true
DesiredCapabilities caps = new DesiredCapabilities(); // ... // Keyboard capabilities caps.setCapability("unicodeKeyboard", false); caps.setCapability("resetKeyboard", true); // ...
Utils
The Utils class is a helper utility designed to facilitate context switching and keyboard state detection in an Appium-based test automation setup. The class primarily focuses on managing contexts (switching between native and Flutter contexts) and checking the visibility of the virtual keyboard in an Android environment.
public class Utils { private AppiumDriver driver; // ...
Declared a private instance variable driver of type AppiumDriver, which will be used to interact with the mobile application.
driver
AppiumDriver
public void switchToNativeContext(AppiumDriver driver) { this.driver = driver; try { // Ensure the driver supports context switching if (!(driver instanceof SupportsContextSwitching)) { throw new IllegalStateException("The driver does not support context switching."); } // List all available contexts Set<String> contextNames = ((SupportsContextSwitching) driver).getContextHandles(); for (String contextName : contextNames) { System.out.println(contextName); // Print available contexts for debugging } // Switch to native context if available if (contextNames.contains("NATIVE_APP")) { ((SupportsContextSwitching) driver).context("NATIVE_APP"); } else { System.out.println("NATIVE_APP context not found!"); } } catch (Exception e) { System.err.println("An error occurred while switching context: " + e.getMessage()); e.printStackTrace(); } }
instanceof SupportsContextSwitching
NATIVE_APP
public void switchToFlutterContext(AppiumDriver driver) { this.driver = driver; try { // Ensure the driver supports context switching if (!(driver instanceof SupportsContextSwitching)) { throw new IllegalStateException("The driver does not support context switching."); } // List all available contexts Set<String> contextNames = ((SupportsContextSwitching) driver).getContextHandles(); for (String contextName : contextNames) { System.out.println(contextName); // Print available contexts for debugging } // Switch to flutter context if available if (contextNames.contains("FLUTTER")) { ((SupportsContextSwitching) driver).context("FLUTTER"); } else { System.out.println("FLUTTER context not found!"); } } catch (Exception e) { System.err.println("An error occurred while switching context: " + e.getMessage()); e.printStackTrace(); } }
FLUTTER
public boolean isKeyboardShown() { return ((AndroidDriver) driver).isKeyboardShown(); }
Checks if the virtual keyboard is currently shown on the Android device.
AndroidDriver
isKeyboardShown
Setup your class to start interacting with the keyboard. In this example I have a class called LoginScreenAndoridTest this is where I’ll use the Utils class to interact with the virtual keyboard. Here’s how to set it up:
LoginScreenAndoridTest
@Test() public void loginIntoAppTest() throws Exception { utils = new Utils(); FlutterElement signInButton = find.byText("SIGN IN"); signInButton.click(); // switch to native context to interact with keyboard utils.switchToNativeContext(testDriver); WebElement emailTextFeild = driver.findElement(AppiumBy.xpath("//android.view.View[contains(@content-desc, 'Email')]/android.widget.EditText[1]")); emailTextFeild.click(); // enter email if (utils.isKeyboardShown()) { utils.typeTextViaKeyboard(testDriver,"JohnDoe@mytest.com"); ((AndroidDriver) testDriver).pressKey(new KeyEvent(AndroidKey.ENTER)); } WebElement passwordTextFeild = testDriver.findElement(AppiumBy.xpath("//android.view.View[contains(@content-desc, 'Password')]/android.widget.EditText[2]")); passwordTextFeild.click(); // enter password if (utils.isKeyboardShown()) { utils.typeTextViaKeyboard(testDriver,"Welcome"); ((AndroidDriver) testDriver).pressKey(new KeyEvent(AndroidKey.ENTER)); } // switch back to Flutter context utils.switchToFlutterContext(testDriver); }
enableFlutterDriverExtension(enableTextEntryEmulation: false);
typeTextViaKeyboard
By following these steps, you can effectively use the emulator virtual keyboard in your Flutter app for Appium testing. This setup ensures that your automated tests can interact with the app just as a real user would, providing more reliable and comprehensive test coverage. Happy testing!
Join our community on Patreon! As a member, you'll get exclusive access to our private code repositories, receive personalized one-on-one debugging and implementation support, and much more.
Save my name, email, and website in this browser for the next time I comment.
All the QA News You Need!
Zero Spam, 100% Quality