是否可以动态导入Java中的包?

时间:2018-06-21 09:11:46

标签: java import

我正在Selenium Java上开发自动化测试,以下是笔记本电脑的配置。

操作系统:Windows 7 Ultimate。

我知道java类中的导入行出现在打包行之后,例如

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

,但是有可能导入行根据某些条件而不是硬编码动态显示。当我有菜单时:

Menu image here

,并希望基于发送给被调用方法的用户选择导入并创建驱动程序实例。 喜欢

public static void Setup(String browser)
{
    if(browser=="Chrome")
    {
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.chrome.ChromeDriver;
        System.setProperty("webdriver.chrome.driver", "d:/chromedriver.exe");
        WebDriver cd = new ChromeDriver();
    }
}

3 个答案:

答案 0 :(得分:3)

如果您的目标是“本地化”这些导入,则可以改用完全限定的名称:

public static void setup(String browser) {
    if (browser.equals("Chrome")) {
        System.setProperty("webdriver.chrome.driver", "d:/chromedriver.exe");
        org.openqa.selenium.WebDriver cd = new org.openqa.selenium.chrome.ChromeDriver();
    }
}

否则,不能,无法在类似的类中编写import语句,它们必须放在文件的顶部。

顺便说一句,您应该像上例中那样将String.equals()进行比较。

答案 1 :(得分:0)

如果您不想使用his answer中提到的@pkpnd等完全限定的名称。您可以将代码分成不同的类和一个可以创建Webdriver的接口:

import org.openqa.selenium.WebDriver;

public interface WebDriverFactory{
    WebDriver create();
}

然后是Chrome的实现:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeDriverFactory implements WebDriverFactory{
    public ChromeDriver create(){
        System.setProperty("webdriver.chrome.driver", "d:/chromedriver.exe");
        return new ChromeDriver();
    }
}

在Setup类中,这将为您提供更好的结构和更少的导入:

public static void Setup(String browser) {
    final WebDriverFactory factory;
    switch(browser){
        case "Chrome":
            factory = new ChromeDriverFactory();
            break;
        // other cases for firefox opera ie and so on

       default: 
            throw new UnsupportedOperationException("Browser " + browser + " not supported!");
    }

    final WebDriver d = factory.create();
}

总体而言,您将避免在安装程序类中导入所有不同的驱动程序。但是,您也拆分并移动了创建驱动程序本身的责任。这样,ChromeDriverFactory可以例如添加更多的代码,而不仅仅是设置属性并返回。等等。

答案 2 :(得分:0)

我建议您使用“状态模式”,如下所示:

public interface Driver {
    void doSomething1();

    void doSomething2();

    void doSomething3();

    void doSomething4();

}

public class DriverHandler implements Driver {
    private Driver implem;

    @Override
    public void doSomething1() {
        implem.doSomething1();
    }

    @Override
    public void doSomething2() {
        implem.doSomething2();
    }

    @Override
    public void doSomething3() {
        implem.doSomething3();
    }

    @Override
    public void doSomething4() {
        implem.doSomething4();
    }

    public DriverHandler(String browserName) {
        swithDriver(browserName);
    }

    public synchronized void swithDriver(String browserName) {
        implem = DriverFactory.create(browserName);
    }
}

public class DriverFactory {
    public static final String FIREFOX = "Firefox";
    public static final String CHROME = "Chrome";

    private DriverFactory() {
        // hide default constructor
    }

    public static Driver create(String browserName) {
        switch (browserName) {
        case CHROME:
            return new ChromeDriver();
        case FIREFOX:
            return new FirefoxDriver();
        default:
            throw new IllegalArgumentException();
        }
    }
}

public class ChromeDriver implements Driver {

    // org.openqa.selenium.WebDriver cd;

    public ChromeDriver() {
        // System.setProperty("webdriver.chrome.driver", "d:/chromedriver.exe");
        // cd = new org.openqa.selenium.chrome.ChromeDriver();
    }

    @Override
    public void doSomething1() {
        System.out.println("i do somenthing 1 with Chrome");
    }

    @Override
    public void doSomething2() {
        System.out.println("i do somenthing 2 with Chrome");
    }

    @Override
    public void doSomething3() {
        System.out.println("i do somenthing 3 with Chrome");
    }

    @Override
    public void doSomething4() {
        System.out.println("i do somenthing 4 with Chrome");
    }

}

public class FirefoxDriver implements Driver {

    public FirefoxDriver() {
        // initFireFoxDriver
    }

    @Override
    public void doSomething1() {
        System.out.println("i do somenthing 1 with Firefox");
    }

    @Override
    public void doSomething2() {
        System.out.println("i do somenthing 2 with Firefox");
    }

    @Override
    public void doSomething3() {
        System.out.println("i do somenthing 3 with Firefox");
    }

    @Override
    public void doSomething4() {
        System.out.println("i do somenthing 4 with Firefox");
    }
}

public class Main {

    public static void main(String[] args) {
        DriverHandler handler = new DriverHandler(DriverFactory.CHROME);
        handler.doSomething1();
        handler.doSomething2();
        handler.doSomething4();
        handler.doSomething3();
        handler.swithDriver(DriverFactory.FIREFOX);
        handler.doSomething2();
        handler.doSomething1();
        handler.doSomething3();
        handler.doSomething4();
    }

}

像这样,您可以制作的每个障碍物都是您自己想要的所有导入物的障碍物