PageObject实例调用会执行某些操作

时间:2018-11-09 09:55:37

标签: java selenium automated-tests pageobjects

因此,我有一个页面对象,该对象处理用户所在页面的所有主要功能。我想这样做是为了在调用页面对象的实例时发生某些事情。在当前情况下,我有:

public MyPageObject MY_SCREEN = new MyPageObject(this);

当我打电话给MY_SCREEN.fillMyScreenFields();时 我希望MY_SCREEN导航到该屏幕,而无需在fillMyScreenFields()

中实现导航功能

1 个答案:

答案 0 :(得分:1)

我仍然不确定您要做什么以及如何获得SO异常,但这是您的选择:

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,具有更流畅的页面对象模式示例,该示例使用流畅的界面

(免责声明:都是我的)

相关问题