appium 获取android 粘贴板上的内容

appium 新版本增加了获取粘贴板的内容。如果使用appium旧版本,获取粘贴板的内容不是那么容易的,甚至百度谷歌各种搜,都无法找到合适的解决方法。新版本获取android 粘贴板内容就显得很容易了,简单一句就可以: 

String ss = driver.getClipboardText();

如下为以有道云笔记apk为示例:
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TestYoudao3 {
    public static void main(String[] args) {
        String udid = "Y9K0214B13002052";
        udid = "192.168.116.101:5555";
        AndroidDriver<WebElement> driver = null;
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", udid);
        capabilities.setCapability("udid", udid);

        // 设置安卓系统版本ad
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("automationName", "uiautomator2");
        capabilities.setCapability("platformVersion", "5.1");

        // session 延迟时间
        capabilities.setCapability("newCommandTimeout", 24000);
        capabilities.setCapability("noReset", true);
        //apabilities.setCapability("device", "selendroid");
        // capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("unicodeKeyboard", "True");
        capabilities.setCapability("resetKeyboard", "True");
        // 设置app的主包名和主类名 微信程序包名和activity 名
        capabilities.setCapability("appPackage", "com.youdao.note");
        capabilities.setCapability("appActivity", "com.youdao.note.activity2.SplashActivity");

        String url = "http://127.0.0.1:4723/wd/hub";

        try {
            driver = new AndroidDriver<WebElement>(new URL(url), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);


        driver.findElementById("com.youdao.note:id/add_note").click();
        driver.findElementById("com.youdao.note:id/add_note_floater_add_note").click();

        driver.setClipboardText("hello clipboard ");  //设置粘贴板内容 设置后android粘贴板的内容就为hello clipboard ,可以手动在android中粘贴试下
        String ss = driver.getClipboardText(); //获取粘贴板内容
        WebElement eletitle = driver.findElementById("com.youdao.note:id/note_title");
        eletitle.click();
        eletitle.sendKeys(ss);  //把粘贴板内容输入到标题文本框中
    }

}

执行后显示的内容

 

欢迎关注技术公众号获取更多最新自动化技术资讯

 

posted @ 2018-09-13 19:07  testway  阅读(1418)  评论(0编辑  收藏  举报