Sending Arbitrary Keystrokes with The Actions API
If you & # x27; re anything like me, sometimes you just want to send arbitrary keystrokes to an app, without necessarily feature plant a text input field yet. Actually, just banter; this is not a genuine hobby of mine, but it & # x27; s a nice way to introduce this edition & # x27; s topic! It is really possible to use the new W3C Actions API not only to send pointer actions, but besides to send key stimulus. At the minute, this is supported in Appium & # x27; s Android drivers. The use cases for this feature are deviate and credibly not too mutual. Some apps require keyboard input on non-text fields or have elements that can & # x27; t be directly accessed (maybe as part of a game that implements its own keyboard, for example). SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses. For the simple intention of sending keystrokes, regardless of whether an element is & # x27; pore & # x27; or not, the Java client has as decent easy method for doing this, utilize theActions class: We construct an instance ofActions by passing in our session, registry asendKeys action on it with the characters we want to type, and then callperform()to get it all happen. The fact that we feature to explicitly callperform()means we could file a number of key inputs, perhaps with a serial ofwaits in between, or possibly interracial together with some pointer inputs as well. This is all good and full, but what if we want lower-level control over the typing? What if we want to urge multiple characters at once, or hold down a meta key (like SHIFT) while typing another character? In that case we & # x27; ll need to explore theKeyInput family. The way that we use it is very similar to the way we use thePointerInputclass in the. First, we define aSequence to contain our actions. Then, we define aKeyInput we use to generate the key actions (KeyDown or KeyUp on specific keys) we will register. Then, we register our actions with the overall successiveness, and eventuallyperform()that sequence with our driver. In the following model, we typecast & quot; Foo & quot; into the app, and get the capital & quot; F & quot; by use a combination of keystrokes that overlap in time: As you can see, this strategy is quite a bit more verbose, since we have to file both the down and up province of every key we want to add to the sequence. (Of course, in real test code we would belike make some helper method to trim boilerplate here). It & # x27; s important to note that these low-level methods use character codification point instead than characters themselves. And we & # x27; re making full use of the built-inKeys class from the Selenium customer, which lets us entree theSHIFT key without needing to look up its code point ourselves. That & # x27; s all there is to it! You can get pretty fancy, of course, because you & # x27; re not limited to the usual ASCII keys and can urge multiple keys at erst, or in a time succession of your choosing. Have a looking at the full codification sample below, where both strategies discussed above are represented: (Don & # x27; t forget to check out the full code sample inside the runnable projecton GitHub) Lead, Content Marketing, HeadSpin Inc. Piali is a dynamic and results-driven Content Marketing Specialist with 8+ years of experience in crafting engross narratives and marketing collateral across diverse industries. She excels in collaborate with cross-functional teams to develop innovative content scheme and deliver compelling, authentic, and impactful content that resonates with target audiences and enhances make authenticity. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts..png)



Sending Arbitrary Keystrokes with The Actions API
AI-Powered Key Takeaways
Check out:
Actions a = new Actions (driver); a.sendKeys (`` foo ''); a.perform ();Also check:
KeyInput keyboard = new KeyInput (`` keyboard ''); Sequence sendKeys = new Sequence (keyboard, 0); sendKeys.addAction (keyboard.createKeyDown (Keys.SHIFT.getCodePoint ())); sendKeys.addAction (keyboard.createKeyDown (`` f '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyUp (`` f '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyUp (Keys.SHIFT.getCodePoint ())); sendKeys.addAction (keyboard.createKeyDown (`` o '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyUp (`` o '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyDown (`` o '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyUp (`` o '' .codePointAt (0))); driver.perform (Arrays.asList (sendKeys));Read:
import io.appium.java_client.AppiumDriver; import io.appium.java_client.MobileBy; importee java.io.IOException; import java.net.URL; import java.util.Arrays; meaning org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; meaning org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; meaning org.openqa.selenium.interactions.KeyInput; significance org.openqa.selenium.interactions.Sequence; signification org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Edition046_W3C_Keys {individual String APP = `` https: //github.com/cloudgrey-io/the-app/releases/download/v1.8.0/TheApp-v1.8.0.apk ''; private By loginScreen = MobileBy.AccessibilityId (`` Login Screen ''); private By username = MobileBy.AccessibilityId (`` username ''); private AppiumDriver driver; private WebDriverWait delay; @ Before public void setUp () throws IOException {DesiredCapabilities crest = new DesiredCapabilities (); caps.setCapability (`` platformName '', `` Android ''); caps.setCapability (`` deviceName '', `` Android Emulator ''); caps.setCapability (`` automationName '', `` UiAutomator2 ''); caps.setCapability (`` app '', APP); driver = new AppiumDriver (new URL (`` http: //localhost:4723/wd/hub ''), caps); wait = new WebDriverWait (driver, 10);} @ After public vacancy tearDown () {try {driver.quit ();} catch (Exception ign) {}} @ Test public void testSendKeysAction () {wait.until (ExpectedConditions.presenceOfElementLocated (loginScreen)) .click (); WebElement usernameField = driver.findElement (username); usernameField.click (); Actions a = new Actions (driver); a.sendKeys (`` foo ''); a.perform (); Assert.assertEquals (`` foo '', usernameField.getText ());} @ Test public void testLowLevelKeys () {wait.until (ExpectedConditions.presenceOfElementLocated (loginScreen)) .click (); WebElement usernameField = driver.findElement (username); usernameField.click (); KeyInput keyboard = new KeyInput (`` keyboard ''); Sequence sendKeys = new Sequence (keyboard, 0); sendKeys.addAction (keyboard.createKeyDown (Keys.SHIFT.getCodePoint ())); sendKeys.addAction (keyboard.createKeyDown (`` f '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyUp (`` f '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyUp (Keys.SHIFT.getCodePoint ())); sendKeys.addAction (keyboard.createKeyDown (`` o '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyUp (`` o '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyDown (`` o '' .codePointAt (0))); sendKeys.addAction (keyboard.createKeyUp (`` o '' .codePointAt (0))); driver.perform (Arrays.asList (sendKeys)); Assert.assertEquals (`` Foo '', usernameField.getText ());}}Piali Mazumdar
Sending Arbitrary Keystrokes with The Actions API
4 Parts
-1280X720-Final-2.jpg)
Regression Intelligence hard-nosed guide for advanced users (Part 3)
-1280X720-Final-2.jpg)
Regression Intelligence practical guide for advanced user (Part 4)
Discover how HeadSpin can endow your business with superior testing capabilities







Discover how HeadSpin can empower your business with superior testing capabilities
Discover how HeadSpin can endue your concern with superior examine capableness
Connet Now


Automate This With SUSA
Test Your App Autonomously







.png)












