Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit f16b47e

Browse files
slu-itAxel Schüssler
authored andcommitted
Created Browser Factories in Core module
1 parent afacb2f commit f16b47e

File tree

8 files changed

+334
-32
lines changed

8 files changed

+334
-32
lines changed

webtester-core/pom.xml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,40 @@
4343
<optional>true</optional>
4444
</dependency>
4545

46+
<!--
47+
Selenium dependencies are scoped as either provided or optional.
48+
- Provided dependencies must be declared by the project using WebTester
49+
- Optional dependencies can be declared in case they are needed
50+
-->
4651
<dependency>
47-
<!-- Provided Selenium dependency used to have access to the Selenium API. -->
4852
<groupId>org.seleniumhq.selenium</groupId>
4953
<artifactId>selenium-support</artifactId>
5054
<scope>provided</scope>
5155
</dependency>
56+
<dependency>
57+
<groupId>org.seleniumhq.selenium</groupId>
58+
<artifactId>selenium-remote-driver</artifactId>
59+
<scope>provided</scope>
60+
</dependency>
5261

53-
<!-- test -->
54-
5562
<dependency>
56-
<!-- Firefox driver used in tests. Is scoped as a test dependency in order
57-
to not be a transitive dependency for the actual project using the WebTester
58-
framework. -->
63+
<groupId>org.seleniumhq.selenium</groupId>
64+
<artifactId>selenium-chrome-driver</artifactId>
65+
<scope>provided</scope>
66+
</dependency>
67+
<dependency>
5968
<groupId>org.seleniumhq.selenium</groupId>
6069
<artifactId>selenium-firefox-driver</artifactId>
61-
<scope>test</scope>
70+
<scope>provided</scope>
6271
</dependency>
72+
<dependency>
73+
<groupId>org.seleniumhq.selenium</groupId>
74+
<artifactId>selenium-ie-driver</artifactId>
75+
<scope>provided</scope>
76+
</dependency>
77+
78+
<!-- test -->
79+
6380
<dependency>
6481
<groupId>org.eclipse.jetty</groupId>
6582
<artifactId>jetty-server</artifactId>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package info.novatec.testit.webtester.browser.factories;
2+
3+
import java.util.function.Function;
4+
5+
import org.openqa.selenium.Proxy;
6+
import org.openqa.selenium.WebDriver;
7+
import org.openqa.selenium.remote.CapabilityType;
8+
import org.openqa.selenium.remote.DesiredCapabilities;
9+
10+
import info.novatec.testit.webtester.browser.Browser;
11+
import info.novatec.testit.webtester.browser.BrowserFactory;
12+
import info.novatec.testit.webtester.browser.WebDriverBrowser;
13+
import info.novatec.testit.webtester.browser.proxy.ProxyConfiguration;
14+
15+
16+
/**
17+
* Base class for all the default {@link BrowserFactory} implementations shipped with WebTester.
18+
* <p>
19+
* This class provides default behaviour for any {@link Browser} initialized with it.
20+
* <p>
21+
* <b>The following capabilities are set by default:</b>
22+
* <ul>
23+
* <li>Native Events are disabled</li>
24+
* <li>Unsigned certificates are accepted</li>
25+
* </ul>
26+
*
27+
* @param <T> the type of the extending factory implementation - used for fluent API for certain methods
28+
* @since 2.1
29+
*/
30+
public class BaseBrowserFactory<T extends BrowserFactory> implements BrowserFactory {
31+
32+
private final Function<DesiredCapabilities, WebDriver> webDriverProducer;
33+
private ProxyConfiguration proxyConfiguration;
34+
35+
protected BaseBrowserFactory(Function<DesiredCapabilities, WebDriver> webDriverProducer) {
36+
this.webDriverProducer = webDriverProducer;
37+
}
38+
39+
@Override
40+
public Browser createBrowser() {
41+
DesiredCapabilities capabilities = getDefaultCapabilities();
42+
return createBrowser(capabilities);
43+
}
44+
45+
protected DesiredCapabilities getDefaultCapabilities() {
46+
DesiredCapabilities capabilities = new DesiredCapabilities();
47+
capabilities.setCapability(CapabilityType.HAS_NATIVE_EVENTS, false);
48+
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
49+
setOptionalProxyConfiguration(capabilities);
50+
return capabilities;
51+
}
52+
53+
@Override
54+
public Browser createBrowser(DesiredCapabilities capabilities) {
55+
return createBrowser(webDriverProducer.apply(capabilities));
56+
}
57+
58+
@Override
59+
public Browser createBrowser(WebDriver webDriver) {
60+
return WebDriverBrowser.buildForWebDriver(webDriver);
61+
}
62+
63+
protected void setOptionalProxyConfiguration(DesiredCapabilities capabilities) {
64+
if (proxyConfiguration != null) {
65+
Proxy proxy = new Proxy();
66+
proxyConfiguration.configureProxy(proxy);
67+
capabilities.setCapability(CapabilityType.PROXY, proxy);
68+
}
69+
}
70+
71+
@Override
72+
@SuppressWarnings("unchecked")
73+
public T withProxyConfiguration(ProxyConfiguration configuration) {
74+
proxyConfiguration = configuration;
75+
return (T) this;
76+
}
77+
78+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package info.novatec.testit.webtester.browser.factories;
2+
3+
import org.openqa.selenium.chrome.ChromeDriver;
4+
5+
import info.novatec.testit.webtester.browser.Browser;
6+
7+
8+
/**
9+
* Factory class for creating Chrome {@link Browser} instances.
10+
* Needs the {@code webdriver.chrome.driver} system property pointing to the driver proxy server executable.
11+
* <p>
12+
* <b>The following capabilities are set by default:</b>
13+
* <ul>
14+
* <li>Native Events are disabled</li>
15+
* <li>Unsigned certificates are accepted</li>
16+
* </ul>
17+
* <b>Additional information on using the {@link ChromeDriver}:</b>
18+
* <p>
19+
* https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver
20+
*
21+
* @see Browser
22+
* @see ChromeDriver
23+
* @since 2.1
24+
*/
25+
public class ChromeFactory extends BaseBrowserFactory<ChromeFactory> {
26+
27+
public ChromeFactory() {
28+
super(ChromeDriver::new);
29+
}
30+
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package info.novatec.testit.webtester.browser.factories;
2+
3+
import org.openqa.selenium.firefox.FirefoxDriver;
4+
5+
import info.novatec.testit.webtester.browser.Browser;
6+
7+
8+
/**
9+
* Factory class for creating Firefox {@link Browser} instances.
10+
* This will only work for Firefox browsers up to version 46.
11+
* All newer versions need to be initialized using the {@link MarionetteFactory}.
12+
* <p>
13+
* <b>The following capabilities are set by default:</b>
14+
* <ul>
15+
* <li>Native Events are disabled</li>
16+
* <li>Unsigned certificates are accepted</li>
17+
* </ul>
18+
* <b>Additional information on using the {@link FirefoxDriver}:</b>
19+
* <p>
20+
* https://github.com/SeleniumHQ/selenium/wiki/FirefoxDriver
21+
*
22+
* @see Browser
23+
* @see FirefoxDriver
24+
* @see MarionetteFactory
25+
* @since 2.1
26+
*/
27+
public class FirefoxFactory extends BaseBrowserFactory<FirefoxFactory> {
28+
29+
public FirefoxFactory() {
30+
super((capabilities) -> {
31+
capabilities.setCapability("marionette", false);
32+
return new FirefoxDriver(capabilities);
33+
});
34+
}
35+
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package info.novatec.testit.webtester.browser.factories;
2+
3+
import org.openqa.selenium.ie.InternetExplorerDriver;
4+
import org.openqa.selenium.ie.InternetExplorerDriverService;
5+
import org.openqa.selenium.remote.DesiredCapabilities;
6+
7+
import info.novatec.testit.webtester.browser.Browser;
8+
9+
10+
/**
11+
* Factory class for creating Internet Explorer {@link Browser} instances.
12+
* Needs the {@code webdriver.ie.driver} system property pointing to the driver proxy server executable.
13+
* <p>
14+
* <b>The following capabilities are set by default:</b>
15+
* <ul>
16+
* <li>Native Events are disabled</li>
17+
* <li>Unsigned certificates are accepted</li>
18+
* </ul>
19+
* <b>Additional information on using the {@link InternetExplorerDriver}:</b>
20+
* <p>
21+
* https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver
22+
*
23+
* @see Browser
24+
* @see InternetExplorerDriver
25+
* @since 2.1
26+
*/
27+
public class InternetExplorerFactory extends BaseBrowserFactory<InternetExplorerFactory> {
28+
29+
public InternetExplorerFactory() {
30+
super(InternetExplorerDriver::new);
31+
}
32+
33+
public Browser createBrowser(int port) {
34+
InternetExplorerDriverService service = InternetExplorerDriverService.createDefaultService();
35+
DesiredCapabilities capabilities = getDefaultCapabilities();
36+
return createBrowser(new InternetExplorerDriver(service, capabilities, port));
37+
}
38+
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package info.novatec.testit.webtester.browser.factories;
2+
3+
import org.openqa.selenium.firefox.FirefoxDriver;
4+
5+
import info.novatec.testit.webtester.browser.Browser;
6+
7+
8+
/**
9+
* Factory class for creating Firefox {@link Browser} instances.
10+
* This will only work for Firefox browsers version 47 and above!
11+
* All older versions should be initialized using the {@link FirefoxFactory}.
12+
* <p>
13+
* <b>The following capabilities are set by default:</b>
14+
* <ul>
15+
* <li>Native Events are disabled</li>
16+
* <li>Unsigned certificates are accepted</li>
17+
* </ul>
18+
* <b>Additional information on using the {@link FirefoxDriver}:</b>
19+
* <p>
20+
* https://github.com/SeleniumHQ/selenium/wiki/FirefoxDriver
21+
*
22+
* @see Browser
23+
* @see FirefoxDriver
24+
* @see FirefoxFactory
25+
* @since 2.1
26+
*/
27+
public class MarionetteFactory extends BaseBrowserFactory<MarionetteFactory> {
28+
29+
public MarionetteFactory() {
30+
super((capabilities) -> {
31+
capabilities.setCapability("marionette", true);
32+
return new FirefoxDriver(capabilities);
33+
});
34+
}
35+
36+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package info.novatec.testit.webtester.browser.factories;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.Mockito.doReturn;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.verifyNoMoreInteractions;
9+
import static org.mockito.Mockito.verifyZeroInteractions;
10+
11+
import java.util.function.Function;
12+
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.mockito.ArgumentCaptor;
16+
import org.mockito.Captor;
17+
import org.mockito.InjectMocks;
18+
import org.mockito.Mock;
19+
import org.mockito.runners.MockitoJUnitRunner;
20+
import org.openqa.selenium.Proxy;
21+
import org.openqa.selenium.WebDriver;
22+
import org.openqa.selenium.remote.CapabilityType;
23+
import org.openqa.selenium.remote.DesiredCapabilities;
24+
25+
import info.novatec.testit.webtester.browser.Browser;
26+
import info.novatec.testit.webtester.browser.proxy.ProxyConfiguration;
27+
28+
29+
@RunWith(MockitoJUnitRunner.class)
30+
public class BaseBrowserFactoryTest {
31+
32+
@Mock
33+
Function<DesiredCapabilities, WebDriver> webDriverProducer;
34+
@InjectMocks
35+
BaseBrowserFactory cut;
36+
37+
@Captor
38+
ArgumentCaptor<DesiredCapabilities> capabilitiesCaptor;
39+
40+
@Test
41+
public void webDriverIsInitializedUsingProducerWhenCreatingBrowser() {
42+
WebDriver webDriver = mock(WebDriver.class);
43+
doReturn(webDriver).when(webDriverProducer).apply(any(DesiredCapabilities.class));
44+
Browser browser = cut.createBrowser();
45+
assertThat(browser.webDriver()).isSameAs(webDriver);
46+
}
47+
48+
@Test
49+
public void defaultCapabilitiesAreSetWhenCreatingBrowser() {
50+
51+
cut.createBrowser();
52+
53+
verify(webDriverProducer).apply(capabilitiesCaptor.capture());
54+
verifyNoMoreInteractions(webDriverProducer);
55+
56+
DesiredCapabilities capabilities = capabilitiesCaptor.getValue();
57+
assertThat(capabilities.getCapability(CapabilityType.HAS_NATIVE_EVENTS)).isEqualTo(false);
58+
assertThat(capabilities.getCapability(CapabilityType.ACCEPT_SSL_CERTS)).isEqualTo(true);
59+
60+
}
61+
62+
@Test
63+
public void proxyCapabilityIsSetIfSpecifiedWhenCreatingBrowser() {
64+
65+
ProxyConfiguration proxyConfiguration = proxy -> {
66+
proxy.setHttpProxy("foo-bar-proxy");
67+
};
68+
69+
cut.withProxyConfiguration(proxyConfiguration).createBrowser();
70+
71+
verify(webDriverProducer).apply(capabilitiesCaptor.capture());
72+
verifyNoMoreInteractions(webDriverProducer);
73+
74+
DesiredCapabilities capabilities = capabilitiesCaptor.getValue();
75+
Proxy proxy = ( Proxy ) capabilities.getCapability(CapabilityType.PROXY);
76+
assertThat(proxy.getHttpProxy()).isEqualTo("foo-bar-proxy");
77+
78+
}
79+
80+
@Test
81+
public void capabilitiesAreNotChangedWhenCreatingBrowserForThem() {
82+
DesiredCapabilities capabilities = mock(DesiredCapabilities.class);
83+
cut.createBrowser(capabilities);
84+
verify(webDriverProducer).apply(capabilities);
85+
verifyZeroInteractions(capabilities);
86+
}
87+
88+
}

0 commit comments

Comments
 (0)