在基于 Eclipse 的應用程序中實際使用 RFT find 方法
在測試基于 Eclipse 的應用程序中使用 find 方法的這個實例僅局限于 AUT 的一個部分,作為得出對象 getter 方法的設計的一個途徑。本應用程序中的對象具有可識別的模式。標準的 SWT 對象是主要被使用的,但還有應用程序開發團隊提供的一些專門的定制類。
測試開發人員創建一個通用的方法來獲取一個特定類的所有對象,或者獲取一個具體父對象下給定索引號的一個對象。要使得自動化在應用程序的局部版本上工作,測試開發人員決定使用對象索引作為尋找對象的關鍵屬性,而不是文本。下面介紹的類將用于做這件事。
注意:這些類中的所有方法都是靜態的,所以測試開發人員不需要將這個類實例化為一個對象就可以使用這些方法?梢酝ㄟ^使用以下格式來調用它們:<class name>. <method name> (<parameters>)
。
FindAppObjects.java 類可以讓您使用兩個不同的方法:getAllObjects 和 getObjectAtIndex
。這些方法通常不直接使用,但它們是其他方法的基礎。然而,您可以直接使用它們來確定通過哪些索引號找到哪些對象。
/** * Given a parent object, find all descendant test objects with a * given class. * @param parent TestObject * @param className String * @return Array of TestObject or null if no objects of type * className were found. */ public static TestObject[] getAllObjects(TestObject parent, String className) |
getAllObjects
將 TestObject 類型的父對象和 String 類型的字符串為輸入,并返回包含了父對象的具體類型的所有子對象的 TestObject 的數組。此實例返回 RootTestObject 的所有子對話框:
RootTestObject root = getRootTestObject (); TestObject[] dialogs = FindAppObjects.getAllObjects(root, "javax.swing.JFrame"); |
選擇正確的父 TestObject 對象是重要的,以便您可以從結果中只獲得您正在尋找的對象。作為一個實例,假設下面的部分表示 AUT 中的一個對象層次:
applicationcompositegroup0button0button1group1button0button1 |
Calling getAllObjects (application, "button")
將返回一個含有四個按鈕的數組。然而,如果不循環掃描該數組,并打印出每個索引號和 button[index].getProperty("text") 字符串的話,就不能很快確定哪些索引號匹配哪些按鈕。
要取代這種麻煩的過程,進行一個以上的分層調用更有意義:
// get all groups in the applicationTestObject[] groups = FindAppObjects.getAllObjects (application, "group");// limit search to only buttons in the first group foundTestObject[] buttons = FindAppObjects.getAllObjects (groups[0], "button"); // click the first button found under the groupnew Button(to[0]).click(); |
getAllObjects (group0, "button")
調用只返回兩個按鈕,它們很可能處于正確的順序(button0,然后 button1),如屏幕上顯示的。如果整個應用程序或對話框中只有一些按鈕,那么獲得所有的按鈕并且計算出索引可能會更容易。
/** * Given a parent object, find the descendent test object * with a given class, at a given one-based index. * * @param parent TestObject * @param className String * @param index int, zero-based index * @return TestObject or null if index is greater than * the total number of objects found. */ public static TestObject getObjectAtIndex (TestObject parent, String className, int index) |
getObjectAtIndex
精煉了 getAllObjects
的使用。它獲取一個父的 TestObject 對象、一個 String 類的字符串對象,和一個以零為基數的整數,然后返回一個單個的 TestObject 對象。例如,此代碼返回指定(父)分組下的第一個按鈕:
TestObject to = getObjectAtIndex(group1, BUTTON, 0); |
這些方法都返回一個 TestObject 對象,并且那些可能不是您需要找的具體類型的對象。上面的兩個方法都返回一般的 TestObject 對象。常常,您希望使用具體類型的對象。當使用這些方法時,確保在您將對象用于代碼中之前,顯式地將所返回的 TestObject 對象轉換為正確的類型。
FindBasicAppObjects.java 類提供進一步的細化和輔助。它是 FindAppObjects.java 的子類,并且使用了 getObjectAtIndex 方法。它包含返回 TestObject 父對象的索引號位置的子對象的 getter 方法。這時產品對象已經被轉換成為了正確的類,所以您不需要自己再次的轉換它。此實例返回標準的 Eclipse SWT widget 類的一個子集,例如 org.eclipse.swt.widgets.Button:
/** * Find the index-specified org.eclipse.swt.widgets.Button * child control under the specified parent. * * @param parent TestObject * @param index int, zero-based * @return WButton */ public static WButton getButton(TestObject parent, int index) |
getDialog
是 FindBasicAppObjects.java 中唯一一個不需要父親對象的方法,因為它假設父對象是 AUT。為了能夠識別正確的對話框,即使該對話框的標題是易變的,getDialog 將正則表達式(Regex)對象作為參數,如下所示:
/** * Find a org.eclipse.swt.widgets.Shell control * with a caption specified by the given * Regular Expression (Regex). * * @param dialogCaption Regex object containing dialog's * expected caption * @return WFrame */ public static WFrame getDialog (Regex dialogCaption) |
比較 Place an Order 對話框的代碼(在此部分的開頭)和在下面實例中使用FindAppObjects
和 FindBasicAppObjects
的同樣的行為。假設 Place an Order 對話框是 WFrame 類型的 Eclipse 對話框,并且按鈕是 WButton 類型的。
public class PlaceAnOrder extends RationalTestScript { private final int CANCEL = 2; private final Regex DIALOG_CAPTION = new Regex ("^Place an Order$"); public WFrame getDialog() { return FindBasicAppObjects.getDialog(DIALOG_CAPTION); } public WButton getButtonCancel() { return FindBasicAppObjects.getButton(getDialog(),CANCEL); } public void testMain(Object[] args) { getButtonCancel().click(); } } |
前面的代碼是以很容易維護的方式編寫的,代碼中每個部分都分隔為容易理解的一塊。對于喜歡更加緊湊布局的編碼人員,該代碼還可以以此方式書寫:
public class PlaceAnOrder extends RationalTestScript { public void testMain(Object[] args) { FindBasicAppObjects. getButton (FindBasicAppObjects. getDialog (DIALOG_CAPTION), CANCEL).click(); } } |
文章來源于領測軟件測試網 http://www.k11sc111.com/