清單 2 顯示了用于驗證 圖 3 中演示的內容是否可以正常工作的代碼:
清單 2. 測試一個良好場景
@Test
public void assertDefinitionPresent() {
TextComponentFixture text1 = new TextComponentFixture(this.fixture,
"wordValue");
text1.enterText("pugnacious");
ButtonFixture bfix = new ButtonFixture(this.fixture, "findWord");
bfix.click();
LabelFixture fix = new LabelFixture(this.fixture, "definition");
fix.shouldHaveThisText("Combative in nature; belligerent.");
}
注意 fixture 對象通過一個邏輯名稱和特定的 GUI 組件連接在一起。例如,在 Word Finder GUI 中,通過編程將 JButton 對象與 “findWord” 名稱聯系起來。請注意在定義按鈕時,我是如何通過調用組件的 setName() 方法做到這點的,如清單 3 所示:
清單 3. 定義 Find Word 按鈕
findWordButton = new JButton();
findWordButton.setBounds(new Rectangle(71, 113, 105, 29));
findWordButton.setText("Find Word");
findWordButton.setName("findWord");
同樣要注意,在 清單 2 中,我是如何通過將 “findWord” 名稱傳遞給 TestNG-Abbot 的 ButtonFixture 對象而獲得對按鈕的引用!皢螕簟 按鈕(調用 click 方法)然后使用 TestNG-Abbot 的 LabelFixture 對象插入單詞的釋義,多么酷!不過不要就此滿足。
文章來源于領測軟件測試網 http://www.anti-gravitydesign.com/