有时候在测试时需要检查一下系统剪贴板中的内容是否正确,这时候可以采用类似下面的方法来插入clipboard检查点:
public void testMain(Object[] args)
{
setClipboardText("hello world!");
clipboardVP("CheckClipboardData","hello world!");
}
/**
* return the text on the clipboard.
* If there is no text, it returns null
*/
public static String getClipboardText()
{
java.awt.datatransfer.Clipboard clip = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
java.awt.datatransfer.Transferable t = clip.getContents(null);
String s = null;
try
{
s = (String)t.getTransferData(java.awt.datatransfer.DataFlavor.stringFlavor);
}
catch(Exception e) {}
return s;
}
/**
* Set the contents of the clipboard to the provided text
*/
public static void setClipboardText(String s)
{
java.awt.datatransfer.Clipboard clip = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
java.awt.datatransfer.StringSelection ss = new java.awt.datatransfer.StringSelection(s);
clip.setContents(ss, ss);
}
/**
* A simple text vp for the clipboard. If you insert a call to this method
* in your test script, the first time the script is run, the clipboard text will
* be captured and written to a VP baseline named by <code>vpName</code>. After
* the baseline is created on the first playback, running the script will cause the
* clipboard text to be captured, compared to the baseline, and the result will
* be written to the test log.
*/
public void clipboardVP(String vpName)
{
vpManual(vpName, getClipboardText()).performTest();
}
public void clipboardVP(String vpName,String expected)
{
System.out.println(vpName);
System.out.println(expected);
System.out.println(getClipboardText());
vpManual(vpName, expected, getClipboardText()).performTest();
}