有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用于多(并发)测试执行的java多浏览器配置文件?

用于多个(并发)测试执行的多个浏览器配置文件

  1. 这可能吗
  2. 例如,我可以同时执行两个测试,但当两个测试在同一浏览器中同时打开时,它们似乎共享相同的cookie
  3. 请查找下面列出的我的主要浏览器工厂类,有谁能建议修改我的代码或所需设置的最佳方法,使我能够实现我的目标

谢谢你的帮助

public class BrowserFactory implements ISuiteListener {
    private static WebDriver webdriver;

    public static WebDriver getDriver() throws Exception {
        try {
            Properties p = new Properties();
            FileInputStream fi = new FileInputStream(Constant.CONFIG_PROPERTIES_DIRECTORY);
            p.load(fi);

            String browserName = p.getProperty("browser");
            switch (browserName) {

            //firefox setup
            case "firefox":
                if (null == webdriver) {
                    System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
                    webdriver = new FirefoxDriver();
                }
                break;

           //chrome setup
            case "chrome":
                if (null == webdriver) {
                    System.setProperty("webdriver.chrome.driver", Constant.CHROME_DRIVER_DIRECTORY);
                    DesiredCapabilities caps = DesiredCapabilities.chrome();
                    LoggingPreferences logPrefs = new LoggingPreferences();
                    logPrefs.enable(LogType.BROWSER, Level.ALL);
                    caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
                    webdriver = new ChromeDriver(caps);
                }
                break;

           //IE setup   
            case "ie":
                if (null == webdriver) {
                    System.setProperty("webdriver.ie.driver", Constant.IE_DRIVER_DIRECTORY);
                    webdriver = new InternetExplorerDriver();
                }
                break;
            }
        } catch (Exception e) {
            System.out.println("Unable to load browser! - Exception: " + e.getMessage());
        }
        return webdriver;
    }



    @AfterClass
    public void quitDriver() throws Exception {
        if (null != webdriver) {
            getDriver().manage().deleteAllCookies();
            webdriver.quit();
            webdriver = null;
        }

        // Output the time when a test class has ended
        String tempTimeEndClass = new SimpleDateFormat("hh.mm.ss").format(new Date());
        System.out.println("\nTEST CLASS END TIME: " + tempTimeEndClass);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    1. 是的,当然有可能
    2. 它们共享相同的cookies,因为您正在创建的WebDriver实例是静态的,尝试删除静态修改器,因此每次启动WebDriver时,您都会得到一个唯一的实例

    public webdriver driver; public WebDriver getDriver() throws Exception { }

    如果上述内容还不够,并且您希望对配置文件执行其他操作;只需通过xml将其作为参数或作为方法中的字符串变量传递:

    currentProfile = "user-data-dir=/path/to/your/custom/profile";
    ChromeOptions options = new ChromeOptions();
    options.addArguments(currentProfile);
    

    再次强调,这里要小心currentProfile需要是一个实例变量,而不是一个静态变量

    祝你好运