我正在尝试将我的类转换为支持builder以便美化我的代码,这是我正在使用的代码,并且我尝试定义名为addSMTPIntegration的方法以使用builder。

这是我的课:

public class IntegrationsPage extends SettingsTab {

    private static final By newIntegrationBth = Locators.findBy("settings_page_integrations_page_add_new_button");
    private IntegrationsTable integrationsTable;
    private SmtpIntegrationForm smtpIntegrationForm;
    private ConfirmPopup confirmPopup;

    public IntegrationsPage(DriverWrapper driver){
        super(driver, "integrations",newIntegrationBth);
        integrationsTable = new IntegrationsTable(driver);
        smtpIntegrationForm = new SmtpIntegrationForm(driver);
        confirmPopup = new ConfirmPopup(driver);
    }

    public void addSMTPIntegration(String name, String server, String port, String fromAddress, boolean mode, String userName, String password){
        clickNewIntegrationButton();
        smtpIntegrationForm.chooseIntegration(IntegrationType.SMTP);
        smtpIntegrationForm.setIntegrationName(name);
        smtpIntegrationForm.setIntegrationServer(server);
        smtpIntegrationForm.setIntegrationPort(port);
        smtpIntegrationForm.setIntegrationFromAddress(fromAddress);
        smtpIntegrationForm.setIntegrationAuth(mode);
        smtpIntegrationForm.setIntegrationUserName(userName);
        smtpIntegrationForm.setIntegrationPassword(password);
        smtpIntegrationForm.clickSaveButton();
        LOG.i("SMTP configuration passed successfully");
    }

    private void clickNewIntegrationButton(){
        clickButton(newIntegrationBth);
    }

    public IntegrationsRow waitIntegrationRowTable(String configurationName) {
        return integrationsTable.waitRowDisplay(configurationName);
    }

    public boolean deleteIntegration(String integrationName) {
        integrationsTable.findRow(integrationName).clickRow();
        integrationsTable.delete(integrationName);
        confirmPopup.clickYes();
        return integrationsTable.findRow(integrationName) == null;
    }
}


我正在尝试定义我的addSMTPIntegration方法以支持builder,从而可以通过以下方式构建它:

smtpIntegrationForm.chooseIntegration(IntegrationType.SMTP).setIntegrationName(name).setIntegrationServer(server) ....等

这是我的smtpIntegrationForm类:

public class SmtpIntegrationForm extends IntegrationCommonSection {


    private static final By integrationServerBy = Locators.findBy("settings_page_integrations_page_integration_server_name_txt");
    private static final By integrationPortBy = Locators.findBy("settings_page_integrations_page_integration_port_txt");
    private static final By integrationFromAddressBy = Locators.findBy("settings_page_integrations_page_integration_from_address_txt");

    SmtpIntegrationForm(DriverWrapper driver){
        super(driver);
    }

    void setIntegrationServer(String server){
        setText(integrationServerBy, server);
    }

    void setIntegrationPort(String port){
        setText(integrationPortBy, port);
    }

    void setIntegrationFromAddress(String address){
        setText(integrationFromAddressBy, address);
    }

    void chooseIntegration(IntegrationType integrationType){
        clickButton(By.cssSelector("li[class~='qa_" + integrationType.value + "']"));
    }


这是IntegrationCommonSection类:

class IntegrationCommonSection extends PageElement {
    private static final By integrationNameBy = Locators.findBy("settings_page_integrations_page_integration_name_txt");
    private static final By integrationAuthBy = Locators.findBy("settings_page_integrations_page_integration_auth_bth");
    private static final By integrationUserNameBy = Locators.findBy("settings_page_integrations_page_integration_username_txt");
    private static final By integrationPasswordBy = Locators.findBy("settings_page_integrations_page_integration_password_txt");
    private static final By integrationSaveBthBy = Locators.findBy("settings_page_integrations_page_integration_save_bth");
    private static final By integrationTestBthBy = Locators.findBy("settings_page_integrations_page_integration_test_bth");

    IntegrationCommonSection(DriverWrapper driver){
        super(driver);
    }

    void setIntegrationName(String name){
        clearAndSetCharacters(integrationNameBy, name);
    }

    void setIntegrationAuth(boolean mode){ //true - with auth, false - no auth
        if(!isCheckBoxEnabled(integrationAuthBy) && mode) {
            clickButton(integrationAuthBy);
        }
    }
    void setIntegrationUserName(String userName){
        setText(integrationUserNameBy, userName);
    }

    void setIntegrationPassword(String password){
        setText(integrationPasswordBy, password);
    }

    void clickSaveButton(){
        clickButton(integrationSaveBthBy);
    }

    void clickTestButton(){
        clickButton(integrationTestBthBy);
    }

}

最佳答案

为此,您需要使用SmtpIntegrationForm对象的返回值修改this的方法,以便可以在构建器模式中构造语句。您无法在addSMTPIntegration()方法中实现此目的。

chooseIntegration(), setIntegrationName(), setIntegrationServer(), etc.中的SmtpIntegrationForm方法应具有SmtpIntegrationForm的返回类型,并且这些方法中的最后一条语句应为return this;,以实现此目的。

进行如下更改:

public class SmtpIntegrationForm extends IntegrationCommonSection {


    private static final By integrationServerBy = Locators.findBy("settings_page_integrations_page_integration_server_name_txt");
    private static final By integrationPortBy = Locators.findBy("settings_page_integrations_page_integration_port_txt");
    private static final By integrationFromAddressBy = Locators.findBy("settings_page_integrations_page_integration_from_address_txt");

    SmtpIntegrationForm(DriverWrapper driver){
        super(driver);
    }

    SmtpIntegrationForm setIntegrationServer(String server){
        setText(integrationServerBy, server);
        return this;
    }

    SmtpIntegrationForm setIntegrationPort(String port){
        setText(integrationPortBy, port);
        return this;
    }

    SmtpIntegrationForm setIntegrationFromAddress(String address){
        setText(integrationFromAddressBy, address);
        return this;
    }

    SmtpIntegrationForm chooseIntegration(IntegrationType integrationType){
        clickButton(By.cssSelector("li[class~='qa_" + integrationType.value + "']"));
        return this;
    }
}

07-27 14:00