在RFT中新增clipboard檢查點

TIB發表於2010-01-17

有時候在測試時需要檢查一下系統剪貼簿中的內容是否正確,這時候可以採用類似下面的方法來插入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();
 }
 

相關文章