當你模擬讓用戶輸入一個新的URL到瀏覽器的URL欄,那麼測試類負責創建它所需的頁面對象。
另一方面,當您在頁面上執行一些操作會導致瀏覽器指向另一個頁面 - 例如,單擊鏈接或提交表單 - 那麼這是該頁面的責任對象返回下一個頁面對象。
由於我對主頁,帳戶頁面和結果頁面之間的關係知之甚少,無法確切地告訴您網站會如何在您的網站上播放,我將以在線商店應用爲例代替。
假設你有一個SearchPage。當您在SearchPage上提交表單時,它將返回一個結果頁面。當你點擊一個結果時,你會得到一個ProductPage。因此,班會是這個樣子(簡稱只是相關的方法):
public class SearchPage {
public void open() {
return driver.get(url);
}
public ResultsPage search(String term) {
// Code to enter the term into the search box goes here
// Code to click the submit button goes here
return new ResultsPage();
}
}
public class ResultsPage {
public ProductPage openResult(int resultNumber) {
// Code to locate the relevant result link and click on it
return new ProductPage();
}
}
的測試方法來執行這個故事會是這個樣子:
@Test
public void testSearch() {
// Here we want to simulate the user going to the search page
// as if opening a browser and entering the URL in the address bar.
// So we instantiate it here in the test code.
SearchPage searchPage = new SearchPage();
searchPage.open(); // calls driver.get() on the correct URL
// Now search for "video games"
ResultsPage videoGameResultsPage = searchPage.search("video games");
// Now open the first result
ProductPage firstProductPage = videoGameResultsPage.openResult(0);
// Some assertion would probably go here
}
因此,大家可以看到,有這個頁面對象的「鏈接」,每個頁面都會返回下一個。
其結果是,你最終會得到很多不同的頁面對象實例化其他頁面對象。所以如果你有一個相當規模的網站,你可以考慮使用依賴注入框架來創建這些頁面對象。
「頁面對象」究竟是什麼意思? – Alp 2012-04-25 12:45:25