如何将两个Java类(Window和Web)合并为一个Java类

时间:2018-02-12 11:04:23

标签: java selenium generics

我正在通过BDD Framework将窗口集成到Web应用程序。我创建了两个驱动程序的Java类(一个用于Window,另一个用于Web),两个驱动程序引用都是公共的。

窗口类:

public class LaunchLogin extends ExecutionerClass {
public static WiniumDriver driver = null;
public static ApplicationPropertiesInitializer applicationProperties = null;
static Properties object,browserLoad = null;
static WebDriverWait wait;
public DesktopOptions options;
public String APP_PATH = "C:\\Windows\\System32\\notepad.exe";
public String WINIUM_PATH = "http://localhost:9999";

public LaunchLogin() {
    if(driver == null)
    {

        applicationProperties = new ApplicationPropertiesInitializer();

        String objectFileName = "src/test/resources/config/object.properties";
        object = loadPropertiesFile(objectFileName);
    }
}

网络课程:

public class LaunchLoginWeb extends ExecutionerClass {
public static WebDriver driver = null;
public static ApplicationPropertiesInitializer applicationProperties = null;
static Properties object,browserLoad = null;
public static WebDriverWait wait;

public LaunchLoginWeb() {
    if(driver == null)
    {
        applicationProperties = new ApplicationPropertiesInitializer();
        initilizeBrowser(applicationProperties.getApplicationPropertiesObject().getProperty("browser"));

        String objectFileName = "src/test/resources/config/object.properties";
        object = loadPropertiesFile(objectFileName);
    }
}

我在通用的帮助下听说过,我们可以将两个班级分成一个Java类。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

鉴于WiniumDriver实现WebDriver,您可以将公共代码计算在超类中,然后有2个子类。

我认为这比在一堂课中拥有一切更好。例如,Web类对这些变量不感兴趣:

public DesktopOptions options;
public String APP_PATH = "C:\\Windows\\System32\\notepad.exe";
public String WINIUM_PATH = "http://localhost:9999";

我会这样做:

public class LaunchLogin extends ExecutionerClass {
  static WebDriver driver = null;
  public static ApplicationPropertiesInitializer applicationProperties = null;
  static Properties object, browserLoad = null;
  static WebDriverWait wait;

  private String objectFileName = "src/test/resources/config/object.properties";

  public LaunchLogin(String type) {
    if(driver == null) {
      applicationProperties = new ApplicationPropertiesInitializer();

      if(type.equals("WEB")) {
        initilizeBrowser(applicationProperties.getApplicationPropertiesObject().getProperty("browser"));
      }
      object = loadPropertiesFile(objectFileName);
    }
  }
}


public class WebLaunchLogin extends LaunchLogin {

  public WebLaunchLogin(String type) {
    super("WEB");
  }

  public static WebDriver getDriver() {
    return driver;
  }
}

public class WiniumLaunchLogin extends LaunchLogin {
  public DesktopOptions options;
  public String APP_PATH = "C:\\Windows\\System32\\notepad.exe";
  public String WINIUM_PATH = "http://localhost:9999";

  public WiniumLaunchLogin() {
    super("WINIUM");
  }

  public static WiniumDriver getDriver() {
    return (WiniumDriver)driver;
  }

}