From dcb95565617d6ab5577fa42aef87ecd51f041782 Mon Sep 17 00:00:00 2001 From: Sebastian Saip Date: Thu, 29 Nov 2012 23:59:19 +0100 Subject: [PATCH 1/4] removed unused imports --- .../web/config/WebBrowserCacheConfig.java | 115 ++++---- .../web/config/WebBrowserTypeConfig.java | 109 ++++--- .../protocol/web/util/BrowserFactory.java | 278 +++++++++--------- 3 files changed, 249 insertions(+), 253 deletions(-) diff --git a/src/protocol/web/org/apache/jmeter/protocol/web/config/WebBrowserCacheConfig.java b/src/protocol/web/org/apache/jmeter/protocol/web/config/WebBrowserCacheConfig.java index 0f888a07747..9df55df0794 100644 --- a/src/protocol/web/org/apache/jmeter/protocol/web/config/WebBrowserCacheConfig.java +++ b/src/protocol/web/org/apache/jmeter/protocol/web/config/WebBrowserCacheConfig.java @@ -1,58 +1,57 @@ -package org.apache.jmeter.protocol.web.config; - -import org.apache.jmeter.config.ConfigTestElement; -import org.apache.jmeter.engine.event.LoopIterationEvent; -import org.apache.jmeter.engine.event.LoopIterationListener; -import org.apache.jmeter.protocol.web.util.BrowserFactory; -import org.apache.jmeter.testbeans.TestBean; -import org.apache.jmeter.testelement.TestListener; -import org.apache.jorphan.logging.LoggingManager; -import org.apache.log.Logger; - -public class WebBrowserCacheConfig extends ConfigTestElement implements TestBean, TestListener { - private static final Logger LOGGER = LoggingManager.getLoggerForClass(); - - private static final long serialVersionUID = -6579029558490898888L; - - private static final String CACHE_SETTINGS = "WebBrowserCacheConfig.cacheSettings"; - - public WebBrowserCacheConfig() { - } - - public String getCacheSettings() { - return getPropertyAsString(CACHE_SETTINGS); - } - - public void setCacheSettings(String cacheSettings) { - LOGGER.info("Setting to: "+cacheSettings); - setProperty(CACHE_SETTINGS, cacheSettings); - } - - @Override - public void testStarted() { - LOGGER.info("Cache settings: "+getCacheSettings()); - if(WebBrowserCacheConfigBeanInfo.CLEAR_ALL.equals(getCacheSettings())) { - BrowserFactory.getInstance().clearBrowser(); - } else if(WebBrowserCacheConfigBeanInfo.CLEAR_COOKIES.equals(getCacheSettings())) { - BrowserFactory.getInstance().clearBrowserCookies(); - } - } - - @Override - public void testStarted(String host) { - testStarted(); - } - - @Override - public void testEnded() { - } - - @Override - public void testEnded(String host) { - } - - @Override - public void testIterationStart(LoopIterationEvent event) { - testStarted(); - } -} +package org.apache.jmeter.protocol.web.config; + +import org.apache.jmeter.config.ConfigTestElement; +import org.apache.jmeter.engine.event.LoopIterationEvent; +import org.apache.jmeter.protocol.web.util.BrowserFactory; +import org.apache.jmeter.testbeans.TestBean; +import org.apache.jmeter.testelement.TestListener; +import org.apache.jorphan.logging.LoggingManager; +import org.apache.log.Logger; + +public class WebBrowserCacheConfig extends ConfigTestElement implements TestBean, TestListener { + private static final Logger LOGGER = LoggingManager.getLoggerForClass(); + + private static final long serialVersionUID = -6579029558490898888L; + + private static final String CACHE_SETTINGS = "WebBrowserCacheConfig.cacheSettings"; + + public WebBrowserCacheConfig() { + } + + public String getCacheSettings() { + return getPropertyAsString(CACHE_SETTINGS); + } + + public void setCacheSettings(String cacheSettings) { + LOGGER.info("Setting to: "+cacheSettings); + setProperty(CACHE_SETTINGS, cacheSettings); + } + + @Override + public void testStarted() { + LOGGER.info("Cache settings: "+getCacheSettings()); + if(WebBrowserCacheConfigBeanInfo.CLEAR_ALL.equals(getCacheSettings())) { + BrowserFactory.getInstance().clearBrowser(); + } else if(WebBrowserCacheConfigBeanInfo.CLEAR_COOKIES.equals(getCacheSettings())) { + BrowserFactory.getInstance().clearBrowserCookies(); + } + } + + @Override + public void testStarted(String host) { + testStarted(); + } + + @Override + public void testEnded() { + } + + @Override + public void testEnded(String host) { + } + + @Override + public void testIterationStart(LoopIterationEvent event) { + testStarted(); + } +} diff --git a/src/protocol/web/org/apache/jmeter/protocol/web/config/WebBrowserTypeConfig.java b/src/protocol/web/org/apache/jmeter/protocol/web/config/WebBrowserTypeConfig.java index f2d06f3bc75..b76d8960369 100644 --- a/src/protocol/web/org/apache/jmeter/protocol/web/config/WebBrowserTypeConfig.java +++ b/src/protocol/web/org/apache/jmeter/protocol/web/config/WebBrowserTypeConfig.java @@ -1,55 +1,54 @@ -package org.apache.jmeter.protocol.web.config; - -import org.apache.jmeter.config.ConfigTestElement; -import org.apache.jmeter.engine.event.LoopIterationEvent; -import org.apache.jmeter.engine.event.LoopIterationListener; -import org.apache.jmeter.protocol.web.util.BrowserFactory; -import org.apache.jmeter.protocol.web.util.BrowserType; -import org.apache.jmeter.testbeans.TestBean; -import org.apache.jmeter.testelement.TestListener; -import org.apache.jorphan.logging.LoggingManager; -import org.apache.log.Logger; - -public class WebBrowserTypeConfig extends ConfigTestElement implements TestBean, TestListener { - private static final Logger LOGGER = LoggingManager.getLoggerForClass(); - - private static final long serialVersionUID = -1257902955849089888L; - - private static final String TYPE = "WebBrowserTypeConfig.type"; - - public WebBrowserTypeConfig() { - } - - public String getType() { - return getPropertyAsString(TYPE); - } - - public void setType(String type) { - LOGGER.info("setType: "+type); - setProperty(TYPE, type); - } - - @Override - public void testStarted() { - BrowserFactory.getInstance().setBrowserType(BrowserType.valueOf(getType())); - } - - @Override - public void testStarted(String host) { - testStarted(); - } - - @Override - public void testEnded() { - } - - @Override - public void testEnded(String host) { - } - - @Override - public void testIterationStart(LoopIterationEvent event) { - LOGGER.info("Setting browser to: "+getType()); - BrowserFactory.getInstance().setBrowserType(BrowserType.valueOf(getType())); - } -} +package org.apache.jmeter.protocol.web.config; + +import org.apache.jmeter.config.ConfigTestElement; +import org.apache.jmeter.engine.event.LoopIterationEvent; +import org.apache.jmeter.protocol.web.util.BrowserFactory; +import org.apache.jmeter.protocol.web.util.BrowserType; +import org.apache.jmeter.testbeans.TestBean; +import org.apache.jmeter.testelement.TestListener; +import org.apache.jorphan.logging.LoggingManager; +import org.apache.log.Logger; + +public class WebBrowserTypeConfig extends ConfigTestElement implements TestBean, TestListener { + private static final Logger LOGGER = LoggingManager.getLoggerForClass(); + + private static final long serialVersionUID = -1257902955849089888L; + + private static final String TYPE = "WebBrowserTypeConfig.type"; + + public WebBrowserTypeConfig() { + } + + public String getType() { + return getPropertyAsString(TYPE); + } + + public void setType(String type) { + LOGGER.info("setType: "+type); + setProperty(TYPE, type); + } + + @Override + public void testStarted() { + BrowserFactory.getInstance().setBrowserType(BrowserType.valueOf(getType())); + } + + @Override + public void testStarted(String host) { + testStarted(); + } + + @Override + public void testEnded() { + } + + @Override + public void testEnded(String host) { + } + + @Override + public void testIterationStart(LoopIterationEvent event) { + LOGGER.info("Setting browser to: "+getType()); + BrowserFactory.getInstance().setBrowserType(BrowserType.valueOf(getType())); + } +} diff --git a/src/protocol/web/org/apache/jmeter/protocol/web/util/BrowserFactory.java b/src/protocol/web/org/apache/jmeter/protocol/web/util/BrowserFactory.java index 0b6d8e4fc40..c6564a88110 100644 --- a/src/protocol/web/org/apache/jmeter/protocol/web/util/BrowserFactory.java +++ b/src/protocol/web/org/apache/jmeter/protocol/web/util/BrowserFactory.java @@ -1,140 +1,138 @@ -package org.apache.jmeter.protocol.web.util; - -import org.apache.jorphan.logging.LoggingManager; -import org.apache.log.Logger; -import org.openqa.selenium.Proxy; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.android.AndroidDriver; -import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.chrome.ChromeDriverService; -import org.openqa.selenium.chrome.ChromeOptions; -import org.openqa.selenium.firefox.FirefoxDriver; -import org.openqa.selenium.remote.CapabilityType; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.RemoteWebDriver; - -import java.io.File; -import java.io.IOException; - -/** - * This is responsible for accessing (and unsetting) a WebDriver browser instance per thread. - */ -public class BrowserFactory { - private static final Logger LOGGER = LoggingManager.getLoggerForClass(); - - /** - * Each thread will reference their WebDriver (browser) instance via this ThreadLocal instance. This is - * initialised in the {@see #threadStarted()} and quit & unset in {@see #threadFinished()}. - */ - private static final ThreadLocal BROWSERS = new ThreadLocal(); - - private static final ThreadLocal SERVICES = new ThreadLocal(); - private static final ThreadLocal PROXIES = new ThreadLocal(); - - private static final ThreadLocal BROWSER_TYPES = new ThreadLocal() { - @Override - public BrowserType initialValue() { - return BrowserType.CHROME; - } - }; - - private static final BrowserFactory INSTANCE = new BrowserFactory(); - - public static BrowserFactory getInstance() { - return INSTANCE; - } - - private BrowserFactory() {} - - /** - * Call this method to get a WebDriver (browser) for the current thread. The returned browser instance will be - * stored and returned on subsequent calls until {@see #clearBrowser()} is called. - * - * @return a thread specific WebDriver instance. - */ - public WebDriver getBrowser() { - if(BROWSERS.get() == null) { - BROWSERS.set(createBrowser()); - } - - return BROWSERS.get(); - } - - private WebDriver createBrowser() { - BrowserType browserType = BROWSER_TYPES.get(); - if(BrowserType.CHROME == browserType) { - initialiseService(); - DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome(); - desiredCapabilities.setCapability(CapabilityType.PROXY, getProxy()); - return new RemoteWebDriver(SERVICES.get().getUrl(), desiredCapabilities); - } - else if(BrowserType.ANDROID == browserType) { - return new AndroidDriver(); - } - else { - DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox(); - desiredCapabilities.setCapability(CapabilityType.PROXY, getProxy()); - return new FirefoxDriver(desiredCapabilities); - } - } - - private static void initialiseService() { - if(SERVICES.get() == null) { - SERVICES.set(new ChromeDriverService.Builder().usingChromeDriverExecutable(new File(System.getProperty("webdriver.chrome.driver"))).usingAnyFreePort().build()); - try { - SERVICES.get().start(); - } catch (IOException e) { - throw new RuntimeException("Failed to start chrome service", e); - } - } - } - - /** - * Removes all cookies in the current browser used by the running thread. - */ - public void clearBrowserCookies() { - getBrowser().manage().deleteAllCookies(); - } - - /** - * Removes any WebDriver instance associated with the calling thread and quits the running browser instance. - */ - public void clearBrowser() { - if(BROWSERS.get() != null) { - BROWSERS.get().quit(); - BROWSERS.remove(); - } - - if(SERVICES.get() != null) { - SERVICES.get().stop(); - SERVICES.set(null); - } - } - - /** - * Use this to set the proxy to use when getting/creating new WebDriver instances {#getBrowser}. Unlike the browsers - * this setting spans across threads, so there is no per thread configured values. - * - * @param proxy is the proxy to use when {#getBrowser} is invoked. - */ - public void setProxy(Proxy proxy) { - PROXIES.set(proxy); - } - - /** - * Access the proxy configured for all browsers accessed from this factory. - * - * @return the configured proxy. - */ - public Proxy getProxy() { - return PROXIES.get(); - } - - public void setBrowserType(BrowserType browserType) { - BROWSER_TYPES.set(browserType); - } - - public BrowserType getBrowserType() { - return BROWSER_TYPES.get(); - } -} +package org.apache.jmeter.protocol.web.util; + +import org.apache.jorphan.logging.LoggingManager; +import org.apache.log.Logger; +import org.openqa.selenium.Proxy; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.android.AndroidDriver; +import org.openqa.selenium.chrome.ChromeDriverService; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.remote.CapabilityType; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + +import java.io.File; +import java.io.IOException; + +/** + * This is responsible for accessing (and unsetting) a WebDriver browser instance per thread. + */ +public class BrowserFactory { + private static final Logger LOGGER = LoggingManager.getLoggerForClass(); + + /** + * Each thread will reference their WebDriver (browser) instance via this ThreadLocal instance. This is + * initialised in the {@see #threadStarted()} and quit & unset in {@see #threadFinished()}. + */ + private static final ThreadLocal BROWSERS = new ThreadLocal(); + + private static final ThreadLocal SERVICES = new ThreadLocal(); + private static final ThreadLocal PROXIES = new ThreadLocal(); + + private static final ThreadLocal BROWSER_TYPES = new ThreadLocal() { + @Override + public BrowserType initialValue() { + return BrowserType.CHROME; + } + }; + + private static final BrowserFactory INSTANCE = new BrowserFactory(); + + public static BrowserFactory getInstance() { + return INSTANCE; + } + + private BrowserFactory() {} + + /** + * Call this method to get a WebDriver (browser) for the current thread. The returned browser instance will be + * stored and returned on subsequent calls until {@see #clearBrowser()} is called. + * + * @return a thread specific WebDriver instance. + */ + public WebDriver getBrowser() { + if(BROWSERS.get() == null) { + BROWSERS.set(createBrowser()); + } + + return BROWSERS.get(); + } + + private WebDriver createBrowser() { + BrowserType browserType = BROWSER_TYPES.get(); + if(BrowserType.CHROME == browserType) { + initialiseService(); + DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome(); + desiredCapabilities.setCapability(CapabilityType.PROXY, getProxy()); + return new RemoteWebDriver(SERVICES.get().getUrl(), desiredCapabilities); + } + else if(BrowserType.ANDROID == browserType) { + return new AndroidDriver(); + } + else { + DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox(); + desiredCapabilities.setCapability(CapabilityType.PROXY, getProxy()); + return new FirefoxDriver(desiredCapabilities); + } + } + + private static void initialiseService() { + if(SERVICES.get() == null) { + SERVICES.set(new ChromeDriverService.Builder().usingChromeDriverExecutable(new File(System.getProperty("webdriver.chrome.driver"))).usingAnyFreePort().build()); + try { + SERVICES.get().start(); + } catch (IOException e) { + throw new RuntimeException("Failed to start chrome service", e); + } + } + } + + /** + * Removes all cookies in the current browser used by the running thread. + */ + public void clearBrowserCookies() { + getBrowser().manage().deleteAllCookies(); + } + + /** + * Removes any WebDriver instance associated with the calling thread and quits the running browser instance. + */ + public void clearBrowser() { + if(BROWSERS.get() != null) { + BROWSERS.get().quit(); + BROWSERS.remove(); + } + + if(SERVICES.get() != null) { + SERVICES.get().stop(); + SERVICES.set(null); + } + } + + /** + * Use this to set the proxy to use when getting/creating new WebDriver instances {#getBrowser}. Unlike the browsers + * this setting spans across threads, so there is no per thread configured values. + * + * @param proxy is the proxy to use when {#getBrowser} is invoked. + */ + public void setProxy(Proxy proxy) { + PROXIES.set(proxy); + } + + /** + * Access the proxy configured for all browsers accessed from this factory. + * + * @return the configured proxy. + */ + public Proxy getProxy() { + return PROXIES.get(); + } + + public void setBrowserType(BrowserType browserType) { + BROWSER_TYPES.set(browserType); + } + + public BrowserType getBrowserType() { + return BROWSER_TYPES.get(); + } +} From 13b7924e1efb0794c41e5049532d8c43b3d07fa0 Mon Sep 17 00:00:00 2001 From: Sebastian Saip Date: Fri, 30 Nov 2012 00:00:17 +0100 Subject: [PATCH 2/4] added web-sampler and selenium jars --- eclipse.classpath | 187 ++++++++++++++++++++++++---------------------- 1 file changed, 98 insertions(+), 89 deletions(-) diff --git a/eclipse.classpath b/eclipse.classpath index 6ad60df597b..e01939a79f9 100644 --- a/eclipse.classpath +++ b/eclipse.classpath @@ -1,89 +1,98 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 677fe7099de7f9035598da9346b197ce33cd4b08 Mon Sep 17 00:00:00 2001 From: Sebastian Saip Date: Fri, 30 Nov 2012 00:09:34 +0100 Subject: [PATCH 3/4] added all jars to classpath --- eclipse.classpath | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/eclipse.classpath b/eclipse.classpath index e01939a79f9..d0cd7f5aab3 100644 --- a/eclipse.classpath +++ b/eclipse.classpath @@ -48,8 +48,10 @@ + + @@ -62,6 +64,7 @@ + @@ -69,12 +72,16 @@ + + + + @@ -91,8 +98,22 @@ - + + + + + + + + + + + + + + + - + \ No newline at end of file From 027c20b8b27831e3f399e12fbca47155ac2aa09b Mon Sep 17 00:00:00 2001 From: Sebastian Saip Date: Fri, 30 Nov 2012 00:12:37 +0100 Subject: [PATCH 4/4] removed unused imports --- .../web/config/WebBrowserTypeConfigTest.java | 120 ++--- .../protocol/web/util/BrowserFactoryTest.java | 500 +++++++++--------- 2 files changed, 306 insertions(+), 314 deletions(-) diff --git a/test/src/org/apache/jmeter/protocol/web/config/WebBrowserTypeConfigTest.java b/test/src/org/apache/jmeter/protocol/web/config/WebBrowserTypeConfigTest.java index 79bf3eb3b19..9e5f2c2844d 100644 --- a/test/src/org/apache/jmeter/protocol/web/config/WebBrowserTypeConfigTest.java +++ b/test/src/org/apache/jmeter/protocol/web/config/WebBrowserTypeConfigTest.java @@ -1,63 +1,57 @@ -package org.apache.jmeter.protocol.web.config; - -import org.apache.jmeter.protocol.web.util.BrowserFactory; -import org.apache.jmeter.protocol.web.util.BrowserType; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.*; - -public class WebBrowserTypeConfigTest { - private WebBrowserTypeConfig typeConfig; - - @Before - public void setUp() { - typeConfig = new WebBrowserTypeConfig(); - } - - @Test - public void shouldBeAbleToReadSamePropertiesFromConfigAfterDeserialisation() throws IOException, ClassNotFoundException { - ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); - ObjectOutputStream outputStream = new ObjectOutputStream(byteArray); - - final String type = "cache setting value"; - - typeConfig.setType(type); - - outputStream.writeObject(typeConfig); - outputStream.flush(); - - ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(byteArray.toByteArray())); - WebBrowserTypeConfig deserialisedtypeConfig = (WebBrowserTypeConfig)inputStream.readObject(); - - assertThat(deserialisedtypeConfig.getType(), is(type)); - } - - @Test - public void shouldUseChromeWhenSpecified() throws Exception { - typeConfig.setType(WebBrowserTypeConfigBeanInfo.CHROME); - typeConfig.testIterationStart(null); - - assertThat(BrowserFactory.getInstance().getBrowserType(), is(BrowserType.CHROME)); - } - - @Test - public void shouldUseFirefoxWhenSpecified() throws Exception { - typeConfig.setType(WebBrowserTypeConfigBeanInfo.FIREFOX); - typeConfig.testIterationStart(null); - - assertThat(BrowserFactory.getInstance().getBrowserType(), is(BrowserType.FIREFOX)); - } -} +package org.apache.jmeter.protocol.web.config; + +import org.apache.jmeter.protocol.web.util.BrowserFactory; +import org.apache.jmeter.protocol.web.util.BrowserType; +import org.junit.Before; +import org.junit.Test; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class WebBrowserTypeConfigTest { + private WebBrowserTypeConfig typeConfig; + + @Before + public void setUp() { + typeConfig = new WebBrowserTypeConfig(); + } + + @Test + public void shouldBeAbleToReadSamePropertiesFromConfigAfterDeserialisation() throws IOException, ClassNotFoundException { + ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); + ObjectOutputStream outputStream = new ObjectOutputStream(byteArray); + + final String type = "cache setting value"; + + typeConfig.setType(type); + + outputStream.writeObject(typeConfig); + outputStream.flush(); + + ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(byteArray.toByteArray())); + WebBrowserTypeConfig deserialisedtypeConfig = (WebBrowserTypeConfig)inputStream.readObject(); + + assertThat(deserialisedtypeConfig.getType(), is(type)); + } + + @Test + public void shouldUseChromeWhenSpecified() throws Exception { + typeConfig.setType(WebBrowserTypeConfigBeanInfo.CHROME); + typeConfig.testIterationStart(null); + + assertThat(BrowserFactory.getInstance().getBrowserType(), is(BrowserType.CHROME)); + } + + @Test + public void shouldUseFirefoxWhenSpecified() throws Exception { + typeConfig.setType(WebBrowserTypeConfigBeanInfo.FIREFOX); + typeConfig.testIterationStart(null); + + assertThat(BrowserFactory.getInstance().getBrowserType(), is(BrowserType.FIREFOX)); + } +} diff --git a/test/src/org/apache/jmeter/protocol/web/util/BrowserFactoryTest.java b/test/src/org/apache/jmeter/protocol/web/util/BrowserFactoryTest.java index b095a285763..d3085d9e223 100644 --- a/test/src/org/apache/jmeter/protocol/web/util/BrowserFactoryTest.java +++ b/test/src/org/apache/jmeter/protocol/web/util/BrowserFactoryTest.java @@ -1,251 +1,249 @@ -package org.apache.jmeter.protocol.web.util; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; -import org.openqa.selenium.Capabilities; -import org.openqa.selenium.Proxy; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.android.AndroidDriver; -import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.chrome.ChromeDriverService; -import org.openqa.selenium.chrome.ChromeOptions; -import org.openqa.selenium.firefox.FirefoxDriver; -import org.openqa.selenium.remote.CapabilityType; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.util.List; -import java.util.Vector; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.assertThat; -import static org.junit.matchers.JUnitMatchers.hasItem; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.isA; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.*; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(BrowserFactory.class) -public class BrowserFactoryTest { - /** - * Used to store the browsers created by {#BrowserCreator} threads. - */ - private final List browsers = new Vector(); - - /** - * The run method will access a browser from the factory and add it to the {#browsers} list. - */ - private class BrowserCreator implements Runnable { - - private BrowserType type; - private Proxy proxy; - - public BrowserCreator(BrowserType type) { - this(type, null); - } - - public BrowserCreator(Proxy proxy) { - this(BrowserType.FIREFOX, proxy); - } - - public BrowserCreator(BrowserType type, Proxy proxy) { - this.type = type; - this.proxy = proxy; - } - - @Override - public void run() { - BrowserFactoryTest.this.factory.setBrowserType(type); - BrowserFactoryTest.this.factory.setProxy(proxy); - browsers.add(BrowserFactoryTest.this.factory.getBrowser()); - } - }; - - private BrowserFactory factory; - - @Before - public void initFactory() { - factory = BrowserFactory.getInstance(); - factory.setBrowserType(BrowserType.FIREFOX); - factory.setProxy(null); - } - - @After - public void clearBrowsers() { - factory.clearBrowser(); - for(WebDriver browser: browsers) { - browser.quit(); - } - browsers.clear(); - } - - @Test - public void shouldReturnTheSameBrowserWhenSubsequentGetBrowserIsInvoked() throws Exception { - FirefoxDriver mockBrowser = mock(FirefoxDriver.class); - whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(Capabilities.class)).thenReturn(mockBrowser); - - WebDriver firstBrowser = factory.getBrowser(); - WebDriver secondBrowser = factory.getBrowser(); - assertThat(firstBrowser, is(sameInstance(secondBrowser))); - - verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); - } - - @Test - public void shouldClearCookiesOnCurrentBrowserWhenClearCookiesIsInvoked() throws Exception { - FirefoxDriver mockBrowser = mock(FirefoxDriver.class); - whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(Capabilities.class)).thenReturn(mockBrowser); - WebDriver.Options mockOptions = mock(WebDriver.Options.class); - when(mockBrowser.manage()).thenReturn(mockOptions); - - factory.clearBrowserCookies(); - - verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); - verify(mockOptions).deleteAllCookies(); - } - - @Test - public void shouldBrowserShouldContainProxySettingsWhenSpecified() throws Exception { - FirefoxDriver mockBrowser = mock(FirefoxDriver.class); - whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(mockBrowser); - - factory.setProxy(new Proxy()); - WebDriver browser = factory.getBrowser(); - assertThat(browser, is(not(nullValue()))); - - verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); - } - - @Test - public void shouldReturnNewBrowserWhenClearBrowserIsInvoked() throws Exception { - FirefoxDriver firstBrowser = mock(FirefoxDriver.class); - FirefoxDriver secondBrowser = mock(FirefoxDriver.class); - whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(firstBrowser, secondBrowser); - - WebDriver beforeReset = factory.getBrowser(); - factory.clearBrowser(); - WebDriver afterReset = factory.getBrowser(); - - assertThat(afterReset, is(not(sameInstance(beforeReset)))); - - verifyNew(FirefoxDriver.class, Mockito.times(2)).withArguments(isA(DesiredCapabilities.class)); - } - - @Test - public void shouldReturnDifferentBrowserWhenCalledFromSeparateThreads() throws Exception { - FirefoxDriver firstBrowser = mock(FirefoxDriver.class); - FirefoxDriver secondBrowser = mock(FirefoxDriver.class); - whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(firstBrowser, secondBrowser); - - Thread firstThread = new Thread(this.new BrowserCreator(BrowserType.FIREFOX)); - Thread secondThread = new Thread(this.new BrowserCreator(BrowserType.FIREFOX)); - - firstThread.start(); - secondThread.start(); - - firstThread.join(); - secondThread.join(); - - assertThat(browsers.size(), is(2)); - assertThat(browsers.get(0), is(not(sameInstance(browsers.get(1))))); - - verifyNew(FirefoxDriver.class, Mockito.times(2)).withArguments(isA(DesiredCapabilities.class)); - } - - @Test - public void shouldBeAbleToSetBrowserTypeFromSeparateThread() throws Exception { - FirefoxDriver firstBrowser = mock(FirefoxDriver.class); - ChromeDriver secondBrowser = mock(ChromeDriver.class); - whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(firstBrowser); - whenNew(ChromeDriver.class).withParameterTypes(ChromeOptions.class).withArguments(isA(ChromeOptions.class)).thenReturn(secondBrowser); - - Thread firstThread = new Thread(this.new BrowserCreator(BrowserType.FIREFOX)); - Thread secondThread = new Thread(this.new BrowserCreator(BrowserType.CHROME)); - - firstThread.start(); - secondThread.start(); - - firstThread.join(); - secondThread.join(); - - assertThat(browsers.size(), is(2)); - assertThat(browsers.get(0), is(not(sameInstance(browsers.get(1))))); - - verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); - verifyNew(ChromeDriver.class, Mockito.times(1)).withArguments(isA(ChromeOptions.class)); - } - - @Test - @PrepareForTest(value = {DesiredCapabilities.class, BrowserFactory.class}) - public void shouldBeAbleToSetProxyFromSeparateThread() throws Exception { - FirefoxDriver firstBrowser = mock(FirefoxDriver.class); - FirefoxDriver secondBrowser = mock(FirefoxDriver.class); - ArgumentCaptor proxyArgumentCaptor = ArgumentCaptor.forClass(Proxy.class); - PowerMockito.mockStatic(DesiredCapabilities.class); - DesiredCapabilities desiredCapabilities = mock(DesiredCapabilities.class); - when(DesiredCapabilities.firefox()).thenReturn(desiredCapabilities); - whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(desiredCapabilities).thenReturn(firstBrowser); - whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(desiredCapabilities).thenReturn(secondBrowser); - - Proxy firstProxy = new Proxy(); - Proxy secondProxy = new Proxy(); - Thread firstThread = new Thread(this.new BrowserCreator(firstProxy)); - Thread secondThread = new Thread(this.new BrowserCreator(secondProxy)); - - firstThread.start(); - secondThread.start(); - - firstThread.join(); - secondThread.join(); - - assertThat(browsers.size(), is(2)); - - verifyNew(FirefoxDriver.class, Mockito.times(2)).withArguments(desiredCapabilities); - verify(desiredCapabilities, Mockito.atLeastOnce()).setCapability(eq(CapabilityType.PROXY), proxyArgumentCaptor.capture()); - - assertThat(proxyArgumentCaptor.getAllValues(), hasItem(firstProxy)); - assertThat(proxyArgumentCaptor.getAllValues(), hasItem(secondProxy)); - } - - @Test - public void shouldReturnFirefoxBrowserWhenNoneSpecified() throws Exception { - FirefoxDriver mockBrowser = mock(FirefoxDriver.class); - whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(mockBrowser); - - assertThat(factory.getBrowser(), is(sameInstance((WebDriver) mockBrowser))); - - verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); - } - - @Test - public void shouldReturnChromeBrowserWhenSpecified() throws Exception { - ChromeDriver mockBrowser = mock(ChromeDriver.class); - whenNew(ChromeDriver.class).withParameterTypes(ChromeOptions.class).withArguments(isA(ChromeOptions.class)).thenReturn(mockBrowser); - - factory.setBrowserType(BrowserType.CHROME); - assertThat(factory.getBrowser(), is(sameInstance((WebDriver)mockBrowser))); - - verifyNew(ChromeDriver.class, Mockito.times(1)).withArguments(isA(ChromeOptions.class)); - } - - @Test - public void shouldReturnAndroidBrowserWhenSpecified() throws Exception { - AndroidDriver mockBrowser = mock(AndroidDriver.class); - whenNew(AndroidDriver.class).withNoArguments().thenReturn(mockBrowser); - - factory.setBrowserType(BrowserType.ANDROID); - assertThat(factory.getBrowser(), is(sameInstance((WebDriver)mockBrowser))); - - verifyNew(AndroidDriver.class, Mockito.times(1)).withNoArguments(); - } -} +package org.apache.jmeter.protocol.web.util; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.openqa.selenium.Capabilities; +import org.openqa.selenium.Proxy; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.android.AndroidDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.remote.CapabilityType; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.List; +import java.util.Vector; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; +import static org.junit.matchers.JUnitMatchers.hasItem; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(BrowserFactory.class) +public class BrowserFactoryTest { + /** + * Used to store the browsers created by {#BrowserCreator} threads. + */ + private final List browsers = new Vector(); + + /** + * The run method will access a browser from the factory and add it to the {#browsers} list. + */ + private class BrowserCreator implements Runnable { + + private BrowserType type; + private Proxy proxy; + + public BrowserCreator(BrowserType type) { + this(type, null); + } + + public BrowserCreator(Proxy proxy) { + this(BrowserType.FIREFOX, proxy); + } + + public BrowserCreator(BrowserType type, Proxy proxy) { + this.type = type; + this.proxy = proxy; + } + + @Override + public void run() { + BrowserFactoryTest.this.factory.setBrowserType(type); + BrowserFactoryTest.this.factory.setProxy(proxy); + browsers.add(BrowserFactoryTest.this.factory.getBrowser()); + } + } + + private BrowserFactory factory; + + @Before + public void initFactory() { + factory = BrowserFactory.getInstance(); + factory.setBrowserType(BrowserType.FIREFOX); + factory.setProxy(null); + } + + @After + public void clearBrowsers() { + factory.clearBrowser(); + for(WebDriver browser: browsers) { + browser.quit(); + } + browsers.clear(); + } + + @Test + public void shouldReturnTheSameBrowserWhenSubsequentGetBrowserIsInvoked() throws Exception { + FirefoxDriver mockBrowser = mock(FirefoxDriver.class); + whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(Capabilities.class)).thenReturn(mockBrowser); + + WebDriver firstBrowser = factory.getBrowser(); + WebDriver secondBrowser = factory.getBrowser(); + assertThat(firstBrowser, is(sameInstance(secondBrowser))); + + verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); + } + + @Test + public void shouldClearCookiesOnCurrentBrowserWhenClearCookiesIsInvoked() throws Exception { + FirefoxDriver mockBrowser = mock(FirefoxDriver.class); + whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(Capabilities.class)).thenReturn(mockBrowser); + WebDriver.Options mockOptions = mock(WebDriver.Options.class); + when(mockBrowser.manage()).thenReturn(mockOptions); + + factory.clearBrowserCookies(); + + verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); + verify(mockOptions).deleteAllCookies(); + } + + @Test + public void shouldBrowserShouldContainProxySettingsWhenSpecified() throws Exception { + FirefoxDriver mockBrowser = mock(FirefoxDriver.class); + whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(mockBrowser); + + factory.setProxy(new Proxy()); + WebDriver browser = factory.getBrowser(); + assertThat(browser, is(not(nullValue()))); + + verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); + } + + @Test + public void shouldReturnNewBrowserWhenClearBrowserIsInvoked() throws Exception { + FirefoxDriver firstBrowser = mock(FirefoxDriver.class); + FirefoxDriver secondBrowser = mock(FirefoxDriver.class); + whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(firstBrowser, secondBrowser); + + WebDriver beforeReset = factory.getBrowser(); + factory.clearBrowser(); + WebDriver afterReset = factory.getBrowser(); + + assertThat(afterReset, is(not(sameInstance(beforeReset)))); + + verifyNew(FirefoxDriver.class, Mockito.times(2)).withArguments(isA(DesiredCapabilities.class)); + } + + @Test + public void shouldReturnDifferentBrowserWhenCalledFromSeparateThreads() throws Exception { + FirefoxDriver firstBrowser = mock(FirefoxDriver.class); + FirefoxDriver secondBrowser = mock(FirefoxDriver.class); + whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(firstBrowser, secondBrowser); + + Thread firstThread = new Thread(this.new BrowserCreator(BrowserType.FIREFOX)); + Thread secondThread = new Thread(this.new BrowserCreator(BrowserType.FIREFOX)); + + firstThread.start(); + secondThread.start(); + + firstThread.join(); + secondThread.join(); + + assertThat(browsers.size(), is(2)); + assertThat(browsers.get(0), is(not(sameInstance(browsers.get(1))))); + + verifyNew(FirefoxDriver.class, Mockito.times(2)).withArguments(isA(DesiredCapabilities.class)); + } + + @Test + public void shouldBeAbleToSetBrowserTypeFromSeparateThread() throws Exception { + FirefoxDriver firstBrowser = mock(FirefoxDriver.class); + ChromeDriver secondBrowser = mock(ChromeDriver.class); + whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(firstBrowser); + whenNew(ChromeDriver.class).withParameterTypes(ChromeOptions.class).withArguments(isA(ChromeOptions.class)).thenReturn(secondBrowser); + + Thread firstThread = new Thread(this.new BrowserCreator(BrowserType.FIREFOX)); + Thread secondThread = new Thread(this.new BrowserCreator(BrowserType.CHROME)); + + firstThread.start(); + secondThread.start(); + + firstThread.join(); + secondThread.join(); + + assertThat(browsers.size(), is(2)); + assertThat(browsers.get(0), is(not(sameInstance(browsers.get(1))))); + + verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); + verifyNew(ChromeDriver.class, Mockito.times(1)).withArguments(isA(ChromeOptions.class)); + } + + @Test + @PrepareForTest(value = {DesiredCapabilities.class, BrowserFactory.class}) + public void shouldBeAbleToSetProxyFromSeparateThread() throws Exception { + FirefoxDriver firstBrowser = mock(FirefoxDriver.class); + FirefoxDriver secondBrowser = mock(FirefoxDriver.class); + ArgumentCaptor proxyArgumentCaptor = ArgumentCaptor.forClass(Proxy.class); + PowerMockito.mockStatic(DesiredCapabilities.class); + DesiredCapabilities desiredCapabilities = mock(DesiredCapabilities.class); + when(DesiredCapabilities.firefox()).thenReturn(desiredCapabilities); + whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(desiredCapabilities).thenReturn(firstBrowser); + whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(desiredCapabilities).thenReturn(secondBrowser); + + Proxy firstProxy = new Proxy(); + Proxy secondProxy = new Proxy(); + Thread firstThread = new Thread(this.new BrowserCreator(firstProxy)); + Thread secondThread = new Thread(this.new BrowserCreator(secondProxy)); + + firstThread.start(); + secondThread.start(); + + firstThread.join(); + secondThread.join(); + + assertThat(browsers.size(), is(2)); + + verifyNew(FirefoxDriver.class, Mockito.times(2)).withArguments(desiredCapabilities); + verify(desiredCapabilities, Mockito.atLeastOnce()).setCapability(eq(CapabilityType.PROXY), proxyArgumentCaptor.capture()); + + assertThat(proxyArgumentCaptor.getAllValues(), hasItem(firstProxy)); + assertThat(proxyArgumentCaptor.getAllValues(), hasItem(secondProxy)); + } + + @Test + public void shouldReturnFirefoxBrowserWhenNoneSpecified() throws Exception { + FirefoxDriver mockBrowser = mock(FirefoxDriver.class); + whenNew(FirefoxDriver.class).withParameterTypes(Capabilities.class).withArguments(isA(DesiredCapabilities.class)).thenReturn(mockBrowser); + + assertThat(factory.getBrowser(), is(sameInstance((WebDriver) mockBrowser))); + + verifyNew(FirefoxDriver.class, Mockito.times(1)).withArguments(isA(DesiredCapabilities.class)); + } + + @Test + public void shouldReturnChromeBrowserWhenSpecified() throws Exception { + ChromeDriver mockBrowser = mock(ChromeDriver.class); + whenNew(ChromeDriver.class).withParameterTypes(ChromeOptions.class).withArguments(isA(ChromeOptions.class)).thenReturn(mockBrowser); + + factory.setBrowserType(BrowserType.CHROME); + assertThat(factory.getBrowser(), is(sameInstance((WebDriver)mockBrowser))); + + verifyNew(ChromeDriver.class, Mockito.times(1)).withArguments(isA(ChromeOptions.class)); + } + + @Test + public void shouldReturnAndroidBrowserWhenSpecified() throws Exception { + AndroidDriver mockBrowser = mock(AndroidDriver.class); + whenNew(AndroidDriver.class).withNoArguments().thenReturn(mockBrowser); + + factory.setBrowserType(BrowserType.ANDROID); + assertThat(factory.getBrowser(), is(sameInstance((WebDriver)mockBrowser))); + + verifyNew(AndroidDriver.class, Mockito.times(1)).withNoArguments(); + } +}