Skip to content

Commit

Permalink
Merge branch 'develop' into #121-print-stack-trace
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/test/java/com/xceptance/neodymium/tests/NeodymiumWebDriverTest.java
  • Loading branch information
Marcel Pfotenhauer committed Aug 27, 2020
2 parents 747a374 + effa81e commit 45c0dc6
Show file tree
Hide file tree
Showing 29 changed files with 971 additions and 155 deletions.
9 changes: 8 additions & 1 deletion config/browser.properties
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,19 @@ browserprofile.Chrome_1500x1000.browser = chrome
browserprofile.Chrome_1500x1000.browserResolution = 1500x1000

# Local headless Chrome
browserprofile.Chrome_headless.name = Headless Google Chrome
browserprofile.Chrome_headless.name = Headless Google Chrome 1024x768
browserprofile.Chrome_headless.browser = chrome
browserprofile.Chrome_headless.browserResolution = 1024x768
browserprofile.Chrome_headless.arguments = -ignore-certificate-errors
browserprofile.Chrome_headless.headless = true

# Local headless Chrome
browserprofile.Chrome_1500x1000_headless.name = Headless Google Chrome 1500x1000
browserprofile.Chrome_1500x1000_headless.browser = chrome
browserprofile.Chrome_1500x1000_headless.browserResolution = 1500x1000
browserprofile.Chrome_1500x1000_headless.arguments = -ignore-certificate-errors
browserprofile.Chrome_1500x1000_headless.headless = true

# Small Firefox
browserprofile.FF_1024x768.name = Firefox 1024x768
browserprofile.FF_1024x768.browser = firefox
Expand Down
4 changes: 4 additions & 0 deletions config/neodymium.properties
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ neodymium.webDriver.firefox.legacyMode = false
## Whether to use a single driver instance for all tests (defaults to false).
neodymium.webDriver.reuseDriver = false

## The number of reuses. If not specified or set below 1 the driver will be reused unlimited times.
## Setting the property to 1 means that the driver is reused once, so the web driver is used twice in total.
#neodymium.webDriver.maxReuse = 0

## Whether to keep browser open after test has finished
# Warning: The webdriver process might stay alive even if you close the browser afterwards
neodymium.webDriver.keepBrowserOpen = false
Expand Down
19 changes: 4 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<maven.compiler.source.version>1.8</maven.compiler.source.version>
<maven.compiler.target.version>1.8</maven.compiler.target.version>
<surefire.version>2.22.2</surefire.version>
<allure.version>2.13.3</allure.version>
<allure.version>2.13.5</allure.version>
<cucumber.version>5.7.0</cucumber.version>
<log4j.version>2.13.3</log4j.version>
</properties>
Expand Down Expand Up @@ -244,11 +244,6 @@
<artifactId>allure-selenide</artifactId>
<version>${allure.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
Expand All @@ -257,7 +252,7 @@
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>5.12.2</version>
<version>5.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand All @@ -267,12 +262,12 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.8</version>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down Expand Up @@ -305,11 +300,5 @@
<version>2.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>xyz.rogfam</groupId>
<artifactId>littleproxy</artifactId>
<version>2.0.0-beta-5</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import com.xceptance.neodymium.module.StatementBuilder;
import com.xceptance.neodymium.module.statement.browser.multibrowser.Browser;
import com.xceptance.neodymium.module.statement.browser.multibrowser.BrowserRunnerHelper;
import com.xceptance.neodymium.module.statement.browser.multibrowser.CachingContainer;
import com.xceptance.neodymium.module.statement.browser.multibrowser.SuppressBrowsers;
import com.xceptance.neodymium.module.statement.browser.multibrowser.WebDriverCache;
import com.xceptance.neodymium.module.statement.browser.multibrowser.WebDriverStateContainer;
import com.xceptance.neodymium.module.statement.browser.multibrowser.configuration.BrowserConfiguration;
import com.xceptance.neodymium.module.statement.browser.multibrowser.configuration.MultibrowserConfiguration;
import com.xceptance.neodymium.util.Neodymium;
Expand All @@ -49,9 +49,7 @@ public class BrowserStatement extends StatementBuilder

private MultibrowserConfiguration multibrowserConfiguration = MultibrowserConfiguration.getInstance();

private WebDriver webdriver;

private BrowserUpProxy proxy;
private WebDriverStateContainer wDSCont;

public BrowserStatement()
{
Expand Down Expand Up @@ -128,7 +126,7 @@ public void evaluate() throws Throwable
*/
public void setUpTest(String browserTag)
{
webdriver = null;
wDSCont = null;
this.browserTag = browserTag;
LOGGER.debug("Create browser for name: " + browserTag);
BrowserConfiguration browserConfiguration = multibrowserConfiguration.getBrowserProfiles().get(browserTag);
Expand All @@ -138,22 +136,20 @@ public void setUpTest(String browserTag)
// try to find appropriate web driver in cache before create a new instance
if (Neodymium.configuration().reuseWebDriver())
{
CachingContainer container = WebDriverCache.instance.getRemoveWebDriverAndProxy(browserConfiguration.getConfigTag());
webdriver = container != null ? container.getWebDriver() : null;
proxy = container != null ? container.getProxy() : null;
if (webdriver != null)
wDSCont = WebDriverCache.instance.removeWebDriverStateContainerByBrowserTag(browserConfiguration.getConfigTag());
if (wDSCont != null && wDSCont.getWebDriver() != null)
{
webdriver.manage().deleteAllCookies();
wDSCont.getWebDriver().manage().deleteAllCookies();
}
}

if (webdriver == null)
if (wDSCont == null)
{
LOGGER.debug("Create new browser instance");
CachingContainer container = BrowserRunnerHelper.createWebdriver(browserConfiguration);
webdriver = new EventFiringWebDriver(container.getWebDriver());
proxy = container.getProxy();
((EventFiringWebDriver) webdriver).register(new NeodymiumWebDriverListener());
wDSCont = BrowserRunnerHelper.createWebDriverStateContainer(browserConfiguration);
EventFiringWebDriver eFWDriver = new EventFiringWebDriver(wDSCont.getWebDriver());
eFWDriver.register(new NeodymiumWebDriverListener());
wDSCont.setWebDriver(eFWDriver);
}
else
{
Expand All @@ -164,13 +160,12 @@ public void setUpTest(String browserTag)
{
throw new RuntimeException("An error occurred during URL creation. See nested exception.", e);
}
if (webdriver != null)
if (wDSCont != null)
{
// set browser window size
BrowserRunnerHelper.setBrowserWindowSize(browserConfiguration, webdriver);
WebDriverRunner.setWebDriver(webdriver);
Neodymium.setDriver(webdriver);
Neodymium.setLocalProxy(proxy);
BrowserRunnerHelper.setBrowserWindowSize(browserConfiguration, wDSCont.getWebDriver());
WebDriverRunner.setWebDriver(wDSCont.getWebDriver());
Neodymium.setWebDriverStateContainer(wDSCont);
Neodymium.setBrowserProfileName(browserConfiguration.getConfigTag());
Neodymium.setBrowserName(browserConfiguration.getCapabilities().getBrowserName());

Expand All @@ -194,34 +189,37 @@ private void initSelenideConfiguration()

public void teardown(boolean testFailed)
{
teardown(testFailed, false, webdriver, proxy);
teardown(testFailed, false, wDSCont);
}

public void teardown(boolean testFailed, boolean preventReuse, WebDriver webDriver, BrowserUpProxy proxy)
public void teardown(boolean testFailed, boolean preventReuse, WebDriverStateContainer webDriverStateContainer)
{
BrowserConfiguration browserConfiguration = multibrowserConfiguration.getBrowserProfiles().get(Neodymium.getBrowserProfileName());

// keep browser open
if ((browserConfiguration != null && !browserConfiguration.isHeadless())
&& ((Neodymium.configuration().keepBrowserOpenOnFailure() && testFailed) || Neodymium.configuration().keepBrowserOpen()))
if (keepOpen(testFailed, browserConfiguration))
{
LOGGER.debug("Keep browser open");
// nothing to do
}
// reuse
else if (Neodymium.configuration().reuseWebDriver() && !preventReuse && isWebDriverStillOpen(webDriver))
else if (canReuse(preventReuse, webDriverStateContainer))
{
LOGGER.debug("Put browser into cache");
WebDriverCache.instance.putWebDriverAndProxy(browserTag, webDriver, proxy);
webDriverStateContainer.incrementUsedCount();
WebDriverCache.instance.putWebDriverStateContainer(browserTag, webDriverStateContainer);
}
// close the WebDriver
else
{
LOGGER.debug("Teardown browser");
WebDriver webDriver = webDriverStateContainer.getWebDriver();
if (webDriver != null)
{
webDriver.quit();
}

BrowserUpProxy proxy = webDriverStateContainer != null ? webDriverStateContainer.getProxy() : null;
if (proxy != null)
{
try
Expand All @@ -235,12 +233,24 @@ else if (Neodymium.configuration().reuseWebDriver() && !preventReuse && isWebDri
}
}

Neodymium.setDriver(null);
Neodymium.setLocalProxy(null);
Neodymium.setWebDriverStateContainer(null);
Neodymium.setBrowserProfileName(null);
Neodymium.setBrowserName(null);
}

private boolean keepOpen(boolean testFailed, BrowserConfiguration browserConfiguration)
{
return (browserConfiguration != null && !browserConfiguration.isHeadless())
&& ((Neodymium.configuration().keepBrowserOpenOnFailure() && testFailed) || Neodymium.configuration().keepBrowserOpen());
}

private boolean canReuse(boolean preventReuse, WebDriverStateContainer webDriverStateContainer)
{
boolean maxReuseReached = (Neodymium.configuration().maxWebDriverReuse() > 0)
&& (webDriverStateContainer.getUsedCount() >= Neodymium.configuration().maxWebDriverReuse());
return Neodymium.configuration().reuseWebDriver() && !preventReuse && !maxReuseReached && isWebDriverStillOpen(webDriverStateContainer.getWebDriver());
}

@Override
public List<Object> createIterationData(TestClass testClass, FrameworkMethod method)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.xceptance.neodymium.module.statement.browser.multibrowser.configuration.MultibrowserConfiguration;
import com.xceptance.neodymium.module.statement.browser.multibrowser.configuration.TestEnvironment;
import com.xceptance.neodymium.util.Neodymium;
import com.xceptance.neodymium.util.NeodymiumConfiguration;

public final class BrowserRunnerHelper
{
Expand Down Expand Up @@ -180,22 +181,23 @@ private static FirefoxBinary createFirefoxBinary(final String pathToBrowser)
*
* @param config
* {@link BrowserConfiguration} that describes the desired browser instance
* @return {@link WebDriver} the instance of the browser described in {@link BrowserConfiguration}
* @return {@link WebDriverStateContainer} the instance of the browser described in {@link BrowserConfiguration} and
* in {@link NeodymiumConfiguration}
* @throws MalformedURLException
* if <a href="https://github.com/Xceptance/neodymium-library/wiki/Selenium-grid">Selenium grid</a> is
* used and the given grid URL is invalid
*/
public static CachingContainer createWebdriver(final BrowserConfiguration config) throws MalformedURLException
public static WebDriverStateContainer createWebDriverStateContainer(final BrowserConfiguration config) throws MalformedURLException
{
final MutableCapabilities capabilities = config.getCapabilities();
final CachingContainer container = new CachingContainer();
final WebDriverStateContainer wDSC = new WebDriverStateContainer();

if (Neodymium.configuration().useLocalProxy())
{
BrowserUpProxy proxy = setupEmbeddedProxy();

// set the Proxy for later usage
container.setProxy(proxy);
wDSC.setProxy(proxy);

// configure the proxy via capabilities
capabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxy));
Expand Down Expand Up @@ -225,7 +227,7 @@ else if (Neodymium.configuration().useProxy())
options.addArguments(config.getArguments());
}

container.setWebDriver(new ChromeDriver(options));
wDSC.setWebDriver(new ChromeDriver(options));
}
else if (firefoxBrowsers.contains(browserName))
{
Expand All @@ -238,7 +240,7 @@ else if (firefoxBrowsers.contains(browserName))
options.addArguments(config.getArguments());
}

container.setWebDriver(new FirefoxDriver(options));
wDSC.setWebDriver(new FirefoxDriver(options));
}
else if (internetExplorerBrowsers.contains(browserName))
{
Expand All @@ -252,25 +254,25 @@ else if (internetExplorerBrowsers.contains(browserName))
}
}

container.setWebDriver(new InternetExplorerDriver(options));
wDSC.setWebDriver(new InternetExplorerDriver(options));
}
else if (safariBrowsers.contains(browserName))
{
final SafariOptions options = (SafariOptions) capabilities;
container.setWebDriver(new SafariDriver(options));
wDSC.setWebDriver(new SafariDriver(options));
}
else
{
container.setWebDriver(new RemoteWebDriver(capabilities));
wDSC.setWebDriver(new RemoteWebDriver(capabilities));
}
}
else
{
// establish connection to target website
container.setWebDriver(new RemoteWebDriver(createGridExecutor(testEnvironment), capabilities));
wDSC.setWebDriver(new RemoteWebDriver(createGridExecutor(testEnvironment), capabilities));
}

return container;
return wDSC;
}

private static BrowserUpProxy setupEmbeddedProxy()
Expand Down
Loading

0 comments on commit 45c0dc6

Please sign in to comment.