页面工厂 - 使用泛型类和方法重构代码

时间:2018-06-17 04:43:31

标签: java selenium generics pageobjects page-factory

我试图减少我的BaseClass中的代码量,并通过最小化重复代码来清理我的PageObject类。在Page课程中,如何调用getTableTitle()课程中的CommonTable<T>方法并返回Page课程

所以我通过实例化CommonTable类来实现这个工作,但是它并不漂亮。当然有一个更好的解决方案。

Page class

public class Page extends BaseClass {

    @FindBy(xpath = "/html/body/div[3]/div/div/div[3]/ul/li[1]/a")
    private WebElement TERMS_AND_CON;

    public TermsAndConditions termsAndConditions() {
        click(TERMS_AND_CON);
        return PageFactory.initElements(driver, TermsAndConditions.class);
    }

    public CommonTable getTable(){
        return PageFactory.initElements(driver, CommonTable.getTableTitle(TermsAndConditions.class));
    }
}

CommonTable类

public abstract class CommonTable<T> {

    private WebDriver driver;

    private Class<T> clazz;

    protected CommonTable(WebDriver driver, Class<T> clazz) {
        this.driver = driver;
        this.clazz = clazz;
    }

    public T getTableTitle(){
        System.out.println(driver.getCurrentUrl());
        return PageFactory.initElements(driver, clazz);
    }
}

编辑1:

public class Page extends BaseClass {

    @FindBy(xpath = "/html/body/div[3]/div/div/div[3]/ul/li[1]/a")
    private WebElement TERMS_AND_CON;

    public TermsAndConditions termsAndConditions() {
        click(TERMS_AND_CON);
        return PageFactory.initElements(driver, TermsAndConditions.class);
    }

    public Page getTable(){
        CommonTable<Page> commonTable = new CommonTable(driver, Page.class);
        commonTable.getTableTitle();
        return PageFactory.initElements(driver, Page.class);
    }
}

0 个答案:

没有答案