Skip to content

Commit 13ed405

Browse files
committed
Reworked LocalBrowserFactory, add support of Opera and Yandex browsers
1 parent ff13540 commit 13ed405

File tree

9 files changed

+200
-16
lines changed

9 files changed

+200
-16
lines changed

src/main/java/aquality/selenium/browser/BrowserName.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ public enum BrowserName {
55
EDGE,
66
FIREFOX,
77
IEXPLORER,
8-
SAFARI
8+
OPERA,
9+
OTHER,
10+
SAFARI,
11+
YANDEX
912
}

src/main/java/aquality/selenium/browser/LocalBrowserFactory.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
import org.openqa.selenium.chrome.ChromeDriver;
1111
import org.openqa.selenium.chrome.ChromeOptions;
1212
import org.openqa.selenium.edge.EdgeDriver;
13+
import org.openqa.selenium.edge.EdgeOptions;
1314
import org.openqa.selenium.firefox.FirefoxDriver;
15+
import org.openqa.selenium.firefox.FirefoxOptions;
1416
import org.openqa.selenium.ie.InternetExplorerDriver;
17+
import org.openqa.selenium.ie.InternetExplorerOptions;
1518
import org.openqa.selenium.remote.AbstractDriverOptions;
1619
import org.openqa.selenium.remote.RemoteWebDriver;
1720
import org.openqa.selenium.safari.SafariDriver;
21+
import org.openqa.selenium.safari.SafariOptions;
1822

1923
public class LocalBrowserFactory extends BrowserFactory {
2024

@@ -34,35 +38,32 @@ protected RemoteWebDriver getDriver() {
3438
Architecture systemArchitecture = driverSettings.getSystemArchitecture();
3539
switch (browserName) {
3640
case CHROME:
41+
case YANDEX:
3742
WebDriverManager.chromedriver().driverVersion(webDriverVersion).setup();
3843
driver = new ChromeDriver((ChromeOptions) driverSettings.getDriverOptions());
3944
break;
45+
case OPERA:
46+
WebDriverManager.operadriver().driverVersion(webDriverVersion).setup();
47+
driver = new ChromeDriver((ChromeOptions) driverSettings.getDriverOptions());
48+
break;
4049
case FIREFOX:
4150
WebDriverManager.firefoxdriver().driverVersion(webDriverVersion).setup();
42-
driver = getDriver(FirefoxDriver.class, driverSettings.getDriverOptions());
51+
driver = new FirefoxDriver((FirefoxOptions) driverSettings.getDriverOptions());
4352
break;
4453
case IEXPLORER:
4554
WebDriverManager.iedriver().architecture(systemArchitecture).driverVersion(webDriverVersion).setup();
46-
driver = getDriver(InternetExplorerDriver.class, driverSettings.getDriverOptions());
55+
driver = new InternetExplorerDriver((InternetExplorerOptions) driverSettings.getDriverOptions());
4756
break;
4857
case EDGE:
4958
WebDriverManager.edgedriver().driverVersion(webDriverVersion).setup();
50-
driver = getDriver(EdgeDriver.class, driverSettings.getDriverOptions());
59+
driver = new EdgeDriver((EdgeOptions) driverSettings.getDriverOptions());
5160
break;
5261
case SAFARI:
53-
driver = getDriver(SafariDriver.class, driverSettings.getDriverOptions());
62+
driver = new SafariDriver((SafariOptions) driverSettings.getDriverOptions());
5463
break;
5564
default:
5665
throw new IllegalArgumentException(String.format("Browser [%s] is not supported.", browserName));
5766
}
5867
return driver;
5968
}
60-
61-
private <T extends RemoteWebDriver> T getDriver(Class<T> driverClass, AbstractDriverOptions<?> options) {
62-
try {
63-
return driverClass.getDeclaredConstructor(AbstractDriverOptions.class).newInstance(options);
64-
} catch (ReflectiveOperationException e) {
65-
throw new UnsupportedOperationException(String.format("Cannot instantiate driver with type '%1$s'.", driverClass), e);
66-
}
67-
}
6869
}

src/main/java/aquality/selenium/configuration/BrowserProfile.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ public IDriverSettings getDriverSettings() {
4949
case IEXPLORER:
5050
driverSettings = new IExplorerSettings(settingsFile);
5151
break;
52+
case OPERA:
53+
driverSettings = new OperaSettings(settingsFile);
54+
break;
5255
case SAFARI:
5356
driverSettings = new SafariSettings(settingsFile);
5457
break;
58+
case YANDEX:
59+
driverSettings = new YandexSettings(settingsFile);
60+
break;
5561
default:
5662
throw new IllegalArgumentException("There are no assigned behaviour for retrieving driver driversettings for browser " + getBrowserName());
5763
}

src/main/java/aquality/selenium/configuration/driversettings/DriverSettings.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ protected List<String> getBrowserStartArguments() {
6969
return startArguments;
7070
}
7171

72+
protected String getBinaryLocation(String defaultBinaryLocation) {
73+
String value = (String) getSettingsFile().getValueOrDefault(getDriverSettingsPath("binaryLocation"), defaultBinaryLocation);
74+
int varStartIndex = value.indexOf('%');
75+
int varEndIndex = value.lastIndexOf('%');
76+
return varStartIndex == 0 && varStartIndex != varEndIndex
77+
? System.getenv(value.substring(varStartIndex + 1, varEndIndex)) + value.substring(varEndIndex + 1)
78+
: getAbsolutePath(value);
79+
}
80+
7281
@SafeVarargs
7382
private final <T> void logCollection(String messageKey, final T... elements) {
7483
if (elements.length == 1 &&
@@ -140,7 +149,7 @@ public String getKey() {
140149
}
141150
}
142151

143-
private String getAbsolutePath(String path) {
152+
protected String getAbsolutePath(String path) {
144153
try {
145154
return new File(path).getCanonicalPath();
146155
} catch (IOException e) {

src/main/java/aquality/selenium/configuration/driversettings/EdgeSettings.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import aquality.selenium.browser.BrowserName;
44
import aquality.selenium.core.utilities.ISettingsFile;
5+
import org.openqa.selenium.chrome.ChromeOptions;
56
import org.openqa.selenium.edge.EdgeOptions;
67
import org.openqa.selenium.remote.AbstractDriverOptions;
78

9+
import java.util.HashMap;
10+
import java.util.Map;
11+
812
public class EdgeSettings extends DriverSettings {
913

1014
public EdgeSettings(ISettingsFile settingsFile){
@@ -15,13 +19,28 @@ public EdgeSettings(ISettingsFile settingsFile){
1519
public AbstractDriverOptions<?> getDriverOptions() {
1620
EdgeOptions edgeOptions = new EdgeOptions();
1721
setCapabilities(edgeOptions);
22+
setPrefs(edgeOptions);
23+
getBrowserStartArguments().forEach(edgeOptions::addArguments);
1824
edgeOptions.setPageLoadStrategy(getPageLoadStrategy());
1925
return edgeOptions;
2026
}
2127

2228
@Override
2329
public String getDownloadDirCapabilityKey() {
24-
throw new IllegalArgumentException("Download directory for EDGE profiles is not supported");
30+
return "download.default_directory";
31+
}
32+
33+
private void setPrefs(EdgeOptions options){
34+
HashMap<String, Object> prefs = new HashMap<>();
35+
Map<String, Object> configOptions = getBrowserOptions();
36+
configOptions.forEach((key, value) -> {
37+
if (key.equals(getDownloadDirCapabilityKey())) {
38+
prefs.put(key, getDownloadDir());
39+
} else {
40+
prefs.put(key, value);
41+
}
42+
});
43+
options.setExperimentalOption("prefs", prefs);
2544
}
2645

2746
@Override
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package aquality.selenium.configuration.driversettings;
2+
3+
import aquality.selenium.browser.BrowserName;
4+
import aquality.selenium.core.utilities.ISettingsFile;
5+
import org.openqa.selenium.chrome.ChromeOptions;
6+
import org.openqa.selenium.remote.AbstractDriverOptions;
7+
8+
public class OperaSettings extends ChromeSettings {
9+
private static final String DEFAULT_BINARY_LOCATION = "%USERPROFILE%\\AppData\\Local\\Programs\\Opera\\launcher.exe";
10+
11+
public OperaSettings(ISettingsFile settingsFile) {
12+
super(settingsFile);
13+
}
14+
15+
@Override
16+
public BrowserName getBrowserName() {
17+
return BrowserName.OPERA;
18+
}
19+
20+
@Override
21+
public AbstractDriverOptions<?> getDriverOptions() {
22+
ChromeOptions options = (ChromeOptions) super.getDriverOptions();
23+
options.setExperimentalOption("w3c", true);
24+
options.setBinary(getBinaryLocation(DEFAULT_BINARY_LOCATION));
25+
return options;
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package aquality.selenium.configuration.driversettings;
2+
3+
import aquality.selenium.browser.BrowserName;
4+
import aquality.selenium.core.utilities.ISettingsFile;
5+
import org.openqa.selenium.chrome.ChromeOptions;
6+
import org.openqa.selenium.remote.AbstractDriverOptions;
7+
8+
public class YandexSettings extends ChromeSettings {
9+
private static final String DEFAULT_BINARY_LOCATION = "%USERPROFILE%\\AppData\\Local\\Yandex\\YandexBrowser\\Application\\browser.exe";
10+
public YandexSettings(ISettingsFile settingsFile) {
11+
super(settingsFile);
12+
}
13+
14+
@Override
15+
public AbstractDriverOptions<?> getDriverOptions() {
16+
ChromeOptions options = (ChromeOptions) super.getDriverOptions();
17+
options.setBinary(getBinaryLocation(DEFAULT_BINARY_LOCATION));
18+
return options;
19+
}
20+
21+
@Override
22+
public BrowserName getBrowserName() {
23+
return BrowserName.YANDEX;
24+
}
25+
}

src/main/resources/settings.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
"pageLoadStrategy": "Normal",
2222
"startArguments": []
2323
},
24+
"edge": {
25+
"webDriverVersion": "latest",
26+
"capabilities": {
27+
"enableVNC": true
28+
},
29+
"options": {
30+
"intl.accept_languages": "en",
31+
"safebrowsing.enabled": "true",
32+
"profile.default_content_settings.popups": "0",
33+
"disable-popup-blocking": "true",
34+
"download.prompt_for_download": "false",
35+
"download.default_directory": "//home//selenium//downloads"
36+
},
37+
"startArguments": []
38+
},
2439
"firefox": {
2540
"webDriverVersion": "latest",
2641
"capabilities": {
@@ -50,8 +65,40 @@
5065
"ignoreProtectedModeSettings": true
5166
}
5267
},
68+
"opera": {
69+
"webDriverVersion": "latest",
70+
"binaryLocation": "%USERPROFILE%\\AppData\\Local\\Programs\\Opera\\launcher.exe",
71+
"capabilities": {
72+
"enableVNC": true
73+
},
74+
"options": {
75+
"intl.accept_languages": "en",
76+
"safebrowsing.enabled": "true",
77+
"profile.default_content_settings.popups": "0",
78+
"disable-popup-blocking": "true",
79+
"download.prompt_for_download": "false",
80+
"download.default_directory": "./downloads"
81+
},
82+
"startArguments": ["--remote-debugging-port=9222", "--no-sandbox", "--disable-dev-shm-usage"]
83+
},
5384
"safari": {
5485
"downloadDir": "/Users/username/Downloads"
86+
},
87+
"yandex": {
88+
"webDriverVersion": "102.0.5005.61",
89+
"binaryLocation": "%USERPROFILE%\\AppData\\Local\\Yandex\\YandexBrowser\\Application\\browser.exe",
90+
"capabilities": {
91+
"enableVNC": true
92+
},
93+
"options": {
94+
"intl.accept_languages": "en",
95+
"safebrowsing.enabled": "true",
96+
"profile.default_content_settings.popups": "0",
97+
"disable-popup-blocking": "true",
98+
"download.prompt_for_download": "false",
99+
"download.default_directory": "./downloads"
100+
},
101+
"startArguments": []
55102
}
56103
},
57104
"timeouts": {

src/test/resources/settings.json

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"browserName" : "chrome",
2+
"browserName" : "opera",
33
"isRemote": false,
44
"remoteConnectionUrl": "http://localhost:4444/wd/hub",
55
"isElementHighlightEnabled" : true,
@@ -20,6 +20,21 @@
2020
},
2121
"startArguments": []
2222
},
23+
"edge": {
24+
"webDriverVersion": "latest",
25+
"capabilities": {
26+
"enableVNC": true
27+
},
28+
"options": {
29+
"intl.accept_languages": "en",
30+
"safebrowsing.enabled": "true",
31+
"profile.default_content_settings.popups": "0",
32+
"disable-popup-blocking": "true",
33+
"download.prompt_for_download": "false",
34+
"download.default_directory": "//home//selenium//downloads"
35+
},
36+
"startArguments": []
37+
},
2338
"firefox": {
2439
"webDriverVersion": "latest",
2540
"capabilities": {
@@ -49,8 +64,40 @@
4964
"ignoreProtectedModeSettings": true
5065
}
5166
},
67+
"opera": {
68+
"webDriverVersion": "latest",
69+
"binaryLocation": "%USERPROFILE%\\AppData\\Local\\Programs\\Opera\\launcher.exe",
70+
"capabilities": {
71+
"enableVNC": true
72+
},
73+
"options": {
74+
"intl.accept_languages": "en",
75+
"safebrowsing.enabled": "true",
76+
"profile.default_content_settings.popups": "0",
77+
"disable-popup-blocking": "true",
78+
"download.prompt_for_download": "false",
79+
"download.default_directory": "./downloads"
80+
},
81+
"startArguments": ["--remote-debugging-port=9222", "--no-sandbox", "--disable-dev-shm-usage"]
82+
},
5283
"safari": {
5384
"downloadDir": "/Users/username/Downloads"
85+
},
86+
"yandex": {
87+
"webDriverVersion": "102.0.5005.61",
88+
"binaryLocation": "%USERPROFILE%\\AppData\\Local\\Yandex\\YandexBrowser\\Application\\browser.exe",
89+
"capabilities": {
90+
"enableVNC": true
91+
},
92+
"options": {
93+
"intl.accept_languages": "en",
94+
"safebrowsing.enabled": "true",
95+
"profile.default_content_settings.popups": "0",
96+
"disable-popup-blocking": "true",
97+
"download.prompt_for_download": "false",
98+
"download.default_directory": "//home//selenium//downloads"
99+
},
100+
"startArguments": []
54101
}
55102
},
56103
"timeouts": {

0 commit comments

Comments
 (0)