追求代碼質量: 使用 TestNG-Abbot 實現自動化 GUI 測試[4] 軟件測試
測試意外場景
當然,如果我非常希望驗證我的 Word Finder GUI,我必須確保在用戶執行意外操作時 —— 程序能夠正常工作,比如在輸入單詞之前按下 Find Word 按鈕,或者情況更糟,比如他們輸入了一個無效的單詞。舉例來說,如果用戶沒有向文本字段輸入內容,GUI 應該顯示特定的信息,如清單 4 所示:
圖 4. 糟糕的極端例子
當然,使用 TestNG-Abbot 測試這種情況非常簡單,不是嗎?我所做的僅僅是將空值傳送到 TextComponentFixture 中,按下按鈕(通過對 ButtonFixture 使用 click 方法)并插入 “Please enter a valid word” 響應!
清單 4. 測試一個極端例子:如果有人沒有輸入單詞就按下了按鈕該怎么辦?
@Test
public void assertNoWordPresentInvalidText() {
TextComponentFixture text1 = new TextComponentFixture(this.fixture,
"wordValue");
text1.enterText("");
ButtonFixture bfix = new ButtonFixture(this.fixture, "findWord");
bfix.click();
LabelFixture fix = new LabelFixture(this.fixture, "definition");
fix.shouldHaveThisText("Please enter a valid word");
}
如清單 4 所示,一旦理解了獲得所需 GUI 組件的引用時,事情并不是很困難。最后一步是檢驗其他 糟糕的極端例子 —— 輸入了無效的單詞。這個過程與 清單 1 和 清單 3 非常相似:僅僅是將所需的 String 傳遞到 TextComponentFixture 對象,單擊,然后插入特定的文本。如清單 5 所示:
清單 5. 輕松驗證另一個極端例子!
@Test
public void assertNoWordPresentInvalidText() {
TextComponentFixture text1 = new TextComponentFixture(this.fixture,
"wordValue");
text1.enterText("Ha77");
ButtonFixture bfix = new ButtonFixture(this.fixture, "findWord");
文章來源于領測軟件測試網 http://www.anti-gravitydesign.com/