Autoplay



Transcripted Summary

# Using the Clipboard with Appium and Android

In this demo, we will start using the clipboard with Appium and Android.

I prepared the class for our tests.

This is the Clipboard_Android_Test.


package Android;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

public class Clipboard_Android_Test {

    private AndroidDriver<WebElement> driver;

    @BeforeTest
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "8.0");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("app", System.getProperty("user.dir") + "/apps/selendroid.apk");
        driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
    }

    @Test
    public void clipboard_test() {
    }

    @AfterTest
    public void tearDown() {
        if (null != driver) {
            driver.quit();
        }
    }
}

Here we have the AndroidDriver - in this case, we will be using AndroidDriver like with the SMS because this function is already included in the AndroidDriver.

Here we have the setup and we will add our test here.

In this case, we will use a new application "selendroid".

This is a different application from API Demos - we will use this in the next chapter for the hybrid application.

So in the project, under apps, we already have a Selendroid and we are using it in the script.

If we open our application, we will find the Selendroid application and it's also a simple application for using anything with Android.



So, here we have a text box.


In our test, we will set our clipboard with the text, and then we should paste it into another text box.

To be able to find this text box, we can open the inspection session, and if we click on this element, we will find that it has an accessibility ID, so we can start directly using this one.

Inside our application, we can just create a new string name and the text will be "Hello TAU".

Then, I will add driver.setClipboardText() which will take a string - in this case, it is the text we just added in the first line.

Here, I am setting the clipboard on the Android device with the text.

After that, I will add nameTxt - this is the text box that I need to find.

I will use driver.findElementByAccessibilityID() and then I will add the accessibility ID.

So, I will go to the inspection session and copy this accessibility ID, or I could use the ID.

I will add MobileElement and then I will cast it to be a MobileElement from a WebElement.

Then I will call nameTxt.clear() first in case there is any existing text, and then we will sendKeys() from the clipboard - so we will use driver.getClipboardText().

Then we will assert that the value is correct - we will use the Assert from testng.asserts - and we will assert that the text is equal to nameTxt.getText().


    @Test
    public void clipboard_test() {
        String text = "Hello TAU";
        driver.setClipboardText(text);
        MobileElement nameTxt = (MobileElement) driver.findElementByAccessibilityId("my_text_fieldCD");
        nameTxt.clear();
        nameTxt.sendKeys(driver.getClipboardText());
        Assert.assertEquals(text, nameTxt.getText());
    }

Then I will try to start my script and check what happened.

I will close the inspection session, open the device, and the Appium server will check our test case.

Now we are installing this Android application, we have the Android SDK version on the device… everything is set here.

We are using UIAutomator2 with the ADB and now we start running our test.

Here, we already added "Hello TAU", the assertion passed and our tests have passed now.



So setClipboardText() is from AndroidDriver and we are using it to set text to a clipboard and then we can use driver.getClipboardText() to be able to load it again.



Resources



Quiz

The quiz for this chapter can be found in Chapter 5.10

© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR