How to check if text is present in clipboard in Ro

2019-06-06 23:18发布

I am testing my Android application using Robotium. In a dialog, I have a button that will copy the text in the dialog to the clipboard. Is it possible to access the clipboard in my test to see if the text has been copied after the button has been pressed? If so, how?

1条回答
虎瘦雄心在
2楼-- · 2019-06-07 00:04

You can use the clipboard manager service in the same way you do in your application and then use its getText() method to retrieve the value. It should look something like (untested, from memory and I have had a few drinks...):

public String getClipboardText(){
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
         android.content.ClipboardManager clipboard =  (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
            return clipboard.getText(); 
    } else{
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
        return clipboard.getText(); 
    }
}

You will then need to assert that this matches the expected result (Whatever you set in the dialog)

查看更多
登录 后发表回答