简体   繁体   English

Java/Selenium:方法中尚未定义 class 的实例

[英]Java/Selenium: Instance of not yet defined class in method

In Java, it is possible to define a return value T, extending an abstract base class - and in the return method, to cast T into an instance of a specific non-abstract class.在 Java 中,可以定义返回值 T,扩展抽象基 class - 并在返回方法中,将 T 转换为特定非抽象 ZA2F2ED4F8EBC2CBB4C21A29DC40AB61 的实例。

Example: My website is supposed to show a registration page in European countries, and a terms+conditions page elsewhere.示例:我的网站应该在欧洲国家显示注册页面,在其他地方显示条款+条件页面。 I have a test method providing this:我有一个提供这个的测试方法:

public <T extends StartBasePage> T openStartPage(boolean isInEurope) {
        if (isInEurope) {
            return (T) pageProvider.createPageAsserted(StartRegistrationPage.class);
        } else {
            return (T) pageProvider.createPageAsserted(StartTermsPage.class);
        }
    }

But is it possible to use T in the method already as an "instance" of abstract StartBasePage and then use methods from that abstract class to define what exactly is supposed to be cast into?但是是否可以在方法中使用 T 已经作为抽象 StartBasePage 的“实例”,然后使用该抽象 class 中的方法来定义应该转换的确切内容?

Imagine StartBasePage has a method extracting the country ISO-2 code in which the starting page was opened;想象一下 StartBasePage 有一个方法提取打开起始页的国家 ISO-2 代码; and there is a boolean method to check if a country code is from a European country:并且有一个 boolean 方法来检查国家代码是否来自欧洲国家:

public <T extends StartBasePage> T openStartPage() {
        if (T.extractCountryFromStartPage().isInEurope()) {
            return (T) pageProvider.createPageAsserted(StartRegistrationPage.class);
        } else {
            return (T) pageProvider.createPageAsserted(StartTermsPage.class);
        }
    }

It is not possible as written, but is there an adaption making this possible?这是不可能的,但是否有适应使这成为可能?

The Null Object Pattern encapsulates behavioral differences like this while keeping the overall interaction the same. Null Object 模式封装了这样的行为差异,同时保持整体交互相同。 The openStartPage method should return a TermsAndConditionsPage whether the user is in Europe or elsewhere.无论用户是在欧洲还是其他地方, openStartPage方法都应该返回一个 TermsAndConditionsPage。 There would be a single sub class of TermsAndConditionsPage, named something like NullTermsAndConditionsPage or MissingTermsAndConditionsPage.将有一个TermsAndConditionsPage 的子class,命名为NullTermsAndConditionsPage 或MissingTermsAndConditionsPage。 Calling the acceptTermsAndConditions() method should accept the terms and conditions, or simply do nothing.调用acceptTermsAndConditions()方法应该接受条款和条件,或者干脆什么都不做。 Then both cases should return the StartRegistrationPage.那么这两种情况都应该返回 StartRegistrationPage。

public class TermsAndConditionsPage {
    private final WebDriver driver;

    public TermsAndConditionsPage(WebDriver driver) {
        this.driver = driver;
    }

    public StartRegistrationPage acceptTermsAndConditions() {
        // Click the "Accept" button and wait

        return new StartRegistrationPage(driver);
    }
}

public class NullTermsAndConditionsPage extends TermsAndConditionsPage {
    @override
     public StartRegistrationPage acceptTermsAndConditions() {
        return new StartRegistrationPage(driver);
    } 
}

Then modify your openStartPage method to detect if the user is in Europe and conditionally return either a new TermsAndConditionsPage or a NullTermsAndConditionsPage:然后修改您的openStartPage方法以检测用户是否在欧洲并有条件地返回新的 TermsAndConditionsPage 或 NullTermsAndConditionsPage:

public TermsAndConditionsPage openStartPage() {
    if (isInEurope()) {
        return new NullTermsAndConditionsPage(driver);
    } else {
        return new TermsAndConditionsPage(driver);
    }
}

Calling and interacting with the openStartPage method is normalized between countries that require a ToS and those that do not: openStartPage方法的调用和交互在需要服务条款的国家和不需要服务条款的国家之间进行标准化:

StartRegistrationPage = openStartPage().acceptTermsAndConditions();

When in Europe, the worse thing that happens is the NullTermsAndConditionsPage.acceptTermsAndConditions() method gets called and does absolutely nothing except return the StartRegistrationPage object, since in those flows that is the page the user lands on.在欧洲,发生的更糟糕的事情是NullTermsAndConditionsPage.acceptTermsAndConditions()方法被调用并且除了返回StartRegistrationPage object 之外什么都不做,因为在这些流程中是用户登陆的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM