Automating the Clipboard on iOS and Android

automate-small-business-guide-768x0-c-default
     Now Appium has the built-in support for automating clipboard with support of following methods:
  • setClipboardText(“value”)
  • getClipboardText()

   To automate this scenario you have to use the latest Appium(>5.0.4) and Selenium(>3.9.1) versions. Following is the sample code:

public class ClipboardTest {  
    private String ANDROID_APP = “https://github.com/cloudgrey-io/the-app/releases/download/v1.4.0/TheApp-v1.4.0.apk”;
    private String IOS_APP = “https://github.com/cloudgrey-io/the-app/releases/download/v1.4.0/TheApp-v1.4.0.app.zip”;
    private String APPIUM_SERVER = “http://localhost:4723/wd/hub”;
 
    private By clipboardNav = MobileBy.AccessibilityId(“Clipboard Demo”);
    private By refreshClipboardBtn = MobileBy.AccessibilityId(“refreshClipboardText”);
    private By clipboardInput = MobileBy.AccessibilityId(“messageInput”);
    private By setTextBtn = MobileBy.AccessibilityId(“setClipboardText”);
 
    @Test
    public void testClipboard_Android() throws IOException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(“platformName”, “Android”);
        capabilities.setCapability(“deviceName”, “Samsung”);
        capabilities.setCapability(“automationName”, “UiAutomator2”);
        capabilities.setCapability(“udid”,”edsdsdssdasc1fsdfsds”);
        capabilities.setCapability(“app”, ANDROID_APP);
        AndroidDriver driver = new AndroidDriver(new URL(APPIUM_SERVER), capabilities);
        automateClipboard(driver);
    }
 
    @Test
    public void testClipboard_iOS() throws IOException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(“platformName”, “iOS”);
        capabilities.setCapability(“platformVersion”, “11.3”);
        capabilities.setCapability(“deviceName”, “iPhone 7”);
        capabilities.setCapability(“udid”,”edsdsdssdasc1fsdfsds”);

        capabilities.setCapability(“app”, IOS_APP);
        IOSDriver driver = new IOSDriver(new URL(APPIUM_SERVER), capabilities);
        automateClipboard(driver);
    }
    private void automateClipboard(AppiumDriver driver) {
        WebDriverWait wait = new WebDriverWait(driver, 5);
        try {
          wait.until(ExpectedConditions.presenceOfElementLocated(clipboardNav)).click();
            String text = “Hello World”;
            ((HasClipboard) driver).setClipboardText(text);
            wait.until(ExpectedConditions.presenceOfElementLocated(refreshClipboardBtn)).click();
            By clipboardText = MobileBy.AccessibilityId(text);
            Assert.assertEquals(driver.findElement(clipboardText).getText(), text);
            text = “Hello World Again”;
            driver.findElement(clipboardInput).sendKeys(text);
            try {
                driver.hideKeyboard();
            } catch (Exception ign) {}
            driver.findElement(setTextBtn).click();
            Assert.assertEquals(((HasClipboard) driver).getClipboardText(), text);
 
        } finally {
            driver.quit();
        }
    }
}

Try to use the above code for your automation.

Reference: Appium Pro

make it perfect!