|
1 | 1 | package com.yourcompany.Tests; |
2 | 2 |
|
3 | 3 | import org.openqa.selenium.JavascriptExecutor; |
| 4 | +import org.openqa.selenium.MutableCapabilities; |
4 | 5 | import org.openqa.selenium.WebDriver; |
5 | | -import org.openqa.selenium.remote.CapabilityType; |
6 | | -import org.openqa.selenium.remote.DesiredCapabilities; |
| 6 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 7 | +import org.openqa.selenium.edge.EdgeOptions; |
| 8 | +import org.openqa.selenium.firefox.FirefoxOptions; |
7 | 9 | import org.openqa.selenium.remote.RemoteWebDriver; |
8 | 10 | import org.testng.ITestResult; |
9 | 11 | import org.testng.annotations.AfterMethod; |
10 | 12 | import org.testng.annotations.DataProvider; |
11 | | -import org.testng.annotations.Listeners; |
12 | 13 |
|
13 | 14 | import java.lang.reflect.Method; |
14 | 15 | import java.net.MalformedURLException; |
15 | | -import java.net.URL;import java.util.HashMap; |
16 | | -import java.rmi.UnexpectedException; |
| 16 | +import java.net.URL; |
17 | 17 | import java.util.HashMap; |
18 | 18 | import java.util.Map; |
19 | 19 |
|
@@ -83,29 +83,47 @@ public String getSessionId() { |
83 | 83 | * @throws MalformedURLException if an error occurs parsing the url |
84 | 84 | */ |
85 | 85 | protected void createDriver(String browser, String version, String os, String testName) |
86 | | - throws MalformedURLException, UnexpectedException { |
87 | | - DesiredCapabilities capabilities = new DesiredCapabilities(); |
88 | | - |
89 | | - capabilities.setCapability(CapabilityType.BROWSER_NAME, browser); |
90 | | - capabilities.setCapability(CapabilityType.BROWSER_VERSION, version); |
91 | | - capabilities.setCapability(CapabilityType.PLATFORM_NAME, os); |
| 86 | + throws MalformedURLException { |
| 87 | + MutableCapabilities options = optionsForBrowser(browser); |
| 88 | + options.setCapability("browserVersion", version); |
| 89 | + options.setCapability("platformName", os); |
92 | 90 |
|
93 | 91 | Map<String, Object> testingBotOptions = new HashMap<>(); |
94 | 92 | testingBotOptions.put("name", testName); |
95 | 93 | if (buildTag != null) { |
96 | 94 | testingBotOptions.put("build", buildTag); |
97 | 95 | } |
98 | | - capabilities.setCapability("tb:options", testingBotOptions); |
| 96 | + options.setCapability("tb:options", testingBotOptions); |
99 | 97 |
|
100 | 98 | webDriver.set(new RemoteWebDriver( |
101 | 99 | new URL("https://" + key + ":" + secret + "@hub.testingbot.com/wd/hub"), |
102 | | - capabilities)); |
| 100 | + options)); |
103 | 101 |
|
104 | 102 | // set current sessionId |
105 | 103 | String id = ((RemoteWebDriver) getWebDriver()).getSessionId().toString(); |
106 | 104 | sessionId.set(id); |
107 | 105 | } |
108 | 106 |
|
| 107 | + /** |
| 108 | + * Returns the Selenium 4 {@link MutableCapabilities} (browser Options) instance |
| 109 | + * matching the requested browser name. |
| 110 | + * |
| 111 | + * @param browser the browser name as supplied by the DataProvider |
| 112 | + * @return the matching browser Options instance |
| 113 | + */ |
| 114 | + private MutableCapabilities optionsForBrowser(String browser) { |
| 115 | + switch (browser.toLowerCase()) { |
| 116 | + case "chrome": |
| 117 | + return new ChromeOptions(); |
| 118 | + case "firefox": |
| 119 | + return new FirefoxOptions(); |
| 120 | + case "microsoftedge": |
| 121 | + return new EdgeOptions(); |
| 122 | + default: |
| 123 | + throw new IllegalArgumentException("Unsupported browser: " + browser); |
| 124 | + } |
| 125 | + } |
| 126 | + |
109 | 127 | /** |
110 | 128 | * Method that gets invoked after test. |
111 | 129 | * Dumps browser log and closes the browser |
|
0 commit comments