关于 selenium webdriver 中的页面对象的快速问题。我们的网站非常动态,有很多 ajax 和各种身份验证状态。很难弄清楚如何定义每个页面对象但是可以说我已经弄清楚并定义了几个代表我们网站的页面对象。
你如何处理从页面到页面的交叉。因此,我为我的主页获取了一个页面对象,为我的帐户页面获取了一个页面对象,为我的结果页面获取了一个。然后我需要编写一个遍历我所有页面的测试来模拟用户执行多个操作。
你怎么说给我一个 HomePage 对象来创建一个新用途 -> 然后获取一个帐户页面对象来执行一些用户操作 - 然后获取一个结果页面对象来验证所有来自单个脚本的这些操作。
人们是如何做到这一点的?
谢谢
最佳答案
当您模拟让用户在浏览器的 URL 栏中输入新 URL 时,测试类有责任创建它需要的页面对象。
另一方面,当您在页面上执行某些操作时,会导致浏览器指向另一个页面 - 例如,单击链接或提交表单 - 然后该页面对象有责任返回下一页对象。
由于我对您的主页、帐户页面和结果页面之间的关系知之甚少,无法准确告诉您它在您的网站中的表现,因此我将使用在线商店应用程序作为示例。
假设您有一个 SearchPage。当您在 SearchPage 上提交表单时,它会返回一个 ResultsPage。当你点击一个结果时,你会得到一个 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
}
如您所见,页面对象存在这种“链接”,其中每个对象都返回下一个对象。
结果是您最终会得到许多不同的页面对象来实例化其他页面对象。因此,如果您有一个相当大的站点,您可以考虑使用依赖注入(inject)框架来创建这些页面对象。
关于selenium-webdriver - Selenium WebDriver 页面对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10315894/