当你模拟让用户输入一个新的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