所以我有一个页面对象,它处理用户所在页面的所有主要功能。我想这样做,这样当调用页面对象的实例时就会发生一些事情。在当前情况下,我有:
public MyPageObject MY_SCREEN = new MyPageObject(this);
当我调用MY_SCREEN.fillMyScreenFields();
我希望 MY_SCREEN
导航到该屏幕,而不在 fillMyScreenFields()
最佳答案
我仍然不确定您想要什么以及如何获得 SO 异常(exception),但以下是您的选择:
public class HomePage {
Webdriver driver; // inject an instance using a DI framework
// option 1: uses the above instance, created by DI or just plain 'new' keyword
public HomePage(){
driver.get("https://yourpage.com/");
}
// option 2: pass in the driver in your tests
public HomePage(WebDriver driver){
driver.get("https://yourpage.com/");
}
// option 3: best one, I'd advise against the above two options,
// there will come a situation when you want to init a page object,
// but you don't want to navigate to it
public void openPage(){
driver.get("https://yourpage.com/");
}
}
这是一个repo一个简单的页面对象模式示例
这是另一个 repo具有使用流畅界面的更复杂的页面对象模式示例
(免责声明:两者都是我的)
关于java - PageObject 实例调用执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53223474/