PageObject实例调用做了一些事情

问题描述 投票:0回答:1

所以我有一个页面对象,它处理用户所在页面的所有主要功能。我想这样做,所以当调用页面对象的实例时会发生一些事情。在目前的情况下,我有:

public MyPageObject MY_SCREEN = new MyPageObject(this);

当我打电话给MY_SCREEN.fillMyScreenFields();时,我想让MY_SCREEN导航到那个屏幕,而没有在fillMyScreenFields()中实现导航功能

java selenium automated-tests pageobjects
1个回答
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/");
    }
}

这是一个带有简单的Page Object模式示例的repo

这是另一个使用更流畅的界面的更复杂的页面对象模式示例的repo

(免责声明:两者都是我的)

© www.soinside.com 2019 - 2024. All rights reserved.