首页 > 解决方案 > 如何在页面对象模型中使用参数驱动程序返回通用类型的新对象

问题描述

我使用硒。我有这样的页面对象:

    public class Portal extends Utils{

    private WebDriver driver;
    private final By getReason= By.xpath("//a[contains(text(),'Get Sol')]");

    public Portal(WebDriver driver) {
        super(driver);
        this.driver = driver;
    }

    public VisitReason clickCurrentReason() {
        clickIn(getReason);
        return new VisitReason(driver);
    }
}

我想要这个方法:clickCurrentReason() return new Object extend Utils class 并传递给它参数:driver。怎么做?我知道我必须使用泛型。我找到了部分解决方案:

public<T extends Utils> T clickCurrentReason(){
        clickIn(getReason);
        return (T)(driver);        
    }

但是如何通过返回:“return new Object(driver)”

@测试方法:

public void Test() {
        TestingEnv testingEnv = new TestingEnv(driver);

        Portal portal = testingEnv.openPage();

        VisitReason visitReason = portal.clickCurrentReason();

        //sometimes instead of the last line it will be: VisitInSpot visitInSpot = portal.clickCurrentReason();
        //sometimes instead of the last line it will be: VisitBack visitBack = portal.clickCurrentReason();

    }

标签: javaseleniumgenericsreflectionpageobjects

解决方案


推荐阅读