如何运行JUnit测试并行Selenium Grid

时间:2016-07-07 06:48:43

标签: java selenium junit parallel-processing webdriver

我有以下问题:

我正在尝试在我们的selenium网格配置上并行运行我们的selenium测试,我们使用junit作为TestFramework。不幸的是它不起作用...... 我重置了我的分支,以显示基本配置是什么。

  public class WebDriverHelpers {

    private WebDriverHelpers() {
    }

    static String iPhoneUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3";

    /**
     * Creates a WebDriver instance to run the tests locally.
     * 
     * @param browserType
     *            The type of the driver, e.g. Firefox.
     * @return The instance of the driver.
     */
    public static WebDriver getLocalDriver(BrowserType browserType,
            String usProxyUrl) {

        switch (browserType) {
        case CHROME:
           String chromeDir = System.getProperty("user.dir");
           System.setProperty("webdriver.chrome.driver",chromeDir+ "/drivers/chromedriver.exe");
           return new ChromeDriver();
        case FIREFOX:
            String marionetteDir = System.getProperty("user.dir");
            String marionetteDriverLocation = marionetteDir + "/drivers/wires.exe";
            System.setProperty("webdriver.gecko.driver", marionetteDriverLocation);
            return new MarionetteDriver();
        case MOBILE_IPHONE:
            return new FirefoxDriver(getCapabilitiesMobile());
        case INTERNET_EXPLORER:
            return new InternetExplorerDriver();
        case FIREFOX_USPROXY:
            return new FirefoxDriver(getCapabilitiesUsProxy(usProxyUrl));
        case MOBILE_USPROXY:
            return new FirefoxDriver(getCapabilitiesMobileUsProxy(usProxyUrl));
        default:
            throw new UnsupportedOperationException(
                    "The driver for " + browserType + " is not supported.");

        }
    }

    /**
     * Creates a WebDriver instance to run the tests using selenium grid.
     * 
     * @return The instance of the driver.
     */
    public static WebDriver getRemoteDriver(BrowserType browserType,
            String usProxyUrl, String seleniumHubUrl)
                    throws MalformedURLException {

        DesiredCapabilities capabilities = DesiredCapabilities.firefox();

        switch (browserType) {
        case CHROME:
            capabilities = DesiredCapabilities.chrome();
            break;
        case FIREFOX:
            capabilities = DesiredCapabilities.firefox();
            break;
        case MOBILE_IPHONE:
            capabilities = setCapabilitiesMobile(capabilities);
            break;
        case INTERNET_EXPLORER:
            capabilities = DesiredCapabilities.internetExplorer();
            break;
        case FIREFOX_USPROXY:
            capabilities = setCapabilitiesUsProxy(capabilities, usProxyUrl);
            break;
        case MOBILE_USPROXY:
            capabilities = setCapabilitiesMobile(capabilities);
            capabilities = setCapabilitiesUsProxy(capabilities, usProxyUrl);
            break;

        default:
            throw new UnsupportedOperationException(
                    "The driver for " + browserType + " is not supported.");
        }

        return new RemoteWebDriver(new URL(seleniumHubUrl), capabilities);
    }

    private static DesiredCapabilities setCapabilitiesMobile(
            DesiredCapabilities capabilities) {
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("general.useragent.override",
                iPhoneUserAgent);
        capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
        return capabilities;
    }

    private static DesiredCapabilities setCapabilitiesUsProxy(
            DesiredCapabilities capabilities, String usProxyUrl) {
        org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
        proxy.setHttpProxy(usProxyUrl).setFtpProxy(usProxyUrl)
                .setSslProxy(usProxyUrl);
        capabilities.setCapability(CapabilityType.PROXY, proxy);
        return capabilities;
    }

    private static DesiredCapabilities getCapabilitiesMobile() {
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        return setCapabilitiesMobile(capabilities);
    }

    private static DesiredCapabilities getCapabilitiesUsProxy(
            String usProxyUrl) {
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        return setCapabilitiesUsProxy(capabilities, usProxyUrl);
    }

    private static DesiredCapabilities getCapabilitiesMobileUsProxy(
            String usProxyUrl) {
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities = setCapabilitiesMobile(capabilities);
        capabilities = setCapabilitiesUsProxy(capabilities, usProxyUrl);
        return capabilities;
    }

}

这些方法可以读取我们的配置文件并提供特定的webdriver。

    @BeforeClass
    public static void classSetUp() {

        readEnvironmentConfigFromFile(getConfigProperties());
        setEnvironmentUrls();
    }

    /**
     * The JUnit framework calls this method every time before it executes a
     * test method.
     */
    @Before
    public void setUp() throws MalformedURLException {

        backupOldScreenshots();

        /**
         * Use local driver to run the tests on your local machine. / Use remote
         * driver to run the tests via selenium grid.
         */
        if (isRemoteTest) {
            driver = WebDriverHelpers.getRemoteDriver(browserType, usProxyUrl,
                    seleniumHubUrl);
        } else {
            // Note: Firefox is always used for local tests
            driver = WebDriverHelpers.getLocalDriver(browserType, usProxyUrl);
        }

        initializeBrowser();
    enter code here
        page = new StartPage(driver);
    }

    protected void initializeBrowser() {
        driver.manage().deleteAllCookies();
        try {
            driver.manage().window().maximize();
        } catch (Exception e) {
            System.out.println("Could not maximize window.");
        }
    }

This is how one of our testsuites looks like

        @RunWith(Suite.class)
    @Suite.SuiteClasses({ a.class, b.class,
            c.class })
    public class AllTests {

    }

你有线索:-)

0 个答案:

没有答案